0% found this document useful (0 votes)
7 views

CH9 - Tree

This document discusses trees and binary search trees. It defines key tree terminology like root, internal nodes, leaf nodes, ancestors, descendants, depth, height, and subtrees. It then describes properties of binary trees like full/strict binary trees and balanced/perfect binary trees. Binary search trees are defined as binary trees where all left descendants of a node are less than the node's value and all right descendants are greater. Operations on binary search trees like insertion, traversal, searching and deletion are outlined with pseudocode for insertion and traversal provided.

Uploaded by

Abdi Negassa
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

CH9 - Tree

This document discusses trees and binary search trees. It defines key tree terminology like root, internal nodes, leaf nodes, ancestors, descendants, depth, height, and subtrees. It then describes properties of binary trees like full/strict binary trees and balanced/perfect binary trees. Binary search trees are defined as binary trees where all left descendants of a node are less than the node's value and all right descendants are greater. Operations on binary search trees like insertion, traversal, searching and deletion are outlined with pseudocode for insertion and traversal provided.

Uploaded by

Abdi Negassa
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Data Structure and

Algorithm
Chapter Nine
Trees
Introduction

• A tree is a set of nodes and edges that connect pairs of nodes. It is


an abstract model of a hierarchical structure.

• Rooted tree has the following structure:

• One node distinguished as root.

• There is a unique path from the root to the each node.

• Every node C except the root is connected from exactly other


node P. P is C's parent, and C is one of P's children.
• The number of edges in a path is the length of the path.
Tree Terminologies
Consider the following tree.
• Root: a node without a parent. A A
• Internal node: a node with at least
one child. A, B, F, I, J B E F G
• External (leaf) node: a node without
a child.  C, D, E, H, K, L, M, G C D H I J
• Ancestors of a node: parent, grandparent,
great-grandparent, etc of a node. K L M
Ancestors of K  A, F, I
• Descendants of a node: children,
grandchildren, great-grandchildren etc of a
node.
Descendants of F  H, I, J, K, L, M
Contd.
• Depth of a node: number of ancestors or length of the path from
the
root to the node. Depth of H  2

• Height of a tree: depth of the deepest node. 3

• Sub tree: a tree consisting of a node and its descendants.


F
H I J

K L M

• Degree: The degree of a node is the number of children it has.

• 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.

• Binary tree is either empty, or it consists of a node called the root


together with two binary trees called the left sub-tree and the right
sub-tree.

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.

• Balanced/Perfect binary tree: a binary tree where each node except


the leaf nodes has left and right children and all the leaves are at the
same level.

• 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

• Binary tree can also be represented in two ways.

1. Array representation
85
2. Linked List representation
80 90
Array representation:-
70 82 87 100
• Consider the following binary tree

• Array representation of the above binary tree is

85 80 70 82 90 87 100
Contd.
Linked List Representation:-

• In linked list representation each element is represented by a node


that has exactly two link fields called one is left pointer (leftChild)
and the other is right pointer (rightChild) and data field called data.
Declaration:-
Struct Node
{
int data;
Node*rightChild;
Node*leftChild;
};
Node*root=NUL
L;

• Each edge in the drawing of a binary tree is represented by a pointer


from the parent node to child node. This pointer is placed in
appropriate link field of the parent node. Since an n element binary
Binary search tree (ordered binary tree)
• A binary tree that may be empty, but if it is not empty, it satisfies the
following.
• Every node has a key and no two elements have the same key.

• 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

Consider the following definition of binary search tree.


struct node
{
int data;
node * leftChild, *rightChild;
};
node *root=NULL;
The following are the operations on a Binary search tree.
• Inserting a new node
• Traversing all elements
• Searching an element
• Deleting a node
Insertion of a new node

• When a node is inserted the definition of binary search tree should be


preserved.
• Suppose there is a binary search tree whose root node is pointed by
root and we want to insert a node (that stores 17) pointed by
InsNodePtr.
Cont.

• 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.

InsNodePtr root root


17 17
Cont.
• Case 2: if there is data
• Search the appropriate position.
• Insert the node in that position

InsNodePtr root root

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";

int j; while(current->data != data)


cout<<"insert the element to { if(current != NULL)
be searched"<<endl; cout<<current->data<<" ";
cin>>j; if(current->data > data) {//go to left
struct node * temp = tree current = current->leftChild;
search(j); if(temp != NULL) }
{ else {//else go to right tree
cout<<" Element found."<< current = current->rightChild;
temp->data<<endl; }
} //not found
else { if(current == NULL) {
cout<<" Element not found\ return NULL;
n"<<j<<endl; }}
} return current;
When we search an element in a
Deletion
• To delete a node (whose data value is N) from binary search tree
(whose root node is pointed by root), four cases should be
considered.
• When a node is deleted the definition of binary search tree should be
preserved. Consider the following binary search tree
root

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

• Delete the copied node

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

Tree has many applications.


• File system maintenance

• Expression evaluation

• Compiler design

• To implement the shortest path problems. Etc,.

• 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.

• Write a single C++ program that implements all the operations


on a tree.

Tree FIM 1.cpp

You might also like