Skip to main content
TREE DATA STRUCTURE
UNIT - I
P R E P A R E D B Y – S A Y A N R O Y
A S S T . P R O F E S S O R , D E P T O F I T
INTRODUCTION TO TREE
DATA STRUCTURE
LINEAR NON LINEAR
(arrays, linked list, stack,
queue)
(Tree, Graph)
0 1 2 3 4
A 10 20 30 40 50
100 200 300
200 300
10 20 30
head
null
0 1 2 3 4
20 30 40
front rear 10
20
30
top
A
B C D
E F G H
I J K L
Tree can be defined as a collection of entities
(nodes) linked together to simulate a hierarchy.
INTRODUCTION TO TREE
DATA STRUCTURE
LINEAR NON LINEAR
(arrays, linked list, stack,
queue)
(Tree, Graph)
0 1 2 3 4
A 10 20 30 40 50
100 200 300
200 300
10 20 30
head
null
0 1 2 3 4
20 30 40
front rear 10
20
30
top
A
B C D
E F G H
I J K L
Tree can be defined as a collection of entities
(nodes) linked together to simulate a hierarchy.
TREE TERMINOLOGY
A
B C D
E F G H
I J K L
Root Node Node which is not having any parent
Nodes It stores some information & also
containing link to another node.
Parent Node Immediate predecessor of any node.
Child Node Immediate successor of any node.
Leaf Node The Node which is not having any
child.
TREE TERMINOLOGY
Root Node Node which is not having any parent
Nodes It stores some information & also
containing link to another node.
Parent Node Immediate predecessor of any node.
Child Node Immediate successor of any node.
Leaf Node The Node which is not having any
child.
Non leaf Node The Node which is having at least one
child.
A
B C D
E F G H
I J K L
TREE TERMINOLOGY
A
B C D
E F G H
I J K L
Root Node Node which is not having any parent
Nodes It stores some information & also
containing link to another node.
Parent Node Immediate predecessor of any node.
Child Node Immediate successor of any node.
Leaf Node The Node which is not having any
child.
Non leaf Node The Node which is having at least one
child.
path Sequence of consecutive edge from
source node to destination node
ancestor Any predecessor node on the path from
root to that node.
TREE TERMINOLOGY
A
B C D
E F G H
I J K L
Root Node Node which is not having any parent
Nodes It stores some information & also
containing link to another node.
Parent Node Immediate predecessor of any node.
Child Node Immediate successor of any node.
Leaf Node The Node which is not having any
child.
Non leaf Node The Node which is having at least one
child.
path Sequence of consecutive edge from
source node to destination node
ancestor Any predecessor node on the path from
root to that node.
decendant Any successor node on the path from
that node to leaf node.
TREE TERMINOLOGY
A
B C D
E F G H
I J K L
Root Node Node which is not having any parent
Nodes It stores some information & also
containing link to another node.
Parent Node Immediate predecessor of any node.
Child Node Immediate successor of any node.
Leaf Node The Node which is not having any
child.
Non leaf Node The Node which is having at least one
child.
path Sequence of consecutive edge from
source node to destination node
ancestor Any predecessor node on the path from
root to that node.
decendant Any successor node on the path from
that node to leaf node.
TREE TERMINOLOGY
Root Node Node which is not having any parent
Nodes It stores some information & also
containing link to another node.
Parent Node Immediate predecessor of any node.
Child Node Immediate successor of any node.
Leaf Node The Node which is not having any
child.
Non leaf Node The Node which is having at least one
child.
path Sequence of consecutive edge from
source node to destination node
ancestor Any predecessor node on the path from
root to that node.
decendant Any successor node on the path from
that node to leaf node.
A
B C D
E F G H
I J K L
TREE TERMINOLOGY
A
B C D
E F G H
I J K L
SUBTREE A node with all its decendent is known
as subtree.
SIBLING
DEGREE
All the child node have same parent.
Number of children of that node.
Degree of tree : Max degree among all nodes.
DEPTH OF NODE Length of path from root to that
node.
HEIGHT OF NODE No. of edges in the longest path
from that node to a leaf.
HEIGHT OF TREE = HEIGHT OF THE ROOT NODE
TREE TERMINOLOGY
A
B C D
E F G H
I J K L
LEVEL OF NODE Length of path from root to that
node.
LEVEL OF NODE = DEPTH OF NODE
1
2
3
0
LEVEL OF TREE = HEIGHT OF TREE
GENERAL TREES
A tree where a node can have any number of
children.
CEO
HR IT FINANCE
APPLICATIONS
Organization
hierarchy
XML
DOCUMENTS
FOLDER
STRUCTURES
BINARY TREE & IT’S TYPES
 A tree where each node has at most two
