Lecture 7 Binary Search Trees, AVL Trees
Lecture 7 Binary Search Trees, AVL Trees
These slides are prepared by Alaa Alslaity based on the lecture notes of Brian Srivastava-Trent University.
Binary Tree
Properties of a BST
All items in the left subtree are less than the root
All items in the right subtree are greater than or equal to the root
Each subtree is itself a BST
≥
3
Valid Binary Search Trees
Properties of a BST
All items in the left subtree are less than
the root
4
Invalid Binary Search Trees
5
What about this??
Disadvantage?
You need to insert in the right place, and,
that can create a hugely unbalanced tree,
so searching can be O(n) in the worst case
Binary Tree Using Nodes
10
BST Traversals
12
13
BST – Find Largest Node
14
BST Search
Compare target value to root
If target < root
take left subtree
If target > root
take right subtree
If target = root
we’ve found target, return it or its index
15
BST Search
algorithm searchBST (val root <pointer>, val argument <key>)
Return: node address if value found, null otherwise
if (root = null)
return null
if (argument < root -> key)
return searchtBST (root->left, argument)
elseif (argument > root -> key)
return searchtBST (root->right, argument)
else return root
16
BST Search
17
BST – Insert Node
18
BST – Insert Node
Examples
19
BST – Insert Node
Iterative Insert
algorithm insertBST (ref root <pointer>, val new)
if (root = null)
root = new
else
pwalk = root
loop (pwalk != null)
parent = pwalk
if (new->key < pwalk)
pwalk = pwalk->left
else
pwalk = pwalk->right
//End of loop
if (new->key < parent->key)
parent-> left = new
else
parent-> right = new
20
return
BST – Insert Node
Recursive Insert
21
BST – Insert Node
Example
22
BST – Insert Node
Example
23
Delete Node - BST
Four possible cases
A. Node to be deleted has no children
Set node’s parent to null
Recycle memory
24
Delete Node - BST
Four possible cases
25
Delete Node - BST
Four possible cases
C. Node to be deleted has only a left subtree
Attach the left subtree to deleted node’s parent
26
Delete Node - BST
Four possible cases
D. Node to be deleted has two subtrees
Find data to take place of the deleted node
Two choices
Find the largest node in the left subtree
Find the smallest node in the right subtree
27
algorithm deleteBST (ref root <pointer>, val dltKey <key>)
Delete Node – BST if (root = null)
Algorithm return false
if (dltKey < root->data.key)
return deleteBST (root->left, dltKey)
elseif (dltKey > root->data.key)
return deleteBST (root->right, dltKey)
Start with some core rule for that type of tree, such as
The maximum difference in height between two subtrees is 2
AVL Tree
Only insert to the right (meaning rotate so things can be inserted to the right)
AA Tree
37
Examples
Not an AVL
38
39
AVL Operations
40
AVL Node Structure RH
Right of right
Adding a node to the right of a RH tree
Right of left
Adding a node to the right of a LH tree
Left of right
Adding a node to the left of a RH tree 42
43
44
Balancing AVL Trees
re-balance tree
Need to rotate nodes
45
Rotation
Rotate right
Y X
X T3 Y
T1
T1 T2 T2 T3
Rotate Left
Sub-trees
Rotate Algorithms
47
Balancing AVL-Rotation
Left of Left
Balance the tree by rotating the out-
of-balance tree to the right
48
Balancing AVL-Rotation
Right of Right
Balance the tree by rotating the
unbalanced tree to the left
49
Balancing AVL-Rotation
Right of Left
Need to rotate two nodes
One to the left and the other
to the right
50
Balancing AVL-Rotation
Left of Right
Similar to Right of Left only
we first rotate to the right and
then to the left
51
AVL Insert
Start as in BST
Find the location to insert node
52
Example
Suppose that we need to insert 15. (right of left)
Three becomes unbalanced at 18
Rotate left (at 12)
Rotate right (at 18)