Data Structures and Algorithms
[Link],
Associate Professor,
School of Computing Science and Engineering,
VIT-Chennai.
A Taxonomy of Trees
General Trees – any number of children / node
Binary Trees – max 2 children / node
Heaps – parent < (>) children
Binary Search Trees
Binary Trees
Binary search tree (Properties):
Every element has a unique key.
The keys in a nonempty left subtree is smaller than
the key in the root of subtree (LST<Root).
The keys in a nonempty right subtree is larger than
the key in the root of subtree (RST>Root).
The left and right subtrees are also binary search
trees.
(LST < Root < RST ) – To be satisfied for the entire
Binary tree and each subtree.
Binary Search Trees
Binary Search Trees (BST) are a type of Binary Trees
with a special organization of data.
This data organization leads to O(log n) complexity
for searches, insertions and deletions in certain types
of the BST (balanced trees).
Organization Rule for BST
• the values in all nodes in the left subtree of a node are less than
the node value
• the values in all nodes in the right subtree of a node are greater
than the node values 63
41 89
34 56 72 95
Organization Rule for BST (Not a BST)
63
34 95
41 56 72 89
Binary Tree
struct searchtree
{
short int Element; // Data Field
node *right, *left; // Address Field
};
BST Operations: Search
Searching in the BST
method search(key)
• implements the binary search based on comparison of the items
in the tree
• the items in the BST must be comparable (e.g integers, string,
etc.)
The search starts at the root. It probes down, comparing the
values in each node with the target, till it finds the first item equal
to the target. Returns this item or null if there is none.
Search in BST - Pseudocode
if the tree is empty
return NULL
else if the item in the node equals the target
return the node value
else if the item in the node is greater than the target
return the result of searching the left subtree
else if the item in the node is smaller than the target
return the result of searching the right subtree
Search(Find) in a BST
Find(Elementtype x,searchtree T)
{
/* return a pointer to the node that
contains key. If there is no such
node, return NULL */
if (T==Null) return NULL or print“X not found”;
if (x<T->Element) return find(x,T->Left);
else
if (X>T->Element)
return find(x,T->Right);
else
return T or print”X found”; 2000 10 3000
}
T
4000 5 Null 5000 12 6000
Null 3 Null Null 11 Null Null 13 Null
Find min in a BST
Findmin(searchTree T)/* Non-recursive*/
{
/* return a pointer to the node that
contains key. If there is no such
node, return NULL */
if (T!=Null)
while(T->Left!=Null)
T=T->Left; 2000 10 3000
return T;
T
}
4000 5 Null 5000 12 6000
Null 3 Null Null 11 Null Null 13 Null
Find max in a BST
Findmax(searchTree T)/* Non-recursive*/
{
/* return a pointer to the node that
contains key. If there is no such
node, return NULL */
if (T!=Null)
while(T->Right!=Null)
T=T->Right; 2000 10 3000
return T;
T
}
4000 5 Null 5000 12 6000
Null 3 Null Null 11 Null Null 13 Null
Find min in a BST
Findmin(searchTree T)/*Recursive*/
{
/* return a pointer to the node that
contains key. If there is no such
node, return NULL */
if (T==Null) return NULL;
else
if (T->Left==Null) return T;
else
return Findmin(T->Left) 2000 10 3000
}
T
4000 5 Null 5000 12 6000
Null 3 Null Null 11 Null Null 13 Null
Find max in a BST
Findmax(searchTree T)/*Recursive*/
{
/* return a pointer to the node that
contains key. If there is no such
node, return NULL */
if (T==Null) return NULL;
else
if (T->Right==Null) return T;
else
return Findmin(T->Right) 2000 10 3000
}
T
4000 5 Null 5000 12 6000
Null 3 Null Null 11 Null Null 13 Null
BST Operations: Insertion
method insert(key)
places a new item near the frontier of the BST while retaining its
organization of data:
starting at the root it probes down the tree till it finds a
node whose left or right pointer is empty and is a logical place
for the new value
uses a binary search to locate the insertion point
is based on comparisons of the new item and values of nodes
in the BST
Elements in nodes must be comparable!
3 Comparisons are being made ( = , < , > ) accordingly elements are
inserted.
Case 1: The Tree is Empty
Set the root to a new node containing the item
Case 2: The Tree is Not Empty
Call a recursive helper method to insert the
item 10 > 7
10
7
5 9 10 > 9
4 6 8 10
Insertion in BST - Pseudocode
if tree is empty
create a root node with the new key
else
compare key with the top node
if key = node key
replace the node with the new value
else if key > node key
compare key with the right subtree:
if subtree is empty create a leaf node
else add key in right subtree
else key < node key
compare key with the left subtree:
if the subtree is empty create a leaf node
else add key to the left subtree
Insertion into a BST
2000 10 3000
4000 5 Null 5000 12 6000
Null 3 Null Null 11 Null Null 13 Null
Insertion into a BST
search insert (Element Type X, SearchTree T)
{ Null 10 Null
if(T==Null)
{ /* Create and return a one-node tree */ 1000 T
T=malloc(sizeof(Struct Treenode));
if(T==Null)
FatalError(“out of Space”);
else
{
T->Element = X;
T->Left=T->Right = Null; 2000 10 3000
} 1000
} 4000 5 Null 5000 12 6000
else
if(x<T->Element) 2000 3000
T->Left=Insert(X,T->Left);
Null 3 Null Null 11 Null Null 13 Null
else
if(x>T->Element) 4000 5000 6000
T->Right=Insert(X,T->Right);
/*else x is in the Tree already; we Will do nothing */
return(T);
}
Write non-recursive algorithm to perform insertion in a BST
For the following data’s form a Binary Search tree by
doing insertion operation. Show the step by step insertion
operation.
12 , 4 , 6 , 5 , 3 , 14 , 13 , 23 , 15
12
14
4
3 6 13 23
5 15
BST Shapes
The order of supplying the data determines where it is
placed in the BST , which determines the shape of the BST
Create BSTs from the same set of data presented each time
in a different order:
a) 17 4 14 19 15 7 9 3 18 10
b) 9 10 17 4 3 7 14 16 15 19
c) 19 17 16 15 14 10 9 7 4 3 can you guess this shape?
BST Shapes
a) 17 4 14 19 15 7 9 3 18 10
17
4 19
3 14 18
7 15
b) 9 10 17 4 3 7 14 16 15 19
c) 19 17 16 15 14 10 9 7 4 3 can you guess this shape?
BST Operations: Removal
removes a specified item from the BST and adjusts the tree
uses a binary search to locate the target item:
starting at the root it probes down the tree till it finds the
target or reaches a leaf node (target not in the tree)
removal of a node must not leave a ‘gap’ in the tree,
Removal in BST - Pseudocode
method remove (key)
I if the tree is empty return false
II Attempt to locate the node containing the target using the
binary search algorithm
if the target is not found return false
else the target is found, so remove its node:
Case 1: if the node has 2 empty subtrees
replace the link in the parent with null
Case 2: if the node has a left and a right subtree
- replace the node's value with the max value in the
left subtree
- delete the max node in the left subtree
Removal in BST - Pseudocode
Case 3: if the node has no left child
- link the parent of the node
- to the right (non-empty) subtree
Case 4: if the node has no right child
- link the parent of the target
- to the left (non-empty) subtree
Removal in BST: Example
Case 1: removing a node with 2 EMPTY SUBTREES
(Deleting a terminal node)
parent 7
cursor 5 9
4 6 8 10
7
Removing 4
replace the link in the
5 9
parent with null
6 8 10
Removal in BST: Example
Case 2: removing a node with 2 SUBTREES (Complete Node)
-(i)replace the node's value with the max value in the left subtree
(Inorder Predecessor)4->5->6->7->8->9->10
delete the max node in the left subtree
cursor Removing 7 cursor
7 6
5 9 5 9
4 6 8 10
4 8 10
Removal in BST: Example
Case 2: removing a node with 2 SUBTREES
-(i)replace the node's value with the min value in the right subtree
(Inorder Successor)4->5->6->7->8->9->10
delete the min node in the right subtree
cursor Removing 7 cursor
7 8
5 9 5 9
4 6 8 10
4 6 10
Removal in BST: Example
Case 3: removing a node with 1 EMPTY SUBTREE
the node has no left child:
link the parent of the node to the right (non-empty) subtree
parent
parent
7 7
cursor
5 9 cursor 5 9
6 8 10 6 8 10
Removal in BST: Example
Case 4: removing a node with 1 EMPTY SUBTREE
the node has no right child:
link the parent of the node to the left (non-empty) subtree
Removing 5
parent
parent
7 cursor 7
cursor
5 9 5 9
4 8 10 4 8 10
Analysis of BST Operations
The complexity of operations get, insert and
remove in BST is O(h) , where h is the height.
O(log n) when the tree is balanced. The updating
operations cause the tree to become unbalanced.
The tree can degenerate to a linear shape and the
operations will become O (n)
Best Case
BST tree = new BST();
[Link] ("E");
[Link] ("C");
[Link] ("D");
[Link] ("A");
[Link] ("H");
[Link] ("F");
[Link] ("K");
>>>> Items in advantageous order:
Output:
K
H
F
E
D
C
A
Worst Case
BST tree = new BST();
for (int i = 1; i <= 8; i++)
[Link] (i);
Output: >>>> Items in worst order:
8
7
6
5
4
3
2
1
Random Case
tree = new BST ();
for (int i = 1; i <= 8; i++)
[Link](random());
Output: >>>> Items in random order:
X
U
P
O
H
F
B
Better Search Trees
Prevent the degeneration of the BST :
A BST can be set up to maintain balance during
updating operations (insertions and removals)
Types of ST which maintain the optimal performance:
splay trees
AVL trees
2-4 Trees
Red-Black trees
B-trees