children.
✅
✅
✅
WHY BINARY TREES ?
 Easy implementation
 Faster searching
 Basis for BST, AVL, Heap
PROPERTIES OF BINARY TREE
 HOW MANY MAX NO OF NODES
POSSIBLE AT ANY LEVEL ?
0
1
2
1
2
4
22
21
20
MAX NO OF NODES POSSIBLE AT
ANY LEVEL i =
 MAX NO OF NODES OF HEIGHT h
2h +1
− 1
22
21
20
+¿ +¿ +¿ ………..+¿2h
 MIN NO OF NODES OF HEIGHT h
h+1
0
1
2
1
2
4
22
21
20
 MAX NO OF NODES OF HEIGHT h
2h +1
− 1
22
21
20
+¿ +¿ +¿ ………..+¿2h
𝑛=2h +1
−1
𝑛+1=2h +1
log2 (𝑛+ 1)=log2 (2h +1
)
log 2 (𝑛+ 1)=h +1
h=log2 (𝑛+ 1)− 1
⌈ ⌉
Minimum Height
Maximum Height
𝑛=h+1
h=𝑛−1
PROPERTIES OF BINARY TREE
BINARY TREE & IT’S TYPES
FULL / PROPER / STRICT BINARY
TREE
Every node has either:
 0 children
 2 children
A
B C
D E
MAX NODES MIN NODES
MIN HEIGHT MAX HEIGHT
BINARY TREE 2h +1
− 1 h+1
BINARY TREE 𝑛−1
FULL TREE 2h +1
− 1 2 h+1
log2 (𝑛+ 1)−1
⌈ ⌉
FULL TREE (𝑛−1)/2
log2 (𝑛+ 1)−1
⌈ ⌉
NO OF LEAF NODE = NO OF
INTERNAL NODE +1
BINARY TREE & IT’S TYPES
COMPLETE BINARY TREE MAX NODES MIN NODES
MIN HEIGHT MAX HEIGHT
BINARY TREE 2h +1
− 1 h+1
BINARY TREE 𝑛−1
FULL TREE 2h +1
− 1 2 h+1
log2 (𝑛+ 1)−1
⌈ ⌉
FULL TREE (𝑛−1)/2
log2 (𝑛+ 1)−1
⌈ ⌉
All levels filled except possibly last
level.
A
B C
D E F
COMPLETE BT 2h +1
− 1
COMPLETE BT
2h
log2 (𝑛+ 1)−1
⌈ ⌉ log 𝑛
BINARY TREE & IT’S TYPES
PERFECT BINARY TREE
All internal nodes have two children and all
leaves are at same level.
A
B C
D E F G
BINARY TREE & IT’S TYPES
SKEWED BINARY TREE
All nodes on one side.
A
B
C
D
BINARY TREE ADT
A Binary Tree is an ADT that stores data
in a hierarchical structure where every
node has at most two children.
The ADT defines the operations that can
be performed on the tree.
Operations of Binary Tree ADT
1. Create Tree
Creates an empty binary tree.
createTree()
2. Insert Node
Adds a new node into the tree.
insert()
BINARY TREE ADT
3. Search
Find whether an element exists.
Search()
4. Traversal
Visit every node exactly once.
Traversal()
5. Delete Node
Removes a node from the tree.
Delete()
ADT VS IMPLEMENTATION
Binary Tree ADT (What)
Insert()
Delete()
Search()
Traversal()
These define what operations are available.
Implementation (How)
There are different ways to implement
those operations.
 Method 1: Array Representation
 Method 2: Linked Representation
