-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter09 BST
1/22
1Data Structures Binary Search Tree
9 Binary Search Trees
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter09 BST
2/22
2Data Structures Binary Search Tree
Objectives
At the end of the lesson, the student should be able to:
Discuss the properties of a binary search trees
Apply the operations on binary search trees
Improve search, insertion and deletion in binary search treesby maintaining the balance using AVL trees
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter09 BST
3/22
3Data Structures Binary Search Tree
Definition
Binary Search Tree
A binary tree that satisfies the BST property - the valueof each node in the tree is greater than the value of the
node in its left and it is less than the value in its right child
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter09 BST
4/22
4Data Structures Binary Search Tree
Linked Representation
Node structure:
If the BST is empty, the left and right pointers of bstHeadpoints to itself; otherwise, the right pointer points to the rootof the tree
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter09 BST
5/22
5Data Structures Binary Search Tree
Operations on BST
Searching for a Key In searching for a value, say k, three conditions are possible:
k = value at the node (successful search)
k < value at the node (search the left subtree)
k > value at the node (search the right subtree)
BSTNode search(int k){BSTNode p = bstHead.right;if (p == bstHead) return null;while (true){
if (k == p.info) return p; /* successful search */else if (k < p.info) /* go left */
if (p.left != null) p = p.left;
else return null; /* not found */else /* go right */if (p.right != null) p = p.right;else return null; /* not found */
}
}
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter09 BST
6/22
6Data Structures Binary Search Tree
Operations on BST
Insertion of a New Key BST property should always be maintained
Insert new key at leaf level:
1. Start the search at the root node. Declare a node p and make it point to the root
2. Do the comparison: if (k == p.info) return false // if key found, insertion not allowed
else if (k < p.info) p = p.left // go left
else p = p.right // if (k > p.info) go right
3. Insert the node (p now points to the new parent of node to insert)
newNode.info = k
newNode.left = null
newNode.right = null
if (k < p.info) p.left = newNode
else p.right = newNode
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter09 BST
7/22
7Data Structures Binary Search Tree
Operations on BST
Deletion of an existing key the BST property should bemaintained always
Case 1: Node d to be deleted is an external node(leaf):Action: update the child pointer of the parent p:
ifd is a left child, set p.left = null otherwise, set p.right = null
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter09 BST
8/22
8Data Structures Binary Search Tree
Operations on BST
Deletion of an existing key the BST property should bemaintained always
Case 2: Node d to be deleted is internal
Case 1: Ifd has a left subtree,1) Get the inorder predecessor, ip. Inorder predecessor isdefined as the rightmost node in the left subtree of thecurrent node, which in this case is d. It contains thepreceding key if the BST is traversed in inorder.
2) Get the parent ofip, say p_ip.3) Replace d with ip.
4) Remove ip from its old location by adjusting the pointers:a. Ifip is not a leaf node:
1) Set the right child ofp_ip to point to the left child ofip2) Set the left child ofip to point to the left child ofd
b. Set the right child ofip to point to the right child ofd
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter09 BST
9/22
9Data Structures Binary Search Tree
Operations on BST
Deletion of an existing key the BST property should bemaintained always
Case 2: Node d to be deleted is internal
Case 1:
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter09 BST
10/22
10Data Structures Binary Search Tree
Operations on BST
Deletion of an existing key the BST property should bemaintained always
Case 2: Node d to be deleted is internal
Case 2: If d has no left son but with a right subtree1)Get the inorder successor, is. Inorder successor is defined as theleftmost node in the right subtree of the current node. Itcontains the succeeding key if the BST is traversed in inorder.
2) Get the parent ofis, say p_is.3) Replace d with is.4) Remove is from its old location by adjusting the pointers:
a. Ifis is not a leaf node:1) Set the left child ofp_is to point to the right child ofis2) Set the right child ofis to point to the right child ofd
b. Set the left child ofis to point to the left child ofd
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter09 BST
11/22
11Data Structures Binary Search Tree
Operations on BST
Deletion of an existing key the BST property should bemaintained always
Case 2: Node d to be deleted is internal
Case 2:
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter09 BST
12/22
12Data Structures Binary Search Tree
Operations on BST
Time complexity of BST
The operations discussed all involve searching for a desired key
The search algorithm in a BST takes O(log2 n) time on the average
The search algorithm in a BST takes O(n) time on the worst case
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter09 BST
13/22
13Data Structures Binary Search Tree
Balanced Binary Search
Trees To ensure that searching takes O(log2 n), balance has to bemaintained in a BST
balance of a node - height of a node's left subtree minusthe height of its right subtree
AVL Tree
Most commonly used balanced BST
Created by G. Adelson-Velskii and E. Landis
Height-balanced tree wherein the the height difference between thesubtrees of a node, for every node in the tree, is at most 1
O(log2n) time complexity for searching, insertion and deletion
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter09 BST
14/22
14Data Structures Binary Search Tree
AVL Trees
Non-AVL Trees
AVL Trees
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter09 BST
15/22
15Data Structures Binary Search Tree
AVL Trees
Tree Rotations Simple Right Rotation (SRR) - used when the new item C is in the left
subtree of the left child B of the nearest ancestor A with balance factor+2
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter09 BST
16/22
16Data Structures Binary Search Tree
AVL TreesTree Rotations
Simple Left Rotation (SLR) - used when the new item C is in the rightsubtree of the right child B of the nearest ancestor A with balance factor -2
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter09 BST
17/22
17Data Structures Binary Search Tree
AVL Trees
Tree Rotations Left-Right Rotation (LRR) - used when the new item C is in the right
subtree of the left child B of the nearest ancestor A with balance factor +2
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter09 BST
18/22
18Data Structures Binary Search Tree
AVL TreesTree Rotations (LRR)
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter09 BST
19/22
19Data Structures Binary Search Tree
AVL TreesTree Rotations
Right-Left Rotation (RLR) - used when the new item C is in the leftsubtree of the right child B of the nearest ancestor A with balance factor-2
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter09 BST
20/22
20Data Structures Binary Search Tree
AVL TreesTree Rotations (RLR)
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter09 BST
21/22
21Data Structures Binary Search Tree
AVL Tree Insertion
Same as in BST but resulting balance has to be checked:
1. A leaf node is inserted into the tree with balance 0
2. Starting at the new node, pass a message up on the tree that theheight of the subtree containing the new node has increased by 1.
If the message is received by a node from its left subtree, 1 isadded to its balance, otherwise -1 is added.
If the resulting balance is +2 or -2, rotation has to beperformed as described.
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter09 BST
22/22
22Data Structures Binary Search Tree
Summary
A Binary Search Tree is a binary tree that satisfies the BSTproperty
Operations on BSTs include insertion of a new key, deletion of anexisting key, and searching for a key
The BST search algorithm runs in O(log2 n) time on the averageand O(n) time on the worst case
Balanced BSTs ensure that searching takes O(log2 n)
An AVL tree is a height-balanced tree wherein the height
difference between the subtrees of a node for every node in thetree is at most 1
Rotations have to be performed during insertion and deletion inan AVL tree to maintain its balance