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

CH 7

The document provides an overview of tree data structures, emphasizing their hierarchical nature and terminology such as root, parent, child, and leaf nodes. It discusses various types of trees, particularly binary trees and binary search trees, along with their properties, traversal methods, and operations like insertion and deletion. Additionally, it includes examples and code snippets for implementing these concepts in programming.

Uploaded by

betesfafirew05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

CH 7

The document provides an overview of tree data structures, emphasizing their hierarchical nature and terminology such as root, parent, child, and leaf nodes. It discusses various types of trees, particularly binary trees and binary search trees, along with their properties, traversal methods, and operations like insertion and deletion. Additionally, it includes examples and code snippets for implementing these concepts in programming.

Uploaded by

betesfafirew05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Data structure and algorithm

Department of
Software Engineering
2nd year
Data structure and
Algorithms
Presented by: Demeke M.
Data structure and algorithm

Chapter seven
Tree

Presented by: Demeke M.


CH 7: Tree
3
Concepts of trees
o In the case linked list, stack, Queue it is impossible to show hierarchical structure
on a collection of data items.
o Tree data structure is a hierarchical structure that is used to represent and organize
data in the form of parent child relationship. It is a way of representing hierarchical
data.
o Tree consist of nodes with a parent-child relationship.
o A tree is a set of nodes and edges that connect pairs of nodes. It is non-linear
abstract model of a hierarchical structure.
CH 7: Tree
4
Tree terminology
o Root: The topmost node in the tree (the starting point of the tree). And that has no
parent nodes.
o Parent: Any node except the root node has one edge upward to a node called
parent. A node that has one or more child nodes.
o Node: fundamental units or element of a tree, containing data.
o Edges: These are the connections between nodes, representing relationships. In any
tree with N nodes there will be maximum of N-1 edge.
o Child: A node that is a descendant of another node (the "parent").
o Subtree: A tree formed by a node and its descendants.
CH 7: Tree
5
Cont.….
o Path: A path through the tree to a node is a sequence of nodes along the edge of
the tree that connect the root to the node.
o Length: The length of a path is the number of arcs in the path.
o Level: The level of a node is equal to the length of the path from the root to the
node plus 1. [Level of a node represents the generation of a node. If the root node is
at level 0, then its next child node is at level 1, its grandchild is at level 2, and so
on.]
o Depth: Means number of ancestors / length of the path from the root to the node.
o Height of a tree: maximum distance from root to leaf [The number of edges on the
longest path from a node to a leaf]. The height of a tree is equal to the maximum
level of a node in the tree.
o Descendent nodes: child, child-of-child etc.
CH 7: Tree
6
Cont.….
o Internal node: which has at least one child Leaf node - which has no child
o Ancestor: nodes which are parent, grandparent, grand- grand-parent, etc.
o Neighbor of a node: Parent or child nodes of that node are called neighbors of that
node.
o Leaf node or external node: The nodes which do not have any child nodes are
called leaf nodes.
o Siblings: In a tree data structure, nodes which belong to same Parent are called
as SIBLINGS. In simple words, the nodes with the same parent are called Sibling
nodes
CH 7: Tree
7
Cont.….
• No. of nodes = 9
Example: • Height = 3
• Levels = 8 level 0, 3 and 10 level 1
• Parent = 8, 3, 6, 10, 14
• Root Node = 8
• Leaves = 1,4,7,13
• Internal nodes = 8, 3,10,6,14
• Ancestors of 6 = 3,8
• Descendants of 10 = 14,13
• Sibling: 1 and 6, 4 and 7
• Child: 3 and 10 is child of 8
• Degree: degree of 8 is 2
• Sub tree: 3, 1, 6, 4, 7 and 6, 4, 7
CH 7: Tree
8
Cont.….
Exercise: tree terminology for the following tree
CH 7: Tree
9
Type of 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
.

Properties of binary trees


• At each level of i, the maximum number of nodes is 2 i
• The height of the tree is defined as the longest path from the root node to the leaf
node. The tree which is shown above has a height equal to 3. Therefore, the
maximum number of nodes at height 3 is equal to (1+2+4+8) = 15. In general, the
maximum number of nodes possible at height h is (20 + 21 + 22 +….2h ) = 2h+1 -1.
• The minimum number of nodes possible at height h is equal to h+1.
• If the number of nodes is minimum, then the height of the tree would be
maximum. Conversely, if the number of nodes is maximum, then the height of the
tree would be minimum.
CH 7: Tree
17
Traversing binary trees
o Tree traversal algorithms are essential for visiting and processing all nodes in a tree data
structure. It is a way in which each node in the tree is visited exactly once in a systematic
manner.
o Binary tree can be traversed by three method
1. Depth-First Traversal
2. Breadth-First Traversal
Reading assignment
3. Stack-less Depth-First Traversal
CH 7: Tree
18
Cont.….
o Depth-first traversal can be traversed in three ways
• Pre order traversal - traversing binary tree in the order of parent, left and right (PLR).
• In order traversal - traversing binary tree in the order of left, parent and right (LPR).
• Post order traversal - traversing binary tree in the order of left, right and parent (LRP).
CH 7: Tree
19
Pre order traversal
o It is used to generate mathematical expression in prefix notation.
o Step
1. Visit the root node
2. Traverse the left sub tree in preorder
3. Traverse the right sub tree in preorder

Node -> left subtree -> right subtree


CH 7: Tree
20
Cont.….
❑ Example: POT
❑ Implementation sample
void Pre_order(node *root){
if(root == NULL)
return;
cout<<root->data<<" ";
Pre_order(root->left);
Pre_order(root->right);
}

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

left subtree -> Node-> right subtree


CH 7: Tree
22
Cont.….
❑ Example: IOT
❑ Implementation sample
void In_order(node *root){
if(root == NULL)
return;
In_order(root->left);
cout<<root->data<<" ";
In_order(root->right);
}

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

left subtree -> right subtree -> Node


CH 7: Tree
24
Cont.….
❑ Example: POT
❑ Implementation sample
void Post_order(node *root){
if(root == NULL)
return;
Post_order(root->left);
Post_order(root->right);
cout<<root->data<<" ";
}

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

You might also like