ARRAY BASED REPRESENTATION OF
BINARY TREE
A
B C
D E
F G
0 1 2 3 4 5 6 7 8
H I
A B C D E F G H I
If a node is at index :
Left child would be at location
Right child would be at location
Parent would be at ⌋
⌊
ARRAY BASED REPRESENTATION OF
BINARY TREE
A
B C
D E
F G
0 1 2 3 4 5 6 7 8
H I
9 10
BINARY TREE
SHOULD BE CBT
ALL LEVELS ARE FILLED
EXCEPT POSSIBLY LAST
LEVEL
IN LAST LEVEL, NODE
SHOULD BE AS LEFT AS
POSSIBLE
- -
A B C D E F G - - H I
ARRAY BASED REPRESENTATION OF
BINARY TREE
A
B C
D E
F G
0 1 2 3 4 5 6 7 8
H I
9 10
- -
A B C D E F G - - H I
Advantages
 Simple implementation
 Fast access O(1)
 No pointer/reference overhead
Disadvantages
 Memory wastage for sparse trees
 Difficult insertion/deletion
 Fixed size
LINKED BASED REPRESENTATION OF
BINARY TREE 10
20 30
class Node {
int data;
Node left;
Node right;
Node(int data) {
this.data = data;
this.left = null;
this.right = null;
}
}
public static void main(String[] args) {
Node root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
}
public class Binarytree1 {
}
BINARY TREE TRAVERSAL
 Tree traversal refers to the process of
visiting or accessing each node of a tree
exactly once in a specific order.
 Unlike linear data structures such as arrays,
linked lists, or queues (which have only one
logical way of traversal), trees offer multiple
ways to traverse their nodes.
TREE TRAVERSAL
TECHNIQUE
DEPTH FIRST
TRAVERSAL
BREADTH
FIRST
TRAVERSAL
INORDER
TRAVERSAL
PREORDER
TRAVERSAL
POSTORDER
TRAVERSAL
Inorder traversal visits the node in the order:
Left -> Root -> Right
INORDER TRAVERSAL
1
2 3
4 5
7
4, 2, 5, 1, 6, 3, 7
6
Preorder traversal visits the node in the order:
Root -> Left -> Right
PREORDER TRAVERSAL
1
2 3
4 5
7
1, 2, 4, 5, 3, 6, 7
6
Postorder traversal visits the node in the order:
Left -> Right -> Root
POSTORDER TRAVERSAL
1
2 3
4 5
7
4, 5, 2, 6, 7, 3, 1
6
BREADTH-FIRST TRAVERSAL
1
2 3
4 5
7
1, 2, 3, 4, 5, 6, 7
6
 Explores nodes level by level from
top to bottom.
 Type: Level Order Traversal
