8 static BinaryTree dummy;
10 public struct BinaryTree
15 void OnSerialize(IOChannel channel)
17 channel.Serialize(root);
20 void OnUnserialize(IOChannel channel)
22 channel.Unserialize(root);
23 count = root ? root.count : 0;
26 int CompareInt(uint a, uint b)
28 return (a > b) ? 1 : ((a < b) ? - 1 : 0);
31 int CompareString(char * a, char * b)
33 return (a && b) ? strcmp(a, b) : -1;
36 void ::FreeString(char * string)
41 int (*CompareKey)(BinaryTree tree, uint a, uint b);
42 void (*FreeKey)(void * key);
54 if(!CompareKey) CompareKey = CompareInt;
57 else if(root.Add(this, node))
58 root = node.Rebalance();
68 if(!CompareKey) CompareKey = CompareInt;
70 return root ? root.Find(this, key) : null;
73 BTNode FindString(char * key)
75 return root ? root.FindString(key) : null;
78 BTNode FindPrefix(char * key)
80 return root ? root.FindPrefix(key) : null;
83 BTNode FindAll(uint key)
85 return root ? root.FindAll(key) : null;
88 void Remove(BTNode node)
90 BTNode parent = node.parent;
93 if(!root || !root.FindNode(node))
95 printf("Removing node not in binary tree\n");
98 /*BTNode swap = node.RemoveSwapRight();
102 if(parent || root == node)
104 root = node.RemoveSwapRight();
108 root = swap.Rebalance();
110 root = parent.Rebalance();
112 root = root.Rebalance();
119 void Delete(BTNode node)
121 void * voidNode = node;
126 char * Print(char * output, TreePrintStyle tps)
129 if(root) root.Print(output, tps);
135 return root ? root.Check(this) : true;
138 property BTNode first { get { return root ? root.minimum : null; } }
139 property BTNode last { get { return root ? root.maximum : null; } }
142 public struct StringBinaryTree : BinaryTree
144 void OnSerialize(IOChannel channel)
146 channel.Serialize((StringBTNode)root);
149 void OnUnserialize(IOChannel channel)
151 StringBTNode root = null;
152 channel.Unserialize(root);
153 this.root = (BTNode)root;
154 count = root ? this.root.count : 0;