CH9 - Tree
CH9 - Tree
Algorithm
Chapter Nine
Trees
Introduction
K L M
• Nodes that have degree ZERO are called Leaf nodes or Terminal
nodes.
Binary Tree
• Binary tree is a tree in which no node can have more than two
children or a tree in which each node has at most two children
called left child and right child.
Root
Root
A
A
B C
B C
D E
D E
F E
Contd.
• Strictly/Full binary tree: a binary tree where each node has either 0
or 2 children.
• Complete binary tree: a binary tree in which the length from the
root to any leaf node is either h or h-1 where h is the height of the
tree. The deepest level should also be filled from left to right.
Representation of a Binary Tree
1. Array representation
85
2. Linked List representation
80 90
Array representation:-
70 82 87 100
• Consider the following binary tree
85 80 70 82 90 87 100
Contd.
Linked List Representation:-
• The keys in the right subtree are larger than the keys in the
root.
• The keys in the left subtree are smaller than the keys in the
root.
• The left and the right subtrees are also binary search trees.
6 15
10
4 8 14 18
7 12 16 19
11 13
Contd.
• Ex1: Creating a binary search tree with the following values.
10 8 15 25 22 30
• Step by step creation of a binary search tree.
Root Root Root
Root
10 10 8 10 15 10 25
10
8 8 15 8 15
Root 25
22 10 30 Root
10
8 15 15
8
25
25
22 30
22
Contd.
Ex2 : Creating a binary search tree with the following values.
14 4 15 3 9 18 16 20 7 5 17
Root
14
4 15
3 9 18
7 16 20
5 17
Because of the recursive definition of trees, it is common
to write these routines recursively.
Operations on a Binary Search Tree
• Case 1: if there is no data in the tree (i.e. root is NULL). The node
pointed by InsNodePtr should be made the root node.
17 17
Cont.
• Case 2: if there is data
• Search the appropriate position.
• Insert the node in that position
InsertBST()
17 10
10
6 15 6 15
4 8 14 4 8 14 18
18
7 12 7 12 16 19
16 19
11 13 11 13 17
Implementation
void insert(int data) { //go to left of the tree
node *tempNode = new node; if(data < parent->data)
node *current; { current = current-
>leftChild;
node *parent; //insert to the left
tempNode->data = data; if(current == NULL) {
parent->leftChild = tempNode;
tempNode->leftChild = NULL;
return;
tempNode->rightChild = NULL; }
}//go to right of the tree
else {
//if tree is empty current = current->rightChild;
if(root == NULL) { //insert to the right
root = tempNode; if(current == NULL) {
parent->rightChild =
}else { tempNode;
current = root; return;
}
parent = NULL;
}
while(1) { }
parent = }
}
Traversing
• Binary search tree can be traversed in three ways.
• Preorder traversal - traversing binary tree in the
order of
parent, left and right.
• Inorder traversal- traversing binary tree in the order
of left, parent and right.
• Postorder traversal- traversing binary tree in the order of left,
right and parent.
•Algorithms for traversing binary search tree:-
void Preorder(node *t)
1. if (t != NULL)
• print t->data;
• if (t->leftChild != NULL) Preorder(t-
>leftChild);
• if (t->rightChild != NULL) Preorder(t-
>rightChild);
Contd.
void Inorder(node *t)
1. if (t != NULL)
• if (t->leftChild != NULL) Inorder(t->leftChild);
• print t->data;
• if (t->rightChild != NULL) Inorder(t->rightChild);
2. else print “Tree Empty”.
Void Postorder(node *t)
3. if (t != NULL)
• if (t->leftChild != NULL) Postorder(t->leftChild);
• if (t->rightChild != NULL) Postorder(t->rightChild);
• print t->data;
4. else print “Tree Empty”
Contd. root
Example:
10
6 15
4 8 14 18
7 12 16 19
11 13 17
Preorder traversal - 10, 6, 4, 8, 7, 15, 14, 12, 11, 13, 18, 16, 17, 19
Inorder traversal - 4, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19
==> Used to display nodes in ascending order.
Postorder traversal- 4, 7, 8, 6, 11, 13, 12, 14, 17, 16, 19, 18, 15, 10
Searching
• To search a node (whose data value is Number) in a binary search
tree (whose root node is pointed by root), one of the three traversal
methods can be used.
node* search(int data) {
node *current = root;
Function Call cout<<"Visiting elements: \n";
10
6 14
3 8 12 18
2 4 7 9 11 13 16 19
5 15 17
1
Deletion
Case 1: Deleting a leaf node (a node having no child)
rootptr
rootptr
Delete 7
10
10
6 14
6 14
3 8 12 18
3 8 12 18
4 9 11 16 19
16 2 13
2 4 7 9 11 13 19
15 17
17 1 5
5 15
1
Contd.
Case 2: Deleting a node having only one child
Approach 1: Deletion by merging – one of the following is done
• If the deleted node is the left child of its parent and the deleted node
has only the left child, the left child of the deleted node is made the
left child of the parent of the deleted node.
• If the deleted node is the left child of its parent and the deleted node
has only the right child, the right child of the deleted node is made
the left child of the parent of the deleted node.
• If the deleted node is the right child of its parent and the node to be
deleted has only the left child, the left child of the deleted node is
made the right child of the parent of the deleted node.
• If the deleted node is the right child of its parent and the deleted
node has only the right child, the right child of the deleted node is
made the right child of the parent of the deleted node.
Contd.
rootptr
rootptr
Delete 2
10
10
6 14
6 14
3 8 12 18
3 8 12 18
4 9 11 16 19
16 1 13
2 4 7 9 11 13 19
15 17
17 5
5 15
1
Contd.
Approach 2: Deletion by copying- the following is done
• Copy the node containing the largest element in the left (or the smallest
element in the right) to the node containing the element to be deleted
rootptr
rootptr
Delete 2 10
10
6 14
6 14
3 8 12 18
3 8 12 18
1 4 9 11 13 16 19
2 4 7 13 16 19
9 11
15 17
15 17 5
1 5
Contd.
Case 3: Deleting a node having two children
• Approach 1: Deletion by merging – one of the following
is
done
• If thedeleted node is the left child of its parent, one of
the following is done
• The left child of the deleted node is made the left child of the
parent of the deleted node, and
• The right child of the deleted node is made the right child of the
node containing largest element in the left of the deleted node
OR
• The right child of the deleted node is made the left child of the
parent of the deleted node, and
• The left child of the deleted node is made the left child of the
Contd.
• If the deleted node is the right child of its parent, one of
the following is done
• The left child of the deleted node is made the right child of the
parent of the deleted node, and
• The right child of the deleted node is made the right child of the
node containing largest element in the left of the deleted node
OR
• The right child of the deleted node is made the right child of the
parent of the deleted node, and
• The left child of the deleted node is made the left child of the
node containing smallest element in the right of the deleted node
Contd. rootptr rootptr
Delete 6
10 10
6 14 8 14
3 8 12 18 7 9 12 18
2 4 7 13 16 19 11 13 16 19
9 11 3
15 17 15 17
1 5 4
2
rootptr
rootptr
1 5
Delete 6 10
10
3 14
6 14
2 4 12 18
3 8 12 18
5 11 13 16 19
1
2 4 7 9 13 16 19
9 11
8 15 17
15 17
1 5
7 9
Contd.
• Approach 2: Deletion by copying- the following is done
• Copy the node containing the largest element in the left (or the
smallest element in the right) to the node containing the element to be
deleted
• Delete the copied node
rootptr rootptr
Delete 6
10 10
6 14 5 14
3 8 12 18 3 8 12 18
22 44 7 99 13 16 19 2 4 7 13 16 19
11 9 11
5 15
15 17 15 17
1 1
Contd. rootptr
10
6 Delete 6 14
3 8 12 18
2 4 7 9 11 13 16 19
15 17
1 5
rootptr
10
7 14
3 8 12 18
2 4 9 11 13 16 19
5 15 17
1
Contd.
Case 4: Deleting the root node, 10
Approach 1: Deletion by merging- one of the following is
done
If the tree has only one node the root node pointer is made to
point
to nothing (NULL)
If the root node has left child
the root node pointer is made to point to the left child
the right child of the root node is made the right child of the
node containing the largest element in the left of the root
node
If root node has right child
the root node pointer is made to point to the right child
the left child of the root node is made the left child of the node
containing the smallest element in the right of the root node
Contd.
rootptr rootptr
rootptr
10 Delete 10 6
6 14 3 8
3 8 12 18 2 4 7 9
2 4 7 9 11 13 16 19 5 14
1
5 15 17 12
1 18
11 13 16 19
15 17
rootptr rootptr
rootptr
10 14
Delete 10
6 14 12 18
3 8 12 18 11 16 19
13
2 4 7 9 11 13 16 19 6 15 17
5 15 17 3 8
1
2 4 7 9
1 5
Contd.
• Approach 2: Deletion by copying- the following is done
• Copy the node containing the largest element in the left (or the smallest
element in the right) to the node containing the element to be deleted
• Delete the copied node
rootptr
rootptr
Delete 10 9
10
6 14
6 14
3 8 12 18
3 8 12 18
2 4 7 11 13 16 19
2 4 7 13 16 19
9 11
15 17
15 17 1 5
1 5
Contd.
rootptr
rootptr
11
10 Delete 10
6 14
6 14
3 8 12 18
3 8 12 18
4 7 9 16 19
16 2 13
2 4 7 9 11 13 19
15 17
17 1 5
5 15
1
Implementation
void DeleteBST(node *root, node *PDNP, int x)
{
node *DNP; // a pointer that points to the currently deleted node
// PDNP is a pointer that points to the parent node of currently deleted node
if(root==NULL)
cout<<"Data not found\n";
else if (root->data>x)
DeleteBST(root->leftChild,
root, x); // delete the element in the
left subtree
else if(root->data<x)
DeleteBST(root->rightChild, root, x);// delete the element in the
right subtree else
{
DNP=root;
if((DNP->leftChild==NULL) && (DNP->rightChild==NULL))
{
if (PDNP->leftChild==DNP)
PDNP->leftChild=NULL;
else
PDNP->rightChild=NULL;
delete DNP;
Implementation
else //find the minimum in the right
else {
{ if(DNP->leftChild!=NULL)
//find the maximum in the left PDNP=DNP;
{
PDNP=DNP; DNP=DNP->rightChild;
DNP=DNP->leftChild;
while(DNP->rightChild!=NULL) while(DNP->leftChild!=NULL){
{ PDNP=DNP;
PDNP=DNP;
DNP=DNP- DNP=DNP->leftChild; }
>rightChild; root->data=DNP->data;
}
root->data=DNP->data; DeleteBST(DNP,PDNP,DNP-
DeleteBST(DNP,PDNP,DNP->data); >data);
} }
}
}
}
Application of Tree
• Expression evaluation
• Compiler design
• There are many applications for trees . One of the popular uses is
the directory structure in many common operating systems
including UNIX, DOS and Windows
Tree Full Implementation
EX.