CONSTRUCT A BINARY TREE FROM
PREORDER AND INORDER
PREORDER – 1, 2, 4, 8, 9, 10, 11, 5, 3, 6, 7
INORDER – 8, 4, 10, 9, 11, 2, 5, 1, 6, 3, 7
(ROOT – L – R)
(L – ROOT – R)
1
8, 4, 10, 9, 11, 2, 5 6, 3, 7
2
CONSTRUCT A BINARY TREE FROM
PREORDER AND INORDER
PREORDER – 1, 2, 4, 8, 9, 10, 11, 5, 3, 6, 7
INORDER – 8, 4, 10, 9, 11, 2, 5, 1, 6, 3, 7
(ROOT – L – R)
(L – ROOT – R)
1
8, 4, 10, 9, 11, 2, 5 6, 3, 7
2
5
8, 4, 10, 9, 11
5
4
CONSTRUCT A BINARY TREE FROM
PREORDER AND INORDER
PREORDER – 1, 2, 4, 8, 9, 10, 11, 5, 3, 6, 7
INORDER – 8, 4, 10, 9, 11, 2, 5, 1, 6, 3, 7
(ROOT – L – R)
(L – ROOT – R)
1
8, 4, 10, 9, 11, 2, 5 6, 3, 7
2
5
8, 4, 10, 9, 11
5
4
8 10, 9, 11
8 9
CONSTRUCT A BINARY TREE FROM
PREORDER AND INORDER
PREORDER – 1, 2, 4, 8, 9, 10, 11, 5, 3, 6, 7
INORDER – 8, 4, 10, 9, 11, 2, 5, 1, 6, 3, 7
(ROOT – L – R)
(L – ROOT – R)
1
8, 4, 10, 9, 11, 2, 5 6, 3, 7
2
5
8, 4, 10, 9, 11
5
4
8 10, 9, 11
8 9
10 11
10 11
3
CONSTRUCT A BINARY TREE FROM
PREORDER AND INORDER
PREORDER – 1, 2, 4, 8, 9, 10, 11, 5, 3, 6, 7
INORDER – 8, 4, 10, 9, 11, 2, 5, 1, 6, 3, 7
(ROOT – L – R)
(L – ROOT – R)
1
8, 4, 10, 9, 11, 2, 5 6, 3, 7
2
5
8, 4, 10, 9, 11
5
4
8 10, 9, 11
8 9
10 11
10 11
3
6 7
6
7
CONSTRUCT A BINARY TREE FROM
POSTORDER AND INORDER
POSTORDER – 9, 1, 2, 12, 7, 5, 3, 11, 4, 8
INORDER – 9, 5, 1, 7, 2, 12, 8, 4, 3, 11
(L – R-ROOT)
(L – ROOT – R)
8
9, 5, 1, 7, 2, 12 4, 3, 11
5
CONSTRUCT A BINARY TREE FROM
POSTORDER AND INORDER
POSTORDER – 9, 1, 2, 12, 7, 5, 3, 11, 4, 8
INORDER – 9, 5, 1, 7, 2, 12, 8, 4, 3, 11
(L – R-ROOT)
(L – ROOT – R)
8
9, 5, 1, 7, 2, 12 4, 3, 11
5
9 1, 7, 2, 12
9 7
CONSTRUCT A BINARY TREE FROM
POSTORDER AND INORDER
POSTORDER – 9, 1, 2, 12, 7, 5, 3, 11, 4, 8
INORDER – 9, 5, 1, 7, 2, 12, 8, 4, 3, 11
(L – R-ROOT)
(L – ROOT – R)
8
9, 5, 1, 7, 2, 12 4, 3, 11
5
9 1, 7, 2, 12
9 7
1 2, 12
1 12
CONSTRUCT A BINARY TREE FROM
POSTORDER AND INORDER
POSTORDER – 9, 1, 2, 12, 7, 5, 3, 11, 4, 8
INORDER – 9, 5, 1, 7, 2, 12, 8, 4, 3, 11
(L – R-ROOT)
(L – ROOT – R)
8
9, 5, 1, 7, 2, 12 4, 3, 11
5
9 1, 7, 2, 12
9 7
1 2, 12
1 12
2
2
CONSTRUCT A BINARY TREE FROM
POSTORDER AND INORDER
POSTORDER – 9, 1, 2, 12, 7, 5, 3, 11, 4, 8
INORDER – 9, 5, 1, 7, 2, 12, 8, 4, 3, 11
(L – R-ROOT)
(L – ROOT – R)
8
9, 5, 1, 7, 2, 12 4, 3, 11
5
9 1, 7, 2, 12
9 7
1 2, 12
1 12
2
2
4
CONSTRUCT A BINARY TREE FROM
POSTORDER AND INORDER
POSTORDER – 9, 1, 2, 12, 7, 5, 3, 11, 4, 8
INORDER – 9, 5, 1, 7, 2, 12, 8, 4, 3, 11
(L – R-ROOT)
(L – ROOT – R)
8
9, 5, 1, 7, 2, 12 4, 3, 11
5
9 1, 7, 2, 12
9 7
1 2, 12
1 12
2
2
4
3, 11
11
CONSTRUCT A BINARY TREE FROM
POSTORDER AND INORDER
POSTORDER – 9, 1, 2, 12, 7, 5, 3, 11, 4, 8
INORDER – 9, 5, 1, 7, 2, 12, 8, 4, 3, 11
(L – R-ROOT)
(L – ROOT – R)
8
9, 5, 1, 7, 2, 12 4, 3, 11
5
9 1, 7, 2, 12
9 7
1 2, 12
1 12
2
2
4
3, 11
11
3
3
CONSTRUCT A BINARY TREE FROM
PREORDER AND POSTORDER
PREORDER – F, B, A, D, C, E, G, I, H
POSTORDER – A, C, E, D, B, H, I, G, F (L – R-ROOT)
(ROOT – L - R)
F
A, C, E, D, B
H, I, G
PREORDER : B, A, D, C, E
POSTORDER : A, C, E, D, B
B
A
A C, E, D POSTORDER : C, E, D
PREORDER : D, C, E
D
C
C E
E
POSTORDER : H, I, G
PREORDER : G, I, H
G
H, I
POSTORDER : H, I
PREORDER : I, H
I
H
H
RECURSIVE TRAVERSAL
A
B C
D E
How Inorder Recursion Works
static void inOrder(Node root) {
//base case
if(root == null) {
return;
}
inOrder(root.left);
System.out.print(root.data + " ");
inOrder(root.right);
}
NON-RECURSIVE TRAVERSAL
In non-recursive traversal, we create and manage our own stack
explicitly.
A
B C
D E
Stack<Node> stack = new Stack<>();
Go left as much as possible Push every node When left /right
becomes null
Pop
Visit
Go right
Again go left
PROCEDURE
NON RECURSIVE TRAVERSAL
A
B C
D E
A
Go left as much as possible Push every node When left /right
becomes null
Pop
Visit
Go right
Again go left
INORDER TRAVERSAL
CURRENT -> A
A
B C
D E
A
B
Go left as much as possible Push every node When left /right
becomes null
Pop
Visit
Go right
Again go left
NON RECURSIVE TRAVERSAL
INORDER TRAVERSAL
CURRENT -> B
A
B C
D E
A
B
D
Go left as much as possible Push every node When left /right
becomes null
Pop
Visit
Go right
Again go left
NON RECURSIVE TRAVERSAL
INORDER TRAVERSAL
CURRENT -> D
A
B C
D E
D
A
B
D
Go left as much as possible Push every node When left /right
becomes null
Pop
Visit
Go right
Again go left
NON RECURSIVE TRAVERSAL
INORDER TRAVERSAL
CURRENT -> null
A
B C
D E
A
B
Go left as much as possible Push every node When left /right
becomes null
Pop
Visit
Go right
Again go left
D
NON RECURSIVE TRAVERSAL
INORDER TRAVERSAL
B
CURRENT -> null
A
B C
D E
A
E
Go left as much as possible Push every node When left /right
becomes null
Pop
Visit
Go right
Again go left
D
NON RECURSIVE TRAVERSAL
INORDER TRAVERSAL
B
CURRENT -> E
A
B C
D E
A
E
Go left as much as possible Push every node When left /right
becomes null
Pop
Visit
Go right
Again go left
D
NON RECURSIVE TRAVERSAL
INORDER TRAVERSAL
B E
CURRENT -> null
A
B C
D E
A
Go left as much as possible Push every node When left /right
becomes null
Pop
Visit
Go right
Again go left
D
NON RECURSIVE TRAVERSAL
INORDER TRAVERSAL
B E A
CURRENT -> null
A
B C
D E
C
Go left as much as possible Push every node When left /right
becomes null
Pop
Visit
Go right
Again go left
D
NON RECURSIVE TRAVERSAL
INORDER TRAVERSAL
B E A
CURRENT -> C
A
B C
D E
C
Go left as much as possible Push every node When left /right
becomes null
Pop
Visit
Go right
Again go left
D
NON RECURSIVE TRAVERSAL
INORDER TRAVERSAL
B E A C
CURRENT -> null
NON RECURSIVE TRAVERSAL
public static void inorder(Node
root) {
}
Stack<Node> stack = new Stack<>();
Node current = root;
while(current != null || !stack.isEmpty()) {
}
while(current != null) {
stack.push(current);
current = current.left;
}
current = stack.pop();
System.out.print(current.data + " ");
current = current.right;