Lecture9 SearchTrees
Lecture9 SearchTrees
1
Introduction
• Will look at indepth discussion of 2
standard tree structures:
– Binary search Trees
– AVL Trees
• Both structures useful in sorting data
• Differ in that AVL trees are balanced
whereas Binary trees are not
2
Binary Search Trees
• As a recap: Given an array with elements
sorted in some order. Use binary search
algorithm yields efficient results. However,
very inefficient insertion and deletion
algorithms on an array soln use of linked
lists. Problem: search alg. sequential;.
• Need a data struct with efficient search alg.,
and insert and delete algs.
3
Binary Search Trees
• Binary search tree is a binary tree with
following properties
1. All items in the left subtree are less than root
2. All items in the right subtree are greater or
equal to the root
3. Each subtree is itself a binary search tree
4
Binary Search Trees
• Generally, each node represents a record rather
than a single data element
• Fig below reflects a binary search tree with K as
key
5
Example – Binary Search Trees
• a & b are complete, d nearly complete, c &
e neither complete nor balanced
6
Example – Invalid Binary search
Trees
7
Operations on B-Search Trees
• Binary Search tree traversal algorithms identical to those
those in chapter 7 (Trees)
• Give results for preorder, postorder, and inorder traversals
from given binary search tree? Which gives ordered list?
8
B-Search Tree Alg
Find Smallest Node
• In a b-search tree, node with smallest value
is the leftmost leaf node in the tree
• Hence follow left branches until you get to
a leaf
9
Find Smallest Node
10
Find Smallest Node
11
Find Largest Node
• Reverse of smallest node.
• Largest node is to the right subtree from the
root, hence start at root of tree, follow right
branches to last node in the tree
algorithm findLargestBST(val root <ptr>)
if (root->right in null)
return (root)
end if
return findLargestBST(root->right)
end findLargestBST 12
B-Search Tree Search
• Find a specific node in a b-search tree
algorithm searchBST(val root <ptr>, val arg <key>)
if (root is null)
return null //base case if not found
endif
if (arg < root->key)
return searchBST(root->left,arg)
else if (arg > root->key)
return searchBST(root->right,arg)
else return root //base case if found
end if
end searchBST 13
Insert Node
• All inserts take place at a leaf or a leaflike
node (a node that has only one null branch)
• Note: nodes with duplicate values will be
found in the right subtree in a BST.
• Will look at 2 ways:
– Iterative insert. See iterativeInsertBST.doc
– Recursive insert. See recursiveInsertBST.doc
14
Insert Node-Examples
15
Iterative Insert -Example
16
Recursive Insert-Example
17
Recursive Insert –Example conti
18
Delete Node
• 1st locate the node.
• Possible cases when you delete a node
1. Node to delete has no children
Set parent to delete node to null
2. Node to delete has only a right child
Attach subtree to delete node’s parent
3. Node to delete has only left subtree
Attach subtree to delete node’s parent
19
Delete Node
4. Node to delete has 2 subtrees
2 ways leaving tree balanced
1. Find largest node in deleted node’s left subtree and
move its data to replace deleted node’s data
2. Find smallest node on deleted node’s right subtree and
move its data to replace deleted node’s data
• See deleteBST.doc
20
Delete Node -Example
21
Delete Node - Example
22
AVL Trees
• Named after 2 Russian mathematicians who
created the structure (G.M. Adelson-Velskii
and E.M. Landis) acronym AVL
• An AVL tree is a height-balanced binary
search tree. Heights of the subtrees differ by
no more than 1
HL – HR <= 1
23
AVL Balance Factor
• Any node in an AVL tree has a balance
factor of either +1, 0, or –1.
• Following descriptive identifiers will be
used:
LH for left high (+1) – left subtree high than right
EH for even high (0) – subtrees have same height
RH for right high (-1) – left subtree shorter
24
AVL Tree - Example
25
Balancing Trees
26
Balancing Trees
• All unbalanced trees fall in one of these:
– Left of left: subtree of a tree that is left high
becoming left high
– Right of right: subtree of a tree that is right high
becoming right high
– Right of left: subtree of a tree that is left high
becoming right high
– Left of right: subtree of a tree that is right high
becoming left high
27
Balancing Trees – Example Cases
28
Balancing Trees – Example Cases
29
Case 1: Left of Left
• Must balance tree by rotating the out of balance node to
the right
30
Case 2: Right of Right
• Must balance tree by rotating out of balance node to the
left
31
Case 3: Right of Left
• Must rotate two nodes, one to the left and the other to the
right to balance tree
32
Case 4: Left of Right
• Two nodes must be rotated, one right, the other left.
33
AVL Node Structure
Node
key <keyType>
data <dataType>
left <ptr to node>
right <ptr to node>
bal <LH, EH, RH> //balance factor
End Node
34
AVL Insert
• All inserts take place at a leaf node
• Hence use inorder traversal algorithm to
locate appropriate leaf node by comparing
new data key to root.
• Once located connect leaf to it’s parent
node and back out of the tree constantly
checking the balance of each node
• If node out of balance, balance it
35
AVL Insert
36
AVL Insert - Design
37
Rotating Algorithm
• To rotate a node exchange the root and
appropriate subtree pointers
• Takes 4 steps:
• 3 steps for the exchange
• 1 step for resetting root ptr
38
AVL tree Rotate Right Algorithm
algorithm rotateRight(ref root <ptr>)
//exchanges ptrs to rotate tree right
Pre: root points to tree to be rotated
Post: node rotated and root updated
1 tempPtr = root->left
2 root->left = tempPtr->right
3 tempPtr->right = root
4 return tempPtr
end rotateRight
39
AVL Tree Rotate right
40
AVL tree Delete Balancing
41
42