Sunteți pe pagina 1din 50

Chapter 5 – Part 2: Search

Trees
• Binary search trees
• AVL trees

1
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 search
tree.

2
Binary Search Tree
Traversals
preorder
23 18 12 20 44 35
23
52
18 44 postorder
12 20 18 35 52 44
12 20 35 52
23

inorder
12 18 20 23 35 44
52

3
Find Smallest Node

Algorithm findSmallestBST (val root <pointer>)


Finds the smallest node in a BST
Pre root is a pointer to a non-empty BST
Return address of smallest node 23

1 if (root −> left = null)


18 44
1 return (root)
2 return findSmallestBST (root −> left) 12 20 35 52

End findSmallestBST

4
Find Largest Node

Algorithm findLargestBST (val root <pointer>)


Finds the largest node in a BST
Pre root is a pointer to a non-empty BST
Return address of largest node 23

1 if (root −> right = null)


18 44
1 return (root)
2 return findLargestBST (root −> right)12 20 35 52

End findLargestBST

5
BST Search
Algorithm searchBST (val root <pointer>, val arg <key>)
Searches a binary search tree for a given value
Pre root is a pointer to a non-empty BST
arg is the key value requested
Return the node address if the value is found; null otherwise
1 if (root = null)
1 return null
2 else if (arg = root −> key)
1 return root 23
3 else if (arg < root −> key)
1 searchBST (root −> left, arg) 18 44
4 else if (arg > root −> key)
1 searchBST (root −> right, arg) 12 20 35 52
5 else
1 return null
End searchBST
6
BST Insertion
Taking place at a 23

node having a null


18 44
branch
12 20 35 52

19
23 23

18 44 22 18 44

12 20 35 52 12 20 35 52

19 19 22
7
Iterative BST Insertion
Algorithm
Algorithm insertBST (ref root <pointer>, val new <pointer>)
Inserts a new node into BST using iteration
Pre root is address of the root
new is address of the new node
Post new node inserted into the tree

8
Iterative BST Insertion
Algorithm
1 if (root = null) 23
1 root = new
2 else 18 44
1 pWalk = root
2 loop (pWalk not null) 12 20 35 52
1 parent = pWalk
2 if (new −> key < pWalk −> key) 19
1 pWalk = pWalk −> left
3 else
1 pWalk = pWalk −> right
Location found for the new node
3 if (new −> key < parent −> key)
1 parent −> left = new
4 else
1 parent −> right = new
3 return
9
End insertBST
Recursive BST Insertion
Algorithm
Algorithm addBST (ref root <pointer>, val new <pointer>)
Inserts a new node into BST using recursion
Pre root is address of the root
new is address of the new node
Post new node inserted into the tree

10
Recursive BST Insertion
Algorithm
23
1 if (root = null)
1 root = new 18 44
2 else
1 if (new −> key < root −> key) 12 20 35 52
1 addBST (root −> left, new)
2 else 19 22
1 addBST (root −> right, new)
3 return
End addBST

11
BST Deletion
• Leaf node: set the deleted node's
parent link to null.
• Node having only right subtree: attach
the right subtree to the deleted
node's parent.
• Node having only left subtree: attach
the left subtree to the deleted node's
parent.
12
BST Deletion
Node having both subtrees

23 23

18 44 ? 44

12 20 35 52 12 20 35 52

19 22 19 22

13
BST Deletion
Node having both subtrees

23 23

18 44 12 44

12 20 35 52 12 20 35 52

19 22 19 22

Using largest node in the left


subtree
14
BST Deletion
Node having both subtrees

23 23

18 44 19 44

12 20 35 52 12 20 35 52

19 22 19 22

Using smallest node in the right


subtree
15
BST Deletion Algorithm
Algorithm deleteBST (ref root <pointer>, val dltKey <key>)
Deletes a node from a BST
Pre root is a pointer to a non-empty BST
dltKey is the key of the node to be deleted
Post node deleted & memory recycled
if dltKey not found, root unchanged
Return true if node deleted; false otherwise
1 if (root = null)
1 return false
2 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

