search tree
TRANSCRIPT
-
8/14/2019 Search Tree
1/50
1
Chapter 5 Part 2: Search Trees
Binary search trees AVL trees
-
8/14/2019 Search Tree
2/50
2
Binary Search Trees
All items in the left subtree < the root . All items in the right subtree >= the
root .
Each subtree is itself a binary searchtree.
-
8/14/2019 Search Tree
3/50
3
Binary Search Tree Traversals
23
18
12 20
44
35 52
preorder23 18 12 20 44 3552
postorder12 20 18 35 52 4423
inorder12 18 20 23 35 4452
-
8/14/2019 Search Tree
4/50
4
Find Smallest Node
Algorithm findSmallestBST (val root )
Finds the smallest node in a BST
Pre root is a pointer to a non-empty BST
Return address of smallest node1 if (root > left = null)
1 return (root)
2 return findSmallestBST (root > left)
End findSmallestBST
23
18
12 20
44
35 52
-
8/14/2019 Search Tree
5/50
5
Find Largest Node
Algorithm findLargestBST (val root )
Finds the largest node in a BST
Pre root is a pointer to a non-empty BST
Return address of largest node1 if (root > right = null)
1 return (root)
2 return findLargestBST (root > right)
End findLargestBST
23
18
12 20
44
35 52
-
8/14/2019 Search Tree
6/50
6
BST SearchAlgorithm searchBST (val root , val arg )Searches a binary search tree for a given value
Pre root is a pointer to a non-empty BSTarg is the key value requested
Return the node address if the value is found; null otherwise1 if (root = null)
1 return null2 else if (arg = root > key)
1 return root3 else if (arg < root > key)
1 searchBST (root > left, arg)4 else if (arg > root > key)
1 searchBST (root > right, arg)5 else
1 return nullEnd searchBST
23
18
12 20
44
35 52
-
8/14/2019 Search Tree
7/50
7
BST Insertion
23
18
12 20
44
35 52
23
18
12 20
44
35 52
19
23
18
12 20
44
35 52
22
19
22
19
Taking place at anode having a nullbranch
-
8/14/2019 Search Tree
8/50
8
Iterative BST InsertionAlgorithm
Algorithm insertBST (ref root , val new )
Inserts a new node into BST using iteration
Pre root is address of the root
new is address of the new nodePost new node inserted into the tree
-
8/14/2019 Search Tree
9/50
9
Iterative BST InsertionAlgorithm
1 if (root = null)1 root = new
2 else1 pWalk = root2 loop (pWalk not null)
1 parent = pWalk
2 if (new > key < pWalk > key)1 pWalk = pWalk > left
3 else1 pWalk = pWalk > right
Location found for the new node3 if (new > key < parent > key)
1 parent > left = new4 else
1 parent > right = new3 returnEnd insertBST
23
18
12 20
44
35 52
19
-
8/14/2019 Search Tree
10/50
10
Recursive BST InsertionAlgorithm
Algorithm addBST (ref root , val new )
Inserts a new node into BST using recursion
Pre root is address of the root
new is address of the new nodePost new node inserted into the tree
-
8/14/2019 Search Tree
11/50
11
Recursive BST InsertionAlgorithm
1 if (root = null)1 root = new
2 else1 if (new > key < root > key)
1 addBST (root > left, new)2 else
1 addBST (root > right, new)3 returnEnd addBST
23
18
12 20
44
35 52
19 22
-
8/14/2019 Search Tree
12/50
12
BST Deletion
Leaf node : set the deleted node'sparent link to null.
Node having only right subtree : attach
the right subtree to the deletednode's parent.
Node having only left subtree : attach
the left subtree to the deleted node'sparent.
-
8/14/2019 Search Tree
13/50
13
BST Deletion
Node having both subtrees
23
18
12 20
44
35 52
2219
23
12 20
44
35 52
2219
?
-
8/14/2019 Search Tree
14/50
14
BST Deletion
Node having both subtrees
23
18
12 20
44
35 52
2219
23
12 20
44
35 52
2219
12
Using largest node in the leftsubtree
-
8/14/2019 Search Tree
15/50
15
BST Deletion
Node having both subtrees
23
18
12 20
44
35 52
2219
23
12 20
44
35 52
2219
19
Using smallest node in the rightsubtree
-
8/14/2019 Search Tree
16/50
16
BST Deletion AlgorithmAlgorithm deleteBST (ref root , val dltKey )Deletes a node from a BST
Pre root is a pointer to a non-empty BSTdltKey is the key of the node to be deleted
Post node deleted & memory recycledif dltKey not found, root unchanged
Return true if node deleted; false otherwise1 if (root = null)
1 return false2 if (dltKey < root > data.key)
1 deleteBST (root > left, dltKey)3 else if (dltKey > root > data.key)
1 deleteBST (root > right, dltKey)4 else
Deleted node found
-
8/14/2019 Search Tree
17/50
17
BST Deletion Algorithm4 else
Deleted node found1 if (root > left = null)
1 dltPtr = root2 root = root > right3 recycle (dltPtr)4 return true
2 else if (root > right = null)1 dltPtr = root2 root = root > left3 recycle (dltPtr)4 return true
3 else1 dltPtr = root > left2 loop (dltPtr > right not null)
1 dltPtr = dltPtr > right3 root > data = dltPtr > data4 return deleteBST (root > left, dltPtr > data.key)
End deleteBST
-
8/14/2019 Search Tree
18/50
18
AVL Trees
The heights of the left subtree and theright subtree differ by no more thanone.
The left and right subtrees are AVL trees themselves.
(G.M. Adelson- Velskii and E.M. Landis, 1962 )
-
8/14/2019 Search Tree
19/50
19
AVL Trees
23
18
12 20
44
35 52
8 14
LH
LH
EH
E
H
EH
EH
EH
EH
EH
-
8/14/2019 Search Tree
20/50
20
Getting Unbalanced
18
12 20
8 14
18
12 20
8 14
4
insert 4
Left of Left
EH
EH
EH
EH
LH LH
LH
LH
EH
EH
E
H
-
8/14/2019 Search Tree
21/50
21
Getting Unbalanced
14
12 20
18
insert 44
Right of Right
23
14
12 20
18 23
44
EH
EH
EH
EH EH RH
EH
EH
RH
RH
RH
-
8/14/2019 Search Tree
22/50
22
Getting Unbalanced
18
12 20
8 14
18
12 20
8 14
insert 13
Right of Left
13
LH LH
EH
EH
EH
EH RH
EH
EH
LH
E
H
-
8/14/2019 Search Tree
23/50
23
Getting Unbalanced
14
12 20
18
insert 19
Left of Right
44
14
12 20
18 44
19
RH
EH
EHEH
EH
RH
EH LH
RH
EH
EH
-
8/14/2019 Search Tree
24/50
24
Balancing Trees
20
18
20
18
12
insert 12
Left of Left
rotate right18
12 20
LH
EH
EH
EH
EH
-
8/14/2019 Search Tree
25/50
25
Balancing Trees
18
12 20
8 14
18
12 20
8 14
4
insert 4
Left of Left
12
8 18
4 14
rotateright
20
LH
EH
EH
EH
EH
EH
LH
EH
EH
EH
EH
-
8/14/2019 Search Tree
26/50
26
Balancing Trees
12
18
insert 20
Right of Right
rotate left18
12 20
12
18
20
RH
EH
EH
E
H
EH
-
8/14/2019 Search Tree
27/50
27
Balancing Trees
14
12 20
insert44
Right of Right
20
14 23
12 18
rotateleft
4418 23
14
12 20
18 23
44
EH
EH
EH
EH
RH
EH
EH
EH
EH
EH
RH
-
8/14/2019 Search Tree
28/50
28
Balancing Trees
12
4
12
8
4
rotateleft
Right of Left
rotate right8
4 12
8
LH
EH
EH
EH
EH
-
8/14/2019 Search Tree
29/50
29
Balancing Trees
18 rotateleft
20
Right of Left
12
4 14
16
14
1812
4 16
18
2014
12 16
4
20
rotateright
EH
EH
LH
EH
EH
EH
LH
EH
EH
EH
EH
-
8/14/2019 Search Tree
30/50
30
Balancing Trees
12
44
rotateright
Left of Right
rotate left18
12 44
18
12
18
44
RH
LH
EH
EH
EH
-
8/14/2019 Search Tree
31/50
31
Balancing Trees
18
12 44
rotateright
52
Left of Right
23
20
rotateleft
18
12 23
4420
52
23
18 44
522012
EH
EH
RH
LH
LH
EH
EH
EH
EH
EH
RH
-
8/14/2019 Search Tree
32/50
32
AVL Node Structure
Nodekey
data
leftSubTree
rightSubTree bal
End Node
18
12 44
5223
20
RH
EH
LH
LH EH
EH
-
8/14/2019 Search Tree
33/50
33
AVL Insertion Algorithm
Algorithm insertAVL (ref root , val newPtr ,
ref taller )
Inserts a new node into an AVL tree using recursion
Pre root is address of the rootnewPtr is address of the new node
Post taller = true indicating the tree's height has increased;false ortherwise
-
8/14/2019 Search Tree
34/50
34
AVL Insertion Algorithm
1 if (root = null)1 taller = true2 root = newPtr
2 else1 if (newPtr > key < root > key)
1 insertAVL (root > left, newPtr, taller)
2 if (taller)1 if (root left-high)
1 leftBalance (root, taller)2 else if (root right-high)
1 taller = false2 root > bal = EH
3 else1 root > bal = LH
2 else if (newPtr > key > root > key)
-
8/14/2019 Search Tree
35/50
35
AVL Insertion Algorithm
2 else if (newPtr > key > root > key)1 insertAVL (root > right, newPtr, taller)2 if (taller)
1 if (root right-high)1 rightBalance (root, taller)
2 else if (root left-high)
1 taller = false2 root > bal = EH
3 else1 root > bal = RH
3 else1 error ("Dupe data")2 recycle (newPtr)3 taller = false
3 returnEnd insertAVL
-
8/14/2019 Search Tree
36/50
36
AVL Left Balance Algorithm
Algorithm leftBalance (ref root , ref taller )
Balances an AVL tree that is left heavy
Pre root is address of the root
taller = truePost root has been updated (if necessary)
taller has been updated
-
8/14/2019 Search Tree
37/50
37
AVL Left Balance Algorithm
1 leftTree = root > left2 if (leftTree left-high)
Case 1: Left of left. Single rotation required.1 adjust balance factors2 rotateRight (root)3 taller = false
18
12 20
8 14
4
12
8 18
4 14 20
EH
EH
E
H
LH
LH
LH
EH
LH
EH
EH
E
H
EH
AVL Left Balance Algorithm
-
8/14/2019 Search Tree
38/50
38
AVL Left Balance Algorithm3 else
Case 2: Right of left. Double rotation required.1 rightTree = leftTree > right2 adjust balance factors3 rotateLeft (leftTree)4 rotateRight (root)5 taller = false
4 return
End leftBalance18
2012
4 14
16
14
1812
4 16
18
2014
12 16
4
20
EH
EH
E
H
LH
R
H
EH
EH
LH
EH
EH
EH
E
H
-
8/14/2019 Search Tree
39/50
39
Rotate Algorithms
Algorithm rotateRight (ref root )
Exchanges pointers to rotate the tree right
Pre root is address of the root
Post Node rotated and root updated
Rotate Algorithms
-
8/14/2019 Search Tree
40/50
40
Rotate Algorithms1 tempPtr = root > left2 root > left = tempPtr > right
3 tempPtr > right = root4 root = tempPtr5 returnEnd rotateRight
18
14
12 16
root
tempPtr20
4
18
14
12 16
root
tempPtr20
4
14
1812
4 16 20
root
tempPtr14
1812
4 16 20
root
-
8/14/2019 Search Tree
41/50
-
8/14/2019 Search Tree
42/50
42
AVL Deletion
12
9 18
delete 9
Right subtree is even-balanced
18
12 20
15
rotateleft
15 20
12
18
15 20
EH
EH
EH
EH
RH
LH
EH
EH
EH
-
8/14/2019 Search Tree
43/50
-
8/14/2019 Search Tree
44/50
44
AVL Deletion Algorithm
Algorithm deleteAVL (ref root , val deleteKey ,
ref shorter )
Deletes a node from an AVL tree
Pre root is address of the rootdeleteKey is the key of the node to be deleted
Post node deleted if foundshorter = true if the tree is shorter
Return success true if node deleted; false otherwise
-
8/14/2019 Search Tree
45/50
45
AVL Deletion Algorithm1 if (root null)
1 shorter = false2 return success = false
2 if (deleteKey < root > key)1 success = deleteAVL (root > left, deleteKey, shorter)2 if (shorter)
1 deleteRightBalance (root, shorter)3 return success
3 else if (deleteKey > root > key)1 success = deleteAVL (root > right, deleteKey, shorter)2 if (shorter)
1 deleteLeftBalance (root, shorter)
3 return success4 else
Delete node found - Test for leaf node
-
8/14/2019 Search Tree
46/50
46
AVL Deletion Algorithm4 else
Delete node found - Test for node having a null subtree1 deleteNode = root2 if (no left subtree)
1 root = root > right2 shorter = true
3 recycle (deleteNode)4 return success = true
3 else if (no right subtree)1 root = root > left2 shorter = true3 recycle (deleteNode)
4 return success = true4 else
Delete node has two subtrees
-
8/14/2019 Search Tree
47/50
47
AVL Deletion Algorithm
4 elseDelete node has two subtrees
1 exchPtr = root > left2 loop (exchPtr > right not null)
1 exchPtr = exchPtr > right3 root > data = exchPtr > data4 deleteAVL (root > left, exchPtr > data.key, shorter)5 if (shorter)
1 deleteRightBalance (root, shorter)6 return success = true
5 return
End deleteAVL
Delete Right Balance
-
8/14/2019 Search Tree
48/50
48
Delete Right BalanceAlgorithm
Algorithm deleteRightBalance (ref root , ref shorter )Adjusts the balance factors, and balance the tree by rotating left if necessary
Pre tree is shorter
Post balance factors updated and balance restoredroot updatedshorter updated
1 if (root left-high)1 root > bal = EH
2 else if (root even-high)
1 root > bal = RH2 shorter = false
3 elseTree was right high already. Rotate left
Delete Right Balance
-
8/14/2019 Search Tree
49/50
49
Delete Right BalanceAlgorithm
3 elseTree was right high already. Rotate left
1 rightTree = root > right2 if (rightTree left-high)
Double rotation required1 leftTree = rightTree > left2 leftTree > bal = EH3 rotateRight (rightTree)4 rotateLeft (root)
3 elseSingle rotation required
Delete Right Balance
-
8/14/2019 Search Tree
50/50
50
gAlgorithm
Single rotation required1 if (rightTree even-high)
1 root > bal = RH2 rightTree > bal = LH3 shorter = false
2 else1 root > bal = EH2 rightTree > bal = EH
3 rotateLeft (root)4 returnEnd deleteRightBalance