The.Algorithm.Design.Manual.Springer-Verlag.1998
The.Algorithm.Design.Manual.Springer-Verlag.1998 The.Algorithm.Design.Manual.Springer-Verlag.1998
Binary Search Trees Next: Priority Queues Up: Fundamental Data Types Previous: Dictionaries Binary Search Trees Fast support of all dictionary operations is realized by binary search trees. A binary tree is a rooted tree where each node contains at most two children. Each child can be identified as either a left or right child. As shown in Figure , a binary tree can be implemented where each node has left and right pointer fields, an (optional) parent pointer, and a data field. Figure: Relationships in a binary search tree A binary search tree labels each node in a binary tree with a single key such that for any node labeled x, all nodes in the left subtree of x have keys < x while all nodes in the right subtree of x have keys > x. The search tree labeling enables us to find where any key is. Start at the root. If it does not contain the key we are searching for, proceed either left or right depending upon whether what we want occurs before or after the root key. This algorithm works because both the left and right subtrees of a binary search tree are binary search trees; the recursive structure yields a recursive algorithm. Accordingly, the dictionary Query operation can be performed in O(h) time, where h is the height of the tree. file:///E|/BOOK/BOOK/NODE26.HTM (1 of 3) [19/1/2003 1:28:24]
Binary Search Trees BinaryTreeQuery(x, k) if if (k < key[x]) then return x then return BinaryTreeQuery(left[x],k) else return BinaryTreeQuery(right[x],k) To insert a new item x with key k into a binary search tree T, it is important to place it where it can be later be found. There is only one such location in any binary search tree, namely by replacing the nil pointer found in T after an unsuccessful query for k. Replacing this nil pointer with a pointer to x is a simple, constant-time operation after the search has been performed in O(h) time. Deletion is somewhat more tricky than insertion, because the node selected to die may not be a leaf. Leaf nodes may be deleted without mercy, by clearing the pointer to the given node. However, internal nodes have children that must remain accessible after the deletion. By restructuring or relabeling the tree, however, the item to delete can always be made into a leaf and then removed. Details appear in any data structures text. When implemented using binary search trees, all three dictionary operations take O(h) time, where h is the height of the tree. The smallest height we could hope for occurs when the tree is perfectly balanced, where . In fact, if we insert the keys in random order, with high probability the tree will have height. However, if we get unlucky with our order of insertion or deletion, we can end up with a linear-height tree in the worst case. The worst case is a serious potential problem. Indeed, it occurs whenever the keys are inserted in sorted order. To avoid such worst-case performance, more sophisticated balanced binary search tree data structures have been developed that guarantee the height of the tree always to be . Therefore, all dictionary operations (insert, delete, query) take time each. Implementations of such balanced tree data structures as red-black trees are discussed in Section . file:///E|/BOOK/BOOK/NODE26.HTM (2 of 3) [19/1/2003 1:28:24]
- Page 89 and 90: References Pav82 T. Pavlidis. Algor
- Page 91 and 92: References Rei72 Rei91 Rei94 E. Rei
- Page 93 and 94: References Prentice Hall, Englewood
- Page 95 and 96: References SR95 SS71 SS90 SSS74 ST8
- Page 97 and 98: References Tin90 Mikkel Thorup. On
- Page 99 and 100: References Wel84 T. Welch. A techni
- Page 101 and 102: References Algorithms Mon Jun 2 23:
- Page 103 and 104: Correctness and Efficiency Next: Co
- Page 105 and 106: Correctness NearestNeighborTSP(P) P
- Page 107 and 108: Correctness Figure: A bad example f
- Page 109 and 110: Efficiency Next: Expressing Algorit
- Page 111 and 112: Keeping Score Next: The RAM Model o
- Page 113 and 114: The RAM Model of Computation substa
- Page 115 and 116: Best, Worst, and Average-Case Compl
- Page 117 and 118: The Big Oh Notation Figure: Illustr
- Page 119 and 120: Logarithms Next: Modeling the Probl
- Page 121 and 122: Logarithms justified in ignoring th
- Page 123 and 124: Modeling the Problem Figure: Modeli
- Page 125 and 126: About the War Stories Next: War Sto
- Page 127 and 128: War Story: Psychic Modeling Next: E
- Page 129 and 130: War Story: Psychic Modeling have pu
- Page 131 and 132: War Story: Psychic Modeling Next: E
- Page 133 and 134: Exercises (b) If I prove that an al
- Page 135 and 136: Fundamental Data Types Next: Contai
- Page 137 and 138: Containers Next: Dictionaries Up: F
- Page 139: Dictionaries Next: Binary Search Tr
- Page 143 and 144: Priority Queues Next: Specialized D
- Page 145 and 146: Specialized Data Structures Next: S
- Page 147 and 148: Sorting Next: Applications of Sorti
- Page 149 and 150: Applications of Sorting Figure: Con
- Page 151 and 152: Data Structures Next: Incremental I
- Page 153 and 154: Incremental Insertion Next: Divide
- Page 155 and 156: Randomization Next: Bucketing Techn
- Page 157 and 158: Randomization Next: Bucketing Techn
- Page 159 and 160: Bucketing Techniques Algorithms Mon
- Page 161 and 162: War Story: Stripping Triangulations
- Page 163 and 164: War Story: Stripping Triangulations
- Page 165 and 166: War Story: Mystery of the Pyramids
- Page 167 and 168: War Story: Mystery of the Pyramids
- Page 169 and 170: War Story: String 'em Up We were co
- Page 171 and 172: War Story: String 'em Up Figure: Su
- Page 173 and 174: Exercises Next: Implementation Chal
- Page 175 and 176: Exercises used to select the pivot.
- Page 177 and 178: Dynamic Programming Next: Fibonacci
- Page 179 and 180: Fibonacci numbers Next: The Partiti
- Page 181 and 182: Fibonacci numbers Next: The Partiti
- Page 183 and 184: The Partition Problem . What is the
- Page 185 and 186: The Partition Problem Figure: Dynam
- Page 187 and 188: Approximate String Matching Next: L
- Page 189 and 190: Approximate String Matching The val
Binary Search Trees<br />
BinaryTreeQuery(x, k)<br />
if<br />
if (k < key[x])<br />
then return x<br />
then return BinaryTreeQuery(left[x],k)<br />
else return BinaryTreeQuery(right[x],k)<br />
To insert a new item x with key k into a binary search tree T, it is important to place it where it can be<br />
later be found. <strong>The</strong>re is only one such location in any binary search tree, namely by replacing the nil<br />
pointer found in T after an unsuccessful query for k. Replacing this nil pointer with a pointer to x is a<br />
simple, constant-time operation after the search has been performed in O(h) time.<br />
Deletion is somewhat more tricky than insertion, because the node selected to die may not be a leaf. Leaf<br />
nodes may be deleted without mercy, by clearing the pointer to the given node. However, internal nodes<br />
have children that must remain accessible after the deletion. By restructuring or relabeling the tree,<br />
however, the item to delete can always be made into a leaf and then removed. Details appear in any data<br />
structures text.<br />
When implemented using binary search trees, all three dictionary operations take O(h) time, where h is<br />
the height of the tree. <strong>The</strong> smallest height we could hope for occurs when the tree is perfectly balanced,<br />
where . In fact, if we insert the keys in random order, with high probability the tree will have<br />
height. However, if we get unlucky with our order of insertion or deletion, we can end up with a<br />
linear-height tree in the worst case. <strong>The</strong> worst case is a serious potential problem. Indeed, it occurs<br />
whenever the keys are inserted in sorted order.<br />
To avoid such worst-case performance, more sophisticated balanced binary search tree data structures<br />
have been developed that guarantee the height of the tree always to be . <strong>The</strong>refore, all dictionary<br />
operations (insert, delete, query) take time each. Implementations of such balanced tree data<br />
structures as red-black trees are discussed in Section .<br />
file:///E|/BOOK/BOOK/NODE26.HTM (2 of 3) [19/1/2003 1:28:24]