16
BST Deletion Algorithm
4 else
Deleted node found
1 if (root −> left = null)
1 dltPtr = root
2 root = root −> right
3 recycle (dltPtr)
4 return true
2 else if (root −> right = null)
1 dltPtr = root
2 root = root −> left
3 recycle (dltPtr)
4 return true
3 else
1 dltPtr = root −> left
2 loop (dltPtr −> right not null)
1 dltPtr = dltPtr −> right
3 root −> data = dltPtr −> data
4 return deleteBST (root −> left, dltPtr −> data.key)
End deleteBST 17
AVL Trees
• The heights of the left subtree and the
right subtree differ by no more than
one.
• The left and right subtrees are AVL
trees themselves.
(G.M. Adelson-Velskii and E.M. Landis, 1962 )

18
AVL Trees

23
LH
18 44
LH E
12 20 35 H 52
E E E E
8 H 14 H H H

E E
H H

19
Getting Unbalanced

18 18
LH insert 4 LH
12 20 12 20
E E LH E
8 H 14 H H
8 14
E E LH E
H H H
4
E
H

Left of
Left
20
Getting Unbalanced

14 14
R insert 44 R
12 H 20 12 H 20
E E E R
H 18 H 23 H 18 H 23
E E E R
H H H H 44
E
H

Right of
Right
21
Getting Unbalanced

18 18
LH insert 13 LH
12 20 12 20
E E R E
8 H 14 H H H
8 14
E E E LH
H H H 13
E
H

Right of
Left
22
Getting Unbalanced

14 14
R insert 19 R
12 H 20 12 H 20
E E E LH
H 18 H 44 H 18 44
E E R E
H H H 19 H

E
H

Left of Right

23
Balancing Trees

20 20 18
LH insert 12 rotate right
E
18 18 12 H 20
E E E
H 12 H H

Left of
Left
24
Balancing Trees

18 18 rotate 12
LH insert 4 right E
12 20 12 20 8 H 18
E E LH E
8 H 14 H 8 14 4 14 H 20
E E E E E
H H 4 H H H

Left of
Left
25
Balancing Trees

12 12 18
R insert 20 rotate left
E
H 18 18 H
12 20
E E E
H 20 H H

Right of
Right
26
Balancing Trees

14 14 rotate 20
R insert left E
12 H 20
44 12 20 14 H 23
E E E R
H 18 H 23 18 23 12 H 18 H 44
E E E E E
H H 44 H H H

Right of
Right
27
Balancing Trees

12 12 8
LH rotate rotate right
E
4
left 8 H
4 12
E E E
H 4 H H
8

Right of Left

28
Balancing Trees

18 rotate 18 rotate 14
LH left right E
12 20 14 20 12 H 18
E E LH E
4 H 14 H 12 16 4 16 H 20
E E E E E
H H 16 4 H H H

Right of Left

29
Balancing Trees

12 12 18
R rotate rotate left
E
H 44
right H
18 12 44
LH E E
18 H H
44

Left of Right
30
Balancing Trees

18 rotate 18 rotate 23
R right left E
12 H 44 12 23 18 H 44
E LH E R
H 23 52 20 44 12 H 20 H 52

LH E E E E
H H H H
20 52

Left of
Right
31
AVL Node Structure
Node
key <key type> 18
data <data type> R
12 H 44
leftSubTree <pointer>
E LH
rightSubTree <pointer> H 23 52

bal <LH, EH, RH> LH E


20 H
End Node E
H

32
AVL Insertion Algorithm

Algorithm insertAVL (ref root <pointer>, val newPtr <pointer>,


ref taller <boolean>)
Inserts a new node into an AVL tree using recursion
Pre root is address of the root
newPtr is address of the new node
Post taller = true indicating the tree's height has increased;
false ortherwise

33
AVL Insertion Algorithm
1 if (root = null)
1 taller = true
2 root = newPtr
2 else
1 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 = false
2 root −> bal = EH
3 else
1 root −> bal = LH
2 else if (newPtr −> key > root −> key)
34
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 = false
2 root −> bal = EH
3 else
1 root −> bal = RH
3 else
1 error ("Dupe data")
2 recycle (newPtr)
3 taller = false
3 return
End insertAVL 35
AVL Left Balance Algorithm

Algorithm leftBalance (ref root <pointer>, ref taller <pointer>)


Balances an AVL tree that is left heavy
Pre root is address of the root
taller = true
Post root has been updated (if necessary)
taller has been updated

36
AVL Left Balance Algorithm
1 leftTree = root −> left
2 if (leftTree left-high)
Case 1: Left of left. Single rotation required.
1 adjust balance factors
2 rotateRight (root)
3 taller = false

