Comprehensive Introduction to Tree Data Structures and Binary Trees
1.
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
2.
INTRODUCTION TO TREE
DATASTRUCTURE
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.
3.
INTRODUCTION TO TREE
DATASTRUCTURE
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.
4.
TREE TERMINOLOGY
A
B CD
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.
5.
TREE TERMINOLOGY
Root NodeNode 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
6.
TREE TERMINOLOGY
A
B CD
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.
7.
TREE TERMINOLOGY
A
B CD
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.
8.
TREE TERMINOLOGY
A
B CD
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.
9.
TREE TERMINOLOGY
Root NodeNode 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
10.
TREE TERMINOLOGY
A
B CD
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
11.
TREE TERMINOLOGY
A
B CD
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
12.
GENERAL TREES
A treewhere a node can have any number of
children.
CEO
HR IT FINANCE
APPLICATIONS
Organization
hierarchy
XML
DOCUMENTS
FOLDER
STRUCTURES
13.
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
14.
PROPERTIES OF BINARYTREE
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
15.
0
1
2
1
2
4
22
21
20
MAX NOOF 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
16.
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
17.
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 𝑛
18.
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
19.
BINARY TREE &IT’S TYPES
SKEWED BINARY TREE
All nodes on one side.
A
B
C
D
20.
BINARY TREE ADT
ABinary 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()
21.
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()
22.
ADT VS IMPLEMENTATION
BinaryTree 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
23.
ARRAY BASED REPRESENTATIONOF
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 ⌋
⌊
24.
ARRAY BASED REPRESENTATIONOF
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
25.
ARRAY BASED REPRESENTATIONOF
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
26.
LINKED BASED REPRESENTATIONOF
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 {
}
27.
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
28.
Inorder traversal visitsthe node in the order:
Left -> Root -> Right
INORDER TRAVERSAL
1
2 3
4 5
7
4, 2, 5, 1, 6, 3, 7
6
29.
Preorder traversal visitsthe node in the order:
Root -> Left -> Right
PREORDER TRAVERSAL
1
2 3
4 5
7
1, 2, 4, 5, 3, 6, 7
6
30.
Postorder traversal visitsthe node in the order:
Left -> Right -> Root
POSTORDER TRAVERSAL
1
2 3
4 5
7
4, 5, 2, 6, 7, 3, 1
6
31.
BREADTH-FIRST TRAVERSAL
1
2 3
45
7
1, 2, 3, 4, 5, 6, 7
6
Explores nodes level by level from
top to bottom.
Type: Level Order Traversal
CONSTRUCT A BINARYTREE 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
45.
RECURSIVE TRAVERSAL
A
B C
DE
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);
}
46.
NON-RECURSIVE TRAVERSAL
In non-recursivetraversal, 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
47.
NON RECURSIVE TRAVERSAL
A
BC
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
48.
A
B C
D E
A
B
Goleft 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
49.
A
B C
D E
A
B
D
Goleft 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
50.
A
B C
D E
D
A
B
D
Goleft 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
51.
A
B C
D E
A
B
Goleft 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
52.
A
B C
D E
A
E
Goleft 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
53.
A
B C
D E
A
E
Goleft 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
54.
A
B C
D E
A
Goleft 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
55.
A
B C
D E
C
Goleft 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
56.
A
B C
D E
C
Goleft 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
57.
NON RECURSIVE TRAVERSAL
publicstatic 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;
Editor's Notes
#2 Data structure is a way of organize the data so that we can manage the data efficiently.
Non-linear means the data you are having multiple levels we can say. It is a form of hierarchy.
Suppose you store data in an array:
Searching is easy, but inserting in the middle is costly.
Suppose you store data in a linked list:
Insertion is easy, but searching is slow.
A Tree provides a hierarchical organization of data and supports efficient searching, insertion, and deletion. Tree basically goes from Top -> Bottom
#3 Somewhere it has written with direction If you will represent tree without direction then it will be ok . But rule is the same. You can go from top to bottom but you can’t go from bottom to top.
#20 An Abstract Data Type (ADT) is a logical or mathematical model of a data structure that specifies:
What operations can be performed
What those operations do
It does not specify how the operations are implemented.
In simple words,
ADT tells us what to do, not how to do it.
#21 An Abstract Data Type (ADT) is a logical or mathematical model of a data structure that specifies:
What operations can be performed
What those operations do
It does not specify how the operations are implemented.
In simple words,
ADT tells us what to do, not how to do it.
#22 There is no built-in BinaryTree class or interface in Java's Collections Framework.
#24 We have to follow the two main properties of CBT
#25 We have to follow the two main properties of CBT
#26 Advantages
Dynamic memory allocation
No memory wastage
Easy insertion/deletion
Suitable for any tree shape
#27 Explores as far as possible along a branch before exploring the next branch.
Types: Inorder, Preorder, Postorder
Breadth-First Traversal (BFT)
Explores nodes level by level from top to bottom.
Type: Level Order Traversal.
#44 SEE , POSTORDER IS DIVIDED INTO TWO PARTS
FOR THIS PARTICULAR SUBPROBLEM, PREORDER IS B, A, D, C, E,
#45
"When does Java remember to come back to A after visiting D?
The Call Stack remembers it.
Every recursive function call is stored in the system's Call Stack.
In non-recursive traversal, we create and manage our own stack explicitly.
#46 "Instead of Java remembering, we will remember."