0% found this document useful (0 votes)
13 views30 pages

TREES

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)
13 views30 pages

TREES

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/ 30

Trees:

Basic terminology used with Tree,


Binary Trees, Binary Tree Representation: Array Representation and
Pointer(Linked List) Representation,
Binary Search Tree, Strictly Binary Tree ,Complete Binary Tree . A
Extended Binary Trees,
Tree Traversal algorithms: Inorder, Preorder and Postorder,
Constructing Binary Tree from given Tree Traversal, Operation of
Insertation , Deletion, Searching & Modification of data in Binary
Search.
Threaded Binary trees, Traversing Threaded Binary trees. Huffman
coding using Binary Tree.
Concept & Basic Operations for AVL Tree, B Tree &
Binary Heaps
Basic terminology
- A tree is a collection of elements called nodes.
- a tree is a hierarchical structure of the nodes.
- The first nodes is known as Root nodes and remaining nodes is known as sub
trees.
- The root of each subtree is
said to be a child of r and r is
Parent of each subtree root.
Basic Terms
1- Root:- it is a special node in a tree structure and the entire tree is
referenced through it. This node does not have a parent. In the tree of above
fig, the root is A.
2- Parent:- it is an immediate predecessor of a node. In fig, A is the parent
of B,C,D
3- Child:- All immediate successors of a node are children. In the figure
B,C and D are the children of A.
4- Siblings:- Nodes with the same parent are sibling. In the figure H,I are
the sibling as they are children of the same parent D.
5- Path:- It is the successive edge from source node to destination node.
The path length from C to node J is 2 and edge is C- G, G-J.
6- Degree of a node:- the degree of a node is a number of children of a
node. In above figure A has degree 3, B,D and G has degree 2.
7- Leaf node:- A node of degree 0 is also known as the leaf node.
Binary tree
A tree is a binary if each node of the tree can have maximum of two
children. Moreover, Children of a node of binary tree are ordered. One
child is called the “Left” child and the other is called “right “ child.
Ex- An example of binary tree is ---

In this figure , every nodes have a


degree of 0,1 and 2. it means every
node has 0 child, 1 child and 2 child.
Representation of a Binary tree
In order to represent a tree in a single one-dimensional array, the nodes are numbered sequentially level by left to right.

When the data of the tree is stored in an array

0 1 2 3 4 5 6 7 8

7 A B C D /0 E F
Location number zero of the array can be used to store the size of the tree of the total
number of nodes ( existing or not existing).
All non- existing nodes are represented by /0 in array.
Index of the left child of a node i=2*I 2n+1
Index of the right child of a node i=2*i+1 2n+2
Index of the parent node i=i/2 floor(n-1)/2

Sibling of a node I will be found at the location i + 1 , if I is a left child of its parents.
Similarly, if i is a right child of its parents then its sibling will be found at i - 1.
Linked list Representation of a Binary tree
Linked list representation of a binary tree is more efficient than array representation. A
node of a binary tree consists of three fields.
1- data
2 – Address of left child
3- Address of right child
Left and right are the pointer type fields

typedef struct node


