Design & Analysis of Algorithm
Binary Search Tree
The Islamia University of Bahawalpur [email protected]
Today Agenda
Tree data structure
Types of trees
Binary Search Tree (BST)
Operation on BST (search, insert, min, max, delete)
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Trees
Root
D u s ty
H oney B ear B ra n d y
B r u n h ild e T e rry C o y o te N ugget
G ill T an sey T w eed Zoe C ro c u s P r im r o s e N ous B e lle
leaf
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Definition of Tree
A tree is a finite set of one or more nodes
such that:
– There is a specially designated node called
the root.
– The remaining nodes are partitioned into n>=0 disjoint sets
T1, ..., Tn, where each of these sets is a tree.
– We call T1, ..., Tn the subtrees of the root.
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Tree Terminology
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Level and Depth
node (13)
degree of a node
leaf (terminal) Level
nonterminal
parent
children A 1
sibling 3 1
degree of a tree (3)
ancestor B
21
C
2 3
D
2 2
level of a node 2
height of a tree (4)
33
E F G H I J
2 30 30 31 30 30
K L M 4
0 40 4 0 4
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Terminology
The degree of a node is the number of subtrees of the node
– The degree of A is 3; the degree of C is 1.
The node with degree 0 is a leaf or terminal node.
A node that has subtrees is the parent of the roots of the
subtrees.
The roots of these subtrees are the children of the node.
Children of the same parent are siblings.
The ancestors of a node are all the nodes
along the path from the root to the node.
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Binary Trees
A binary tree is a finite set of nodes that is either empty or
consists of a root and two disjoint binary trees called the left
subtree and the right subtree.
Any tree can be transformed into binary tree.
– by left child-right sibling representation
The left subtree and the right subtree are distinguished.
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Binary Tree Types
Full Binary Tree
It is a special kind of a binary tree that has either zero children or two children. It means that all the nodes
in that binary tree should either have two child nodes of its parent node or the parent node is itself the leaf
node or the external node.
Complete Binary tree
A complete binary tree is another specific type of binary tree where all the tree levels are filled entirely with
nodes, except the lowest level of the tree. Also, in the last or the lowest level of this binary tree, every node
should possibly reside on the left side. Here is the structure of a complete binary tree:
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Tree Types
Perfect Binary Tree
A binary tree is said to be ‘perfect’ if all the internal nodes have strictly two children, and every external or
leaf node is at the same level or same depth within a tree
Balance Tree
A binary tree is said to be ‘balanced’ if the tree height is O(logN), where ‘N’ is the number of nodes.
Skew / Degenerate Binary Tree
A binary tree is said to be a degenerate binary tree or pathological binary tree if every internal node has
only a single child.
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Samples of Trees
Skewed Binary Tree Complete Binary Tree
A A 1 A
B B 2 B C
C
3 D E F G
D
4 H I
E 5
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Full BT VS Complete BT
A full binary tree of depth k is a binary tree of
depth k having 2 -1 nodes, k k>=0.
A binary tree with n nodes and depth k is
complete iff its nodes correspond to the nodes
numbered from 1 to n in the full binary tree of
depth k. Complete binary tree Full binary tree of depth 4
A A
B C B C
D E F G D E F G
H I J K L M N O
H I
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Linked Representation
typedef struct node *tree_pointer;
typedef struct node {
int data;
tree_pointer left_child, right_child;
};
data
left_child data right_child
left_child right_child
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Binary Search Tree
Dictionary Data Structure
Search tree property
all keys in left subtree smaller
than root’s key
8
all keys in right subtree larger
than root’s key
5 11
result:
easy to find any given key
2 6 10 12
inserts/deletes by changing
links 4 7 9 14
13
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Binary Search Tree Property
Binary search tree property:
If y is in left subtree of x,
then key [y] ≤ key [x] 5
3 7
If y is in right subtree of x, 2 5 9
then key [y] ≥ key [x]
15
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Binary Search Trees
Support many dynamic set operations
SEARCH, MINIMUM, MAXIMUM, PREDECESSOR,
SUCCESSOR, INSERT, DELETE
Running time of basic operations on binary search trees
On average: (lgn)
The expected height of the tree is lgn
In the worst case: (n)
The tree is a linear chain of n nodes
16
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Worst Case
(n)
17
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Traversing a Binary Search Tree
In-order tree walk:
Root is printed between the values of its left and right subtrees: left, root, right
Keys are printed in sorted order
Preorder tree walk:
root printed first: root, left, right
Post-order tree walk:
root printed last: left, right, root
5 Inorder: 2 3 5 5 7 9
Preorder: 5 3 2 5 7 9
3 7
2 5 9 Postorder: 2 5 3 9 7 5
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Traversing a Binary Search Tree
Alg: INORDER-TREE-WALK(x)
1. if x NIL
2. then INORDER-TREE-WALK ( left [x] )
3. print key [x]
4. INORDER-TREE-WALK ( right [x] )
E.g.:
5
3 7
Output: 2 3 5 5 7 9
2 5 9
Running time:
(n), where n is the size of the tree rooted at
Design & Analysis of Algorithm
x @copyright – The Islamia University of Bahawalpur
Searching for a Key
Given a pointer to the root of a tree and a key k:
Return a pointer to a node with key k 5
if one exists
3 7
Otherwise return NIL
2 4 9
Idea
Starting at the root: trace down a path by comparing
k with the key of the current node:
If the keys are equal: we have found the key
If k < key[x] search in the left subtree of x
If k > key[x] search in the right subtree of x
20
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Example: TREE-SEARCH
15
Search for key 13:
6 18 15 6 7 13
3 7 17 20
2 4 13
9
21
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Searching for a Key
Alg: TREE-SEARCH(x, k) 5
if x = NIL or k = key [x]
3 7
then return x
2 4 9
if k < key [x]
then return TREE-SEARCH(left [x], k )
else
return TREE-SEARCH(right [x], k )
Running Time: O (h),
h – the height of the tree
22
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Finding the Minimum in a Binary Search Tree
Goal: find the minimum value in a BST
Following left child pointers from the root, until a NIL
is encountered
Alg: TREE-MINIMUM(x) 15
1. while left [x] NIL
2. do x ← left [x] 6 18
3. return x 3 7 17 20
2 4 13
Running time: O(h), h – height of tree 9
Minimum = 2
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Finding the Maximum in a Binary Search Tree
Goal: find the maximum value in a BST
Following right child pointers from the root, until a
NIL is encountered
Alg: TREE-MAXIMUM(x)
1. while right [x] NIL 15
2. do x ← right [x]
6 18
3. return x
3 7 17 20
2 4 13
Running time: O(h), h – height of tree
9
Maximum = 20
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Insertion
Goal:
Insert value v into a binary search tree
Idea:
If key [x] < v move to the right child of x, Insert value 13
else move to the left child of x
12
When x is NIL, we found the correct position
If v < key [y] insert the new node as y’s left child 5 18
else insert it as y’s right child 2 9 15 19
1 3 13 17
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Example: TREE-INSERT
x=root[T], y=NIL y
Insert 13: 12 12
x
5 18 5 18
2 9 15 19 2 9 15 19
1 3 17 1 3 17
12 12
x y
5 18 5 18
2 9 15 19 2 9 15 19
1 3 17 1 3 13 17
x = NIL
y = 15
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Deletion
Goal:
Delete a given node z from a binary search tree
Idea:
Case 1: z has no children
Delete z by making the parent of z point to NIL
15 15
5 16 5 16
3 12 20 3 12 20
z
10 13 18 23 10 18 23
6 delete 6
7 7
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Deletion
Case 2: z has one child
Delete z by making the parent of z point to z’s child,
instead of to z
15 delete 15
z
5 16 5 20
3 12 20 3 12 18 23
10 13 18 23 10
6 6
7 7
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Binary Search Trees - Summary
Operations on binary search trees:
SEARCH O(h)
PREDECESSOR O(h)
SUCCESOR O(h)
MINIMUM O(h)
MAXIMUM O(h)
INSERT O(h)
DELETE O(h)
These operations are fast if the height of the tree is small –
otherwise their performance is similar to that of a linked list
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Design & Analysis of Algorithm
End
Lecture – 6
Binary Search Tree (BST)
The Islamia University of Bahawalpur [email protected]