Unit-III Tree
Unit-III Tree
1
So far we discussed Linear data structures like
stack
2
Introduction to trees
• So far we have discussed mainly linear data structures – arrays, lists, stacks
and queues.
such that
• The remaining nodes are partitioned into n>=0 disjoint set T1,…,Tn, where
each of these sets is a tree. T1,…,Tn are called the subtrees of the root.
20
Type of Trees
• General tree
• Binary tree
• Binary Search Tree
• AVL Tree
• B Tree
• B+ Tree
• Etc…….
General Tree
• A general tree is a data structure in that each node can have infinite number of
children .
• In general tree, root has in-degree 0 and maximum out-degree n.
• Height of a general tree is the length of longest path from root to the leaf of tree.
Binary tree
• A Binary tree is a data structure in that each node has at most two
nodes left and right
• In binary tree, root has in-degree 0 and maximum out-degree 2.
• In binary tree, each node have in-degree one and maximum out-
degree 2.
Binary Tree Properties
• The maximum number of nodes at level ‘l’ of a binary tree is 2l
• The Maximum number of nodes in a binary tree of height ‘h’ is 2h – 1.
(if root is at height 1)
• The Maximum number of nodes in a binary tree of height ‘h’ is 2h+1 – 1.
(if root is at height 0)
• In a Binary Tree with N nodes, minimum possible height
or the minimum number of levels is Log2(N+1).
• A Binary Tree with L leaves has at least | Log2L |+ 1 levels.
• In Binary tree where every node has 0 or 2 children, the number of leaf
nodes is always one more than nodes with two children.
• In a non empty binary tree, if n is the total number of nodes and e is
the total number of edges, then e = n-1
24
Representation of Binary Tree
1. Array Representation
2. Linked List Representation.
Array Representation
26
[1] A
Array Representation [2] B
[3] C
A [4] D
A [1] [5] E
[2] B
[3] -- [6] F
B C [7] G
[4]
-- A [8] H
[5]
C -- [9] I
[6]
[7] --
D B C
D [8]
[9] --
. .
E D E F G
[16] E
H I
Linked Representation
Struct node
{
int data;
struct node * left;
struct node *right;
};
Binary Tree Types
A binary tree is said to be Skewed Binary Tree if every node in the tree contains either
only left or only right sub tree. If the node contains only left sub tree then it is called left-
skewed binary tree and if the tree contains only right sub tree then it is called right-
skewed binary tree.
Strictly Binary Tree
• Expression trees are a special kind of binary tree used to evaluate certain expressions.
• Two common types of expressions that a binary expression tree can represent are
algebraic and boolean.
• These trees can represent expressions that contain both unary and binary operators.
• The leaves of a binary expression tree are operands, such as constants or variable names,
and the other nodes contain operators.
• Expression tree are used in most compilers.
Extended Binary Tree
Extended binary tree is a type of binary tree in which all the null
sub tree of the original tree are replaced with special nodes called
external nodes whereas other nodes are called internal nodes
Operations on Binary Tree
37
Tree traversal
• Traversal is a process to visit all the nodes of a tree and may print their
values too.
• All nodes are connected via edges (links) we always start from the root
(head) node.
• Generally we traverse a tree to search or locate given item or key in the tree or
to print all the values it contains.
38
Pre-order, In-order, Post-order
• Pre-order
<root><left><right>
• In-order
<left><root><right>
• Post-order
<left><right><root>
39
Pre-order Traversal
The preorder traversal of a non empty binary tree is defined as follows:
• Visit the root node
• Traverse the left sub-tree in preorder
• Traverse the right sub-tree in preorder
40
Pre-order Pseudocode
struct Node{
char data;
Node *left;
Node *right;
}
void Preorder(Node *root)
{
if (root==NULL) return;
printf (“%c”, root->data);
Preorder(root->left);
Preorder(root->right);
}
41
In-order traversal
• The in-order traversal of a nonempty binary tree is defined asfollows:
• Traverse the left sub-tree in in-order
• Visit the root node
• Traverse the right sub-tree in inorder
42
In-order Pseudocode
struct Node{
char data;
Node *left;
Node *right;
}
void Inorder(Node *root)
{
if (root==NULL) return;
Inorder(root->left);
printf (“%c”, root->data);
Inorder(root->right);
}
43
Post-order traversal
• The post-order traversal of a nonempty binary tree is defined
asfollows:
• Traversethe left sub-tree in post-order
• Traverse the right sub-tree in post-order
• Visit the root node
44
Post-order Pseudocode
struct Node{
char data;
Node *left;
Node *right;
}
void Postorder(Node *root)
{
if (root==NULL) return;
Postorder(root->left);
Postorder(root->right);
printf (“%c”, root->data);
}
45
Level-order Traversal
20
1
8 22
23
4 12
45
10 14
46
Level-order Traversal using Queue