AVL Tree Examples
1)
Consider inserting 46 into the following AVL Tree:
32
/
\
16
48
/
\
/
\
8
24
40
56
/
\
/
\
36
44
52
60
\
46, inserted here
Initially, using the standard binary search tree insert, 46 would
go to the right of 44. Now, let's trace through the rebalancing
process from this place.
First, we call the method on this node. Once we set its height,
we check to see if the node is balanced. (This simply looks up
the heights of the left and right subtrees, and decides if the
difference is more than 1.) In this case, the node is balanced, so
we march up to the parent node, that stores 44.
We will trace through the same steps here, setting the new
height of this node (this is important!) and determining that
this node is balanced, since its left subtree has a height of 1
and the right subtree has a height of 0.
Similarly, we set the height and decide that the nodes storing
40 and 48 are balanced as well. Finally, when we reach the root
node storing
32, we realize that our tree is imbalanced.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '09
 Guha
 Computer Science, Graph Theory, Binary Search, Harshad number, Selfbalancing binary search tree

Click to edit the document details