{
Int data;
{
struct node *left;
struct node *right;
} node;
Height of a node:-
height of a node is the distance of the node from
its farthest descendant.
Nodes Heig
ht
A 3
C 2
B,F 1
D,E,H,G 0

Height / Depth of a Tree:- Height of a tree is


height of the root node.
Max and Min No of Node in a Binary tree
at level 0 The number of nodes is = 2i = 20 = 1 Type Max Min Node
node
at level 1 The number of nodes is = 2i = 21 = 2
Binary tree 2h+1-1 h+1
at level 2 The number of nodes is = 2i = 22 = 4
Full Binary 2h+1-1 2h+1
at level h The number of nodes is = 2i = 2h Tree
So total number of nodes of height h is Complete 2h+1-1 2h
n = 20 + 21 + 22 + 23+…………….. + 2h Binary Tree

n = 20 + 21 + 22 + 23+…………….. + 2h ===== n=2h+1-1

Min No of Nodes -- h+1


When h=0, n= 1
h=1, n= 2 …… and so on

Ex- Prove that the height of a binary tree is h= (log


2 (n+1)) -1
Types of binary tree:
1- Full Binary Tree/ proper binary tree/ Strictly binary tree:
A Binary Tree is a full binary tree if every node has 0 or 2 children. The following are
the examples of a full binary tree. We can also say a full binary tree is a binary tree in
which all nodes except leaf nodes have two children.
A full Binary tree is a special type of binary tree in which every parent node/internal
node has either two or no children. It is also known as a proper binary tree.
• Max no of nodes in FBT n=2h+1-1
• Min no. of nodes in FBT n = 2h+1
for h=0 Min no. of nodes in FBT is 1
h=1 Min no. of nodes in FBT is 2
h=2 Min no. of nodes in FBT is 5 and so on
Types of binary tree:
Complete Binary Tree:
A complete binary tree is said to be complete binary tree if each of its node has either two children or no children at all.
Number of nodes at any level i in a complete binary tree is given by 2i
A Binary Tree is a Complete Binary Tree if all the levels are completely filled except possibly the last level and the last
level has all keys as left as possible.
A complete binary tree is shown in figure.

Max No of Nodes in a CBT= == n= 2h+1-1


Min No of Nodes in a CBT==== n= 2h
Degenerate Binary Tree:
A binary tree is a degenerate binary tree in which each parent node/ internal node has only
one child node which is associated with it. It is also known as skewed binary tree.
It has two types
1- left skewed binary tree
2- Right skewed binary tree
Perfect Binary Tree: A perfect Binary Tree is a binary tree in which each of the internal
nodes has exactly two child nodes and all the leaf nodes are situated at the same level of the
tree.
At each level, it has no of nodes = 2i
Extended Binary Tree:-
Extended binary tree is a type of binary tree in which all the null subtrees of a given tree are
replaced with special nodes called the external nodes, where other nodes are known as internal
nodes.
The internal nodes are represented by circle
The external nodes are represented by square
Use :- it is used to represent in algebraic expression.
No of External Nodes= No of internal nods+1
Traversal of a Binary Tree:
There are three types of Traversal
1- Pre-order Traversal : Root, Left, Right-- RLR
2- In-order traversal : Left, Root, Right--- LRR
3- Post-order Traversal: Left, Right Root---LRR

Problem1- Pre-order : A B D E F C G H J L K
In-order : D B F E A G C L J H K
Problem 2- In-order : D B H I E A C F G
Post-order: D I H E B G F C A
Problem1- Pre-order : A B D E H I C F G
In-order : D B H I E A C F G Draw the tree?
C- Function of binary tree traversal
In order
Pre order Void Inorder(node *p)
{
if (tree!=null)
Void preorder(node *p)
{
{
Inorder(p🡪left);
if (tree!=null) printf(“%d \n”,p🡪 num);
{ Inorder(p🡪right);
printf(“%d \n”,p-🡪 num); }
preorder(p🡪left); }
preorder(p🡪right);
}
}
C- Function of binary tree traversal
Postorder
Void Postorder(node *p)
{
if (tree!=null)
{
Postorder(p---->left);
Postorder(p---->right);
printf(“%d \n”,p----> num);
}
}
Binary Search tree:- A binary search tree follows some order to arrange the elements. In a Binary search tree,
the value of left node must be smaller than the parent node, and the value of right node must be greater than
the parent node. This rule is applied recursively to the left and right subtrees of the root.

Advantages of Binary search tree


• Searching an element in the Binary search tree is
easy as we always have a hint that which subtree
has the desired element.
• As compared to array and linked lists, insertion and
deletion operations are faster in BST.
• Example of creating a binary search
tree
Suppose the data elements are - 45,
15, 79, 90, 10, 55, 12, 20, 50
C- Function of binary search tree
Calculate internal nodes
To Calculate total no of nodes-- int internal _nodes(BST *tree)
{
int total_nodes(BST, *tree) If( ( tree==null) II ((tree🡪left==null)&&(tree🡪right==null)))
{ return 0;
if (tree== null) Else
return 0; return
else ((internal_nodes(tree🡪left)+(total_nodes(tree🡪right) +1)
return }
((total_nodes(tree🡪left)+(total_nodes(tree🡪right)+1)
}
C- Function of binary search tree
To Calculate external no of nodes-- Calculate height
int determineheight (BST *tree)
int External_nodes(BST, *tree) { int leftheight, rightheight;
{ If( ( tree==null)
if (tree== null) return 0;
Else
return 0;
leftheight= determineheight (tree-🡪left);
elseif rightheight= determineheight (tree-🡪right);
((tree🡪left==null)&&(tree🡪right==null))) If(leftheight>rightheight)
return 1; return ++leftheight;
else else
Return ++ rightheight;
Return
}
((total_nodes(tree🡪left)+(total_nodes(tree🡪right)); }
}
C- Function of binary search tree
Mirror Image
Void findmirrorimage(BST, *tree)
{
BST *temp;
If (tree!=null)
{
findmirrorimage(tree🡪left);
findmirrorimage(tree🡪right);
temp= tree🡪left;
tree🡪left= tree🡪right;
tree🡪right=temp;
}
}
Application of trees:;
1- Expression Trees: when an expression is represented through a tree,
it is known as expression tree. The leaves of an expression tree are
operands, (such as constants or variables name) and all the internal
nodes contain operations.
Fig 1 gives an example of an expression tree.
(a+b*c)*e+f
Prefix= + * + a * b c e f
Postfix= a b c * + e * f +
Ex-2
((((a * x + b) * x + c ) * x + d ) * x + c ) * x + f

You might also like