Mod 3 Bcs 401
Mod 3 Bcs 401
How efficient are AVL trees? As with any search tree, the critical characteristic is the tree’s height. It
turns out that it is bounded both above and below.
The inequalities immediately imply that the operations of searching and insertion are (log n) in the
worst case.
The drawbacks of AVL trees are frequent rotations and the need to maintain bal ances for its nodes.
These drawbacks have prevented AVL trees from becoming the standard structure for implementing
dictionaries. At the same time, their un derlying idea—that of rebalancing a binary search tree via
rotations—has proved to be very fruitful and has led to discoveries of other interesting variations of
the classical binary search tree
2-3 Trees:
. A 2-3 tree is a tree that can have nodes of two kinds:2-nodesand3-nodes.A2-
nodecontainsasinglekeyK and has two children: the left child serves as the root of a subtree whose
keys are less than K, and the right child serves as the root of a subtree whose keys are greater than K.
(In other words, a 2-node is the same kind of node we have in the classical binary search tree.) A 3-
node contains two ordered keys K1 and K2 (K1<K2) and has three
children. The leftmost child serves as the root of a subtree with keys less than K1, the middle child
serves as the root of a subtree with keys between K1 and K2, and the rightmost child serves as the
root of a subtree with keys greater than K2
As for any search tree, the efficiency of the dictionary operations depends on the tree’s height. A 2-3
tree of height h with the smallest number of keys is a full tree of 2-nodes (such as the final tree in for
h = 2). Therefore, for any 2-3 tree of height h with n nodes, we get the inequality
Key values in a heap are ordered top down; i.e.,a sequence of values on any path from the
root to a leaf is decreasing (non increasing, if equal keys are allowed). However, there is no
left-to-right order in key values; i.e., there is no relationship among key values for nodes
either on the same level of the tree or, more generally, in the left and right subtrees of the
same node.
Important properties of heaps,
• To construct a heap for a given list of keys
• There are two principal alternatives for doing this.
• The first is the bottom-up heap construction algorithm-It initializes the essentially complete
binary tree with n nodes by placing keys in the order given and then “heapifies” the tree as
follows. Starting with the last parental node, the algorithm checks whether the parental
dominance holds for the key in this node. If it does not, the algorithm exchanges the node’s key
K with the larger key of its children and checks whether the parental dominance holds for K in
its new position. This process continues until the parental dominance for K is satisfied.
(Eventually, it has to because it holds automatically for any key in a leaf.) After completing the
“heapification” of the subtree rooted at the current parental node, the algorithm proceeds to do
the same for the node’s immediate predecessor. The algorithm stops after this is done for the
root of the tree.
Let h be the height of the tree. According to the first property of heaps in the list at the
beginning of the section, h =⌊log2 n⌋ or just ⌈log2 (n + 1) ⌉ −1=k −1 for the specific values of
n we are considering.
The total number of key comparisons involving a key on level i will be 2(h − i).Therefore,
the total number of key comparisons in the worst case will be
• The alternative (and less efficient) algorithm constructs a heap by successive insertions of a
new key into a previously constructed heap; some people call it the top-down heap
construction algorithm.
So how can we insert a new key K into a heap? First, attach a new node with key K in it after
the last leaf of the existing heap. Then sift K up to its appropriate place in the new heap as
follows. Compare K with its parent’s key: if the latter is greater than or equal to K, stop (the
structure is a heap); otherwise, swap these two keys and compare K with its new parent. This
swapping continues until K is not greater than its last parent or it reaches the root
Inserting a key (10) into the heap constructed.The new key is sifted up via a swap with
its parent until it is not larger than its parent (or is in the root).