18 12
LH E
12 20 8 H 18
LH E LH E
8 14 H 4 14 H 20
LH E E E E
4 H H H H
37
E
H
AVL Left Balance Algorithm
3 else
Case 2: Right of left. Double rotation required.
1 rightTree = leftTree −> right
2 adjust balance factors
3 rotateLeft (leftTree)
4 rotateRight (root)
5 taller = false
4 return
End leftBalance

18 18 14
LH E
12 20 14 20 12 H 18
R E LH E
4 H 14 H 12 16 4 16 H 20
E E E E E
H H 16 4 H H H
38
E
H
Rotate Algorithms

Algorithm rotateRight (ref root <pointer>)


Exchanges pointers to rotate the tree right
Pre root is address of the root
Post Node rotated and root updated

39
Rotate Algorithms
1 tempPtr = root −> left r
2 root −> left = tempPtr −> right oot 18
3 tempPtr −> right = root tempPtr
4 root = tempPtr 14 20
5 return
End rotateRight 12 16

r r r
oot 18 oot 14 oot 14
tempPtr tempPtr
14 20 12 18 12 18

12 16 4 16 20 4 16 20

4 40
Rotate Algorithms
Algorithm rotateLeft (ref root <pointer>)
Exchanges pointers to rotate the tree left
Pre root is address of the root
Post Node rotated and root updated
1 tempPtr = root −> right
2 root −> right = tempPtr −> left
3 tempPtr −> left = root
4 root = tempPtr
5 return
End rotateLeft

41
AVL Deletion

12 12 rotate 18
R delete 9 left LH
9 H 18 18 12 20
E E E E
H 15 H 20 15 20 H 15 H

E E E
H H H

Right subtree is even-


balanced
42
AVL Deletion

12 12 rotate 18
R delete 9 left E
9 H 18 18 12 H 20
E R E E
H H 20 20 H H

E
H

Right subtree is not even-


balanced
43
AVL Deletion Algorithm

Algorithm deleteAVL (ref root <pointer>, val deleteKey <key>,


ref shorter <boolean>)
Deletes a node from an AVL tree
Pre root is address of the root
deleteKey is the key of the node to be deleted
Post node deleted if found
shorter = true if the tree is shorter
Return success true if node deleted; false otherwise

44
AVL Deletion Algorithm
1 if (root null)
1 shorter = false
2 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 success
4 else
Delete node found - Test for leaf node
45
AVL Deletion Algorithm
4 else
Delete node found - Test for node having a null subtree
1 deleteNode = root
2 if (no left subtree)
1 root = root −> right
2 shorter = true
3 recycle (deleteNode)
4 return success = true
3 else if (no right subtree)
1 root = root −> left
2 shorter = true
3 recycle (deleteNode)
4 return success = true
4 else
Delete node has two subtrees
46
AVL Deletion Algorithm
4 else
Delete node has two subtrees
1 exchPtr = root −> left
2 loop (exchPtr −> right not null)
1 exchPtr = exchPtr −> right
3 root −> data = exchPtr −> data
4 deleteAVL (root −> left, exchPtr −> data.key, shorter)
5 if (shorter)
1 deleteRightBalance (root, shorter)
6 return success = true
5 return
End deleteAVL

47
Delete Right Balance
Algorithm
Algorithm deleteRightBalance (ref root <pointer>, ref shorter
<pointer>)
Adjusts the balance factors, and balance the tree by rotating left if
necessary
Pre tree is shorter
Post balance factors updated and balance restored
root updated
shorter updated
1 if (root left-high)
1 root −> bal = EH
2 else if (root even-high)
1 root −> bal = RH
2 shorter = false
3 else
Tree was right high already. Rotate left 48
Delete Right Balance
Algorithm
3 else
Tree was right high already. Rotate left
1 rightTree = root −> right
2 if (rightTree left-high)
Double rotation required
1 leftTree = rightTree −> left
2 leftTree −> bal = EH
3 rotateRight (rightTree)
4 rotateLeft (root)
3 else
Single rotation required

49
Delete Right Balance
Algorithm
Single rotation required
1 if (rightTree even-high)
1 root −> bal = RH
2 rightTree −> bal = LH
3 shorter = false
2 else
1 root −> bal = EH
2 rightTree −> bal = EH
3 rotateLeft (root)
4 return
End deleteRightBalance

50

S-ar putea să vă placă și