CH 7
CH 7
Department of
Software Engineering
2nd year
Data structure and
Algorithms
Presented by: Demeke M.
Data structure and algorithm
Chapter seven
Tree
Reading assignment
CH 7: Tree
10
Binary tree
o A tree in which each node has at most two children called left child and right child.
o A node have only left and right child or
• Only left child or only right child.
• A leaf node has no left or right child. Root
• A leaf node has only NULL. Right-child of root
Left-child of root
Leaf
CH 7: Tree
11
Types of binary tree
o Strict binary tree / full binary tree
• Each node has exactly either two or zero child.
• Every non-leaf node has non-empty left and right sub trees.
CH 7: Tree
12
Cont.….
o Balanced binary tree o Complete binary tree
• a binary tree where each node except the • A binary tree in which the length from the
leaf nodes has left and right children and root to any leaf node is either h or h-1 where
all the leaves are at the same level. h is the height of the tree.
• The deepest level should also be filled from
left to right.
CH 7: Tree
13
Binary search trees
o Also called ordered binary tree.
o A binary search tree is either empty or in which every node contains a key
and satisfied the conditions:
• The key in the left child of a node (if it exist) is less than the key in its parent
node.
• The key in the right child of a node (if it exist) is greater than the key in its
parent node.
• The left and right sub-trees of the root are again binary search trees.
• No two entries in a binary search tree may have a equal keys.
CH 7: Tree
14
Cont.….
o In short: • Value of all the nodes in left sub-tree
• Value at node is lesser.
• Smaller values in left sub-tree • Value of all the nodes in right sub tree
• Larger values in right sub-tree is greater.
• Example
• X>Y
• X<Z
CH 7: Tree
15
Example
o Binary search trees o Not a binary search tree
10>25?, False
30>45?, False
CH 7: Tree
16
.
Output: + - A * B C + D / E F
CH 7: Tree
21
In order traversal
o It is used to generate mathematical expression in infix notation[Ascending order].
o Steps
1. Traverse the left sub tree in-order
2. Visit the root node /print root value
3. Traverse the right sub tree in-order
Output: A – B * C + D + E /F
CH 7: Tree
23
Post order traversal
o It is used to generate mathematical expression in postfix notation.
o Steps
1. Traverse the left sub tree in post order
2. Traverse the right sub tree in post order
3. Visit the root node / print root value
Output: A B C * - D E F / + +
CH 7: Tree
25
Cont.….
o We can construct a binary tree from given two traversal. Example
▪ Construct a binary tree whose traversal are given below
• In order: D B F E A G C L J H K
• Pre order: A B D E F C G H J L K
CH 7: Tree
26
Search
o To search a node (whose Num value is X) in a binary search tree (whose root node is
pointed by RootNodePtr). One of the three traversal methods can be used.
o Return type of the SearchBST function can be
• Pointer that points to the node containing or
• Simple Boolean type True/False RootNodePtr
CH 7: Tree
27
Cont.….
❑ Implementation of search sample code
int search_BST(node *root, int key){
if(root == NULL){
return 0;
}
else if(root->data == key){
return 1;
}
else if(key <= root->data){
return search_BST(root->left, key);
}
else{
return search_BST(root->right, key);
}}
CH 7: Tree
28
Binary tree implementation
o Tree is a data structure similar to linked list. Instead of pointing to one node each
node can point to a number of nodes.
o Binary tree can be implemented by
• Linked list
• Array
o Binary tree operation
▪ The following operations can be implemented efficiently using a binary search
tree
• Insertion – add a new node in a tree
• Searching – find the node with a given value
• Display – display value(by traversing method)
• Deletion – remove a node from the tree
CH 7: Tree
29
Define a binary search tree
o Before implementing binary tree first define node class. Each node in binary
tree will contain three attributes. Those are
• Data –The value stored in the node.
• Left – Pointer to the left child.
• Right - Pointer to the right child.
o Data structure of binary tree
Struct node{
int num;
node *left, *right;
};
CH 7: Tree
30
Insertion
o When a node is inserted the definition of binary search tree should be
preserved.
o Suppose there is a binary search tree whose root node is pointed by
RootNodePtr and we want to insert a node (that stores 17) pointed by InsNode.
❑ Case 1: There is no data in the tree (RootNodePtr is NULL)
• Then new node is made the root node.
CH 7: Tree
31
Cont.….
❑ Case 2: If there is data in the tree (RootNodePtr is not NULL)
• Search the appropriate position
• Insert the node in that position.
CH 7: Tree
32
Algorithmic implementation sample of code
node* insert_BST(node *root, int data){ h
if(root == NULL){
root = New_node(data);
return root;
}
else if(data <= root->data){
root->left = insert_BST(root->left, data);
}
else {
root->right = insert_BST(root->right, data);
}
return root;
}
CH 7: Tree
33
Deletion
o To delete a node (whose num value is N) from binary search tree (whose root node
is pointed by RNP). There are four cases should be considered.
• Deleting a root node.
• Deleting a leaf node.
• Deleting a node having only left child.
• Deleting a node having only right child.
• Deleting a node having both left and right child nodes.
CH 7: Tree
34
Cont.….
❑ Case 1: Deleting a leaf node (a node having no child), e.g. 7
CH 7: Tree
35
Cont.….
❑ Case 2: Deleting a node having only one child, e.g. 2
o Approach 1: Deletion by merging
CH 7: Tree
36
Cont.….
o Approach 2: Deletion by copying
• 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 and
• Delete the copied node
CH 7: Tree
37
Cont.….
❑Case 3: Deleting a node having two children, e.g. 6
o Approach 1: Deletion by merging
▪ If the deleted 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 node containing smallest
element in the right of the deleted node
CH 7: Tree
38
Cont.….
o 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
CH 7: Tree
39
Cont.….
CH 7: Tree
40
Cont.….
CH 7: Tree
41
Cont.….
o 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 and
• Delete the copied node
CH 7: Tree
42
Cont.….
CH 7: Tree
43
Cont.….
❑ Case 4: Deleting the root node, 10
o Approach 1: Deletion by merging
▪ 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
CH 7: Tree
44
Cont.….
CH 7: Tree
45
Cont.….
CH 7: Tree
46
Cont.….
o Approach 2: Deletion by copying-
• 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 and
• Delete the copied node
CH 7: Tree
47
Cont.….
CH 7: Tree
Sample of code for deletion 48
delete temp;
node* delete_BST(node *root, int del){
return root;
if(root == NULL)
}
return NULL;
if(del < root->data){ else if(root->right == NULL){
root->left = delete_BST(root->left, del); node *temp = root;
} root = root->left;
else if(del > root->data){ delete temp;
root->right = delete_BST(root->right, del); return root;
}
}
else{
//case 3: 2 children
//case 1: no child
if(root->left == NULL && root->right == NULL){ else{
delete root; node *temp = find_min(root->right);
//root = NULL; root->data = temp->data;
return NULL; root->right = delete_BST(root->right, temp-
} >data);
//case 2: one child }
else if(root->left == NULL){
}
node *temp = root;
return root;
root = root->right;
}
CH 7: Tree
49
Reading assignment
Balancing a tree
Heap concept
Polish notation and expression trees
Graph related concept
CH 7: Tree
50
Application of tree
❑ The following are the application of tree
o To store natural hierarchical data like file system
o For dictionary
o Network routing algorithm like DNS
o Machine learning algorithm
o Organize data for searching, insertion and deletion
Thank You