3
Most read
10
Most read
12
Most read
Binary trees: introduction (complete and
extended binary trees),
Memory representation (sequential, linked)
Binary tree traversal: pre-order, in-order and
post-order (traversal algorithms using stacks)
A tree is a finite set of zero or more nodes
such that:
There is a specially designated node called
the root.
The remaining nodes are partitioned into
n>=0 disjoint sets T1, ..., Tn, where each of
these sets is a tree.
We call T1, ..., Tn the subtrees of the root.
Level and Depth
K L
E F
B
G
C
M
H I J
D
A
Level
0
1
2
3
node (13)
degree of a node(shown by green
number)
leaf (terminal)
nonterminal
parent
children
sibling
ancestor
descendant
level of a node
height(depth) of a tree (4)
3
2 1 3
2 0 0 1 0 0
0 0 0
Terminology
The degree of a node is the number of subtrees
of the node
The degree of A is 3; the degree of C is 1.
The node with degree 0 is a leaf or terminal
node.
A node that has subtrees is the parent of the
roots of the subtrees.
The roots of these subtrees are the children of
the node.
Children of the same parent are siblings.
The ancestors of a node are all the nodes
along the path from the root to the node.
Tree Properties
A
B C
D
G
E F
IH
Property Value
Number of nodes
Height
Root Node
Leaves
Interior nodes
Number of levels
Ancestors of H
Descendants of B
Siblings of E
degree of node A
Binary Trees
A special class of trees: max degree for each
node is 2
Recursive definition: A binary tree is a finite
set of nodes that is either empty or consists of
a root and two disjoint binary trees called the
left subtree and the right subtree.
J
IM
H
L
A
B
C
D
E
F GK
Samples of Trees
A
B
A
B
A
B C
GE
I
D
H
F
Complete Binary Tree
Skewed Binary Tree
E
C
D
Maximum Number of Nodes in BT
The maximum number of nodes on level i of a
binary tree is 2i
, i>=0.
The maximum number of nodes in a binary
tree
of depth k is 2k
-1, k>=1.
Full BT vs. Complete BT
A full binary tree of depth k is a binary tree of depth k
having 2k
-1 nodes, k>=1.
A binary tree with n nodes and depth k is
complete iff its nodes correspond to the nodes numbered
from 1 to n in the full binary tree of depth k.
A
B C
GE
I
D
H
F
A
B C
GE
K
D
J
F
IH ONML
Full binary tree of depth 4Complete binary tree
Complete Binary Tree
If a complete binary tree with n nodes
(depth= log└ n + 1 )┘
is represented sequentially,
then for any node with index i, 1<=i<=n, we have:
parent(i) is at i└ /2┘ if i!=1. If i=1, i is at the root and
has no parent.
leftChild(i) is at 2i if 2i<=n. If 2i>n, then i has no
left child.
rightChild(i) is at 2i+1 if 2i+1<=n. If 2i+1>n,
then i has no right child.
Extended Binary TreeA binary tree T is said to be a 2-tree or an extended binary tree if each
node N has either 0 or 2 children.
The nodes with 2 children are called internal nodes.
The nodes with o children are called external nodes. A
B C
G
D F
A
B C
G
D F
Fig: Binary Tree T Fig: Extended 2-tree
Sequential
RepresentationA
B
--
C
--
--
--
D
--
.
E
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
.
[16]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
A
B
C
D
E
F
G
H
I
A
B
E
C
D
A
B C
GE
I
D
H
F
(1) waste space
(2) insertion/deletion
problem
Linked Representation
struct btnode {
int data;
btnode *left, *right;
};
dataleft right
data
left right
Binary Tree Traversals
There are three standard ways of traversing a
binary tree T with root R.
These three algorithms, called:
preorder
inorder
postorder
Arithmetic Expression Using BT
+
*
A
*
/
E
D
C
B
preorder traversal
+ * * / A B C D E
prefix expression
inorder traversal
A / B * C * D + E
infix expression
postorder traversal
A B / C * D * E +
postfix expression
Preorder Traversal (recursive
version)
Algorithm:
1. Process the root R.
2. Traverse the left subtree of R in preorder.
3. Traverse the right subtree of R in preorder.
Pseudo-Code:
void preorder(btnode ptr)
/* preorder tree traversal */
{
if (ptr!=NULL) {
cout<<ptr->data;
preorder(ptr->left);
predorder(ptr->right);
}
}
+ * * / A B C D E
Inorder Traversal (recursive version)
Algorithm:
1. Traverse the left subtree of R in inorder.
2. Process the root R.
3. Traverse the right subtree of R in inorder.
Pseudo-Code:
void inorder(btnode ptr)
/* inorder tree traversal */
{
if (ptr!=NULL) {
inorder(ptr->left);
cout<<ptr->data;
indorder(ptr->right);
}
}
A / B * C * D + E
Postorder Traversal (recursive version)
Algorithm:
1. Traverse the left subtree of R in postorder.
2. Traverse the right subtree of R in postorder.
3. Process the root R.
Pseudo-Code:
void postorder(btnode ptr)
/* postorder tree traversal */
{
if (ptr!=NULL) {
postorder(ptr->left);
postorder(ptr->right);
cout<<ptr->data;
}
}
A B / C * D * E +
Preorder Traversal (using stack)
PREORD(INFO, LEFT, RIGHT, ROOT)
1. Set TOP:= 1, STACK[1]:= NULL and PTR := ROOT.
2. Repeat Steps 3 to 5 while PTR != NULL:
3. Apply PROCESS to INFO[PTR].
4. If RIGHT[PTR]!= NULL then
Set TOP := TOP+1, and STACK[TOP]:= RIGHT[PTR].
5. If LEFT[PTR]!= NULL then
Set PTR:= LEFT[PTR].
Else
Set PTR:= STACK[TOP] TOP:= TOP-1.
[End of If structure]
[End of step 2 Loop]
6. Exit.
+ * * / A B C D E
Inorder Traversal (using stack)
INORD(INFO, LEFT, RIGHT, ROOT)
1.Set TOP:= 1, STACK[1]:= NULL and PTR := ROOT.
2.Repeat while PTR != NULL:
a) Set TOP:=TOP+1, STACK[TOP]:=PTR.
b) Set PTR:= LEFT[PTR].
[End of loop]
3.Set PTR:=STACK[TOP], TOP:=TOP-1.
4.Repeat Steps 5 to 7 while PTR != NULL:
5.Apply PROCESS to INFO[PTR].
6.If RIGHT[PTR]!= NULL, then:
a) Set PTR:=RIGHT[PTR]
b) Go to Step 2.
[End of If structure]
7.Set PTR:=STACK[TOP] and TOP:=TOP-1.
[End of step 4 Loop.]
8.Exit
A / B * C * D + E
Postorder Traversal (using stack)
POSTORD(INFO, LEFT, RIGHT, ROOT)
1.Set TOP:= 1, STACK[1]:= NULL and PTR := ROOT.
2.Repeat Step 3 to 5 while PTR!=NULL:
3.Set TOP := TOP+1, STACK[Top]:=PTR.
4.If RIGHT[PTR]!=NULL, then:
Set TOP := TOP+1, Stack[Top]:=-
RIGHT[PTR].
[End of If structure]
5.Set PTR:=LEFT[PTR]
[End of step 3 Loop]
6.Set PTR:=STACK[Top], TOP := TOP-1.
7.Repeat while PTR>0:
a)Apply PROCESS to INFO[PTR].
b)PTR:=STACK[Top], TOP := TOP-1.
[End of Loop]
8.Repeat while PTR<0:
a) Set PTR:= -PTR.
b) Go to Step 2.
[End of If structure]
A B / C * D * E +
Data Structure and Algorithms Binary Tree

More Related Content

PPTX
Unit 4-booth algorithm
PDF
sparse matrix in data structure
PPTX
Analysis and Design of Algorithms
PPTX
Searching & Sorting Algorithms
PPTX
Unit v memory &amp; programmable logic devices
PPTX
Prim's algorithm
PPTX
DMA and DMA controller
PPTX
Input Output Organization
Unit 4-booth algorithm
sparse matrix in data structure
Analysis and Design of Algorithms
Searching & Sorting Algorithms
Unit v memory &amp; programmable logic devices
Prim's algorithm
DMA and DMA controller
Input Output Organization

What's hot (20)

PPTX
General register organization (computer organization)
PPT
Ch 2 lattice & boolean algebra
PPTX
Sum of subset problem.pptx
PPTX
Graph traversals in Data Structures
PDF
TOC 1 | Introduction to Theory of Computation
PPT
Binary Search
PPTX
Binary Search Tree in Data Structure
PPTX
Tree traversal techniques
PPTX
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDA
PPTX
Queue ppt
PPTX
Dijkstra's algorithm presentation
PPT
Ch12 microprocessor interrupts
PPTX
Introduction to data structure ppt
PPTX
Asymptotic notations
PDF
Applications of stack
PPT
Binary search tree(bst)
PPS
Computer instructions
PPTX
Shortest path algorithm
PPTX
Computer Organisation & Architecture (chapter 1)
PPTX
DFS and BFS
General register organization (computer organization)
Ch 2 lattice & boolean algebra
Sum of subset problem.pptx
Graph traversals in Data Structures
TOC 1 | Introduction to Theory of Computation
Binary Search
Binary Search Tree in Data Structure
Tree traversal techniques
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDA
Queue ppt
Dijkstra's algorithm presentation
Ch12 microprocessor interrupts
Introduction to data structure ppt
Asymptotic notations
Applications of stack
Binary search tree(bst)
Computer instructions
Shortest path algorithm
Computer Organisation & Architecture (chapter 1)
DFS and BFS
Ad

Similar to Data Structure and Algorithms Binary Tree (20)

PPT
ds 10-Binary Tree.ppt
PPT
PPT
Data Structure And Algorithms for Computer Science
PPTX
Unit-VStackStackStackStackStackStack.pptx
PPT
binary tree
PDF
Chapter 5_Trees.pdf
PPTX
PPT
Admissions in india 2015
PPTX
Unit 6 tree
PPTX
binary tree.pptx
PDF
Lecture notes data structures tree
PPTX
Basics of Binary Tree and Binary Search Tree.pptx
PPT
Chap 5 Tree.ppt
PPTX
tree Data Structures in python Traversals.pptx
PPTX
Why Tree is considered a non-linear data structure?
PPTX
Creating a Binary tree from a General Tree.pptx
PPTX
Tree structure and its definitions with an example
PDF
Binary Trees
PPTX
trees in data structure
PPT
Algorithm and Data Structure - Binary Trees
ds 10-Binary Tree.ppt
Data Structure And Algorithms for Computer Science
Unit-VStackStackStackStackStackStack.pptx
binary tree
Chapter 5_Trees.pdf
Admissions in india 2015
Unit 6 tree
binary tree.pptx
Lecture notes data structures tree
Basics of Binary Tree and Binary Search Tree.pptx
Chap 5 Tree.ppt
tree Data Structures in python Traversals.pptx
Why Tree is considered a non-linear data structure?
Creating a Binary tree from a General Tree.pptx
Tree structure and its definitions with an example
Binary Trees
trees in data structure
Algorithm and Data Structure - Binary Trees
Ad

More from ManishPrajapati78 (15)

PPT
Data Structure and Algorithms Binary Search Tree
PPT
Data Structure and Algorithms Queues
PPTX
Data Structure and Algorithms Merge Sort
PPTX
Data Structure and Algorithms The Tower of Hanoi
PPT
Data Structure and Algorithms Stacks
PPT
Data Structure and Algorithms Linked List
PPT
Data Structure and Algorithms Sorting
PPT
Data Structure and Algorithms Arrays
PPT
Data Structure and Algorithms
PPT
Data Structure and Algorithms Hashing
PPTX
Data Structure and Algorithms Graph Traversal
PPT
Data Structure and Algorithms Graphs
PPT
Data Structure and Algorithms Huffman Coding Algorithm
PPT
Data Structure and Algorithms Heaps and Trees
PPT
Data Structure and Algorithms AVL Trees
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Queues
Data Structure and Algorithms Merge Sort
Data Structure and Algorithms The Tower of Hanoi
Data Structure and Algorithms Stacks
Data Structure and Algorithms Linked List
Data Structure and Algorithms Sorting
Data Structure and Algorithms Arrays
Data Structure and Algorithms
Data Structure and Algorithms Hashing
Data Structure and Algorithms Graph Traversal
Data Structure and Algorithms Graphs
Data Structure and Algorithms Huffman Coding Algorithm
Data Structure and Algorithms Heaps and Trees
Data Structure and Algorithms AVL Trees

Recently uploaded (20)

PPTX
DevOpsDays Halifax 2025 - Building 10x Organizations Using Modern Productivit...
PPTX
Foundations of Marketo Engage: Nurturing
PDF
Understanding the Need for Systemic Change in Open Source Through Intersectio...
PDF
infoteam HELLAS company profile 2025 presentation
PDF
IDM Crack 6.42 Build 42 Patch Serial Key 2025 Free New Version
PDF
Workplace Software and Skills - OpenStax
PDF
AI-Powered Fuzz Testing: The Future of QA
PDF
Sun and Bloombase Spitfire StoreSafe End-to-end Storage Security Solution
PDF
Crypto Loss And Recovery Guide By Expert Recovery Agency.
PPTX
Viber For Windows 25.7.1 Crack + Serial Keygen
PPTX
Chapter_05_System Modeling for software engineering
PDF
PDF-XChange Editor Plus 10.7.0.398.0 Crack Free Download Latest 2025
PPTX
ROI from Efficient Content & Campaign Management in the Digital Media Industry
PPTX
Chapter 1 - Transaction Processing and Mgt.pptx
PPTX
Human Computer Interaction lecture Chapter 2.pptx
PPTX
DevOpsDays Halifax 2025 - Building 10x Organizations Using Modern Productivit...
PDF
Sanket Mhaiskar Resume - Senior Software Engineer (Backend, AI)
PDF
Building an Inclusive Web Accessibility Made Simple with Accessibility Analyzer
PDF
Mobile App Backend Development with WordPress REST API: The Complete eBook
PPTX
Bandicam Screen Recorder 8.2.1 Build 2529 Crack
DevOpsDays Halifax 2025 - Building 10x Organizations Using Modern Productivit...
Foundations of Marketo Engage: Nurturing
Understanding the Need for Systemic Change in Open Source Through Intersectio...
infoteam HELLAS company profile 2025 presentation
IDM Crack 6.42 Build 42 Patch Serial Key 2025 Free New Version
Workplace Software and Skills - OpenStax
AI-Powered Fuzz Testing: The Future of QA
Sun and Bloombase Spitfire StoreSafe End-to-end Storage Security Solution
Crypto Loss And Recovery Guide By Expert Recovery Agency.
Viber For Windows 25.7.1 Crack + Serial Keygen
Chapter_05_System Modeling for software engineering
PDF-XChange Editor Plus 10.7.0.398.0 Crack Free Download Latest 2025
ROI from Efficient Content & Campaign Management in the Digital Media Industry
Chapter 1 - Transaction Processing and Mgt.pptx
Human Computer Interaction lecture Chapter 2.pptx
DevOpsDays Halifax 2025 - Building 10x Organizations Using Modern Productivit...
Sanket Mhaiskar Resume - Senior Software Engineer (Backend, AI)
Building an Inclusive Web Accessibility Made Simple with Accessibility Analyzer
Mobile App Backend Development with WordPress REST API: The Complete eBook
Bandicam Screen Recorder 8.2.1 Build 2529 Crack

Data Structure and Algorithms Binary Tree

  • 1. Binary trees: introduction (complete and extended binary trees), Memory representation (sequential, linked) Binary tree traversal: pre-order, in-order and post-order (traversal algorithms using stacks)
  • 2. A tree is a finite set of zero or more nodes such that: There is a specially designated node called the root. The remaining nodes are partitioned into n>=0 disjoint sets T1, ..., Tn, where each of these sets is a tree. We call T1, ..., Tn the subtrees of the root.
  • 3. Level and Depth K L E F B G C M H I J D A Level 0 1 2 3 node (13) degree of a node(shown by green number) leaf (terminal) nonterminal parent children sibling ancestor descendant level of a node height(depth) of a tree (4) 3 2 1 3 2 0 0 1 0 0 0 0 0
  • 4. Terminology The degree of a node is the number of subtrees of the node The degree of A is 3; the degree of C is 1. The node with degree 0 is a leaf or terminal node. A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the children of the node. Children of the same parent are siblings. The ancestors of a node are all the nodes along the path from the root to the node.
  • 5. Tree Properties A B C D G E F IH Property Value Number of nodes Height Root Node Leaves Interior nodes Number of levels Ancestors of H Descendants of B Siblings of E degree of node A
  • 6. Binary Trees A special class of trees: max degree for each node is 2 Recursive definition: A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary trees called the left subtree and the right subtree.
  • 8. Samples of Trees A B A B A B C GE I D H F Complete Binary Tree Skewed Binary Tree E C D
  • 9. Maximum Number of Nodes in BT The maximum number of nodes on level i of a binary tree is 2i , i>=0. The maximum number of nodes in a binary tree of depth k is 2k -1, k>=1.
  • 10. Full BT vs. Complete BT A full binary tree of depth k is a binary tree of depth k having 2k -1 nodes, k>=1. A binary tree with n nodes and depth k is complete iff its nodes correspond to the nodes numbered from 1 to n in the full binary tree of depth k. A B C GE I D H F A B C GE K D J F IH ONML Full binary tree of depth 4Complete binary tree
  • 11. Complete Binary Tree If a complete binary tree with n nodes (depth= log└ n + 1 )┘ is represented sequentially, then for any node with index i, 1<=i<=n, we have: parent(i) is at i└ /2┘ if i!=1. If i=1, i is at the root and has no parent. leftChild(i) is at 2i if 2i<=n. If 2i>n, then i has no left child. rightChild(i) is at 2i+1 if 2i+1<=n. If 2i+1>n, then i has no right child.
  • 12. Extended Binary TreeA binary tree T is said to be a 2-tree or an extended binary tree if each node N has either 0 or 2 children. The nodes with 2 children are called internal nodes. The nodes with o children are called external nodes. A B C G D F A B C G D F Fig: Binary Tree T Fig: Extended 2-tree
  • 14. Linked Representation struct btnode { int data; btnode *left, *right; }; dataleft right data left right
  • 15. Binary Tree Traversals There are three standard ways of traversing a binary tree T with root R. These three algorithms, called: preorder inorder postorder
  • 16. Arithmetic Expression Using BT + * A * / E D C B preorder traversal + * * / A B C D E prefix expression inorder traversal A / B * C * D + E infix expression postorder traversal A B / C * D * E + postfix expression
  • 17. Preorder Traversal (recursive version) Algorithm: 1. Process the root R. 2. Traverse the left subtree of R in preorder. 3. Traverse the right subtree of R in preorder. Pseudo-Code: void preorder(btnode ptr) /* preorder tree traversal */ { if (ptr!=NULL) { cout<<ptr->data; preorder(ptr->left); predorder(ptr->right); } } + * * / A B C D E
  • 18. Inorder Traversal (recursive version) Algorithm: 1. Traverse the left subtree of R in inorder. 2. Process the root R. 3. Traverse the right subtree of R in inorder. Pseudo-Code: void inorder(btnode ptr) /* inorder tree traversal */ { if (ptr!=NULL) { inorder(ptr->left); cout<<ptr->data; indorder(ptr->right); } } A / B * C * D + E
  • 19. Postorder Traversal (recursive version) Algorithm: 1. Traverse the left subtree of R in postorder. 2. Traverse the right subtree of R in postorder. 3. Process the root R. Pseudo-Code: void postorder(btnode ptr) /* postorder tree traversal */ { if (ptr!=NULL) { postorder(ptr->left); postorder(ptr->right); cout<<ptr->data; } } A B / C * D * E +
  • 20. Preorder Traversal (using stack) PREORD(INFO, LEFT, RIGHT, ROOT) 1. Set TOP:= 1, STACK[1]:= NULL and PTR := ROOT. 2. Repeat Steps 3 to 5 while PTR != NULL: 3. Apply PROCESS to INFO[PTR]. 4. If RIGHT[PTR]!= NULL then Set TOP := TOP+1, and STACK[TOP]:= RIGHT[PTR]. 5. If LEFT[PTR]!= NULL then Set PTR:= LEFT[PTR]. Else Set PTR:= STACK[TOP] TOP:= TOP-1. [End of If structure] [End of step 2 Loop] 6. Exit. + * * / A B C D E
  • 21. Inorder Traversal (using stack) INORD(INFO, LEFT, RIGHT, ROOT) 1.Set TOP:= 1, STACK[1]:= NULL and PTR := ROOT. 2.Repeat while PTR != NULL: a) Set TOP:=TOP+1, STACK[TOP]:=PTR. b) Set PTR:= LEFT[PTR]. [End of loop] 3.Set PTR:=STACK[TOP], TOP:=TOP-1. 4.Repeat Steps 5 to 7 while PTR != NULL: 5.Apply PROCESS to INFO[PTR]. 6.If RIGHT[PTR]!= NULL, then: a) Set PTR:=RIGHT[PTR] b) Go to Step 2. [End of If structure] 7.Set PTR:=STACK[TOP] and TOP:=TOP-1. [End of step 4 Loop.] 8.Exit A / B * C * D + E
  • 22. Postorder Traversal (using stack) POSTORD(INFO, LEFT, RIGHT, ROOT) 1.Set TOP:= 1, STACK[1]:= NULL and PTR := ROOT. 2.Repeat Step 3 to 5 while PTR!=NULL: 3.Set TOP := TOP+1, STACK[Top]:=PTR. 4.If RIGHT[PTR]!=NULL, then: Set TOP := TOP+1, Stack[Top]:=- RIGHT[PTR]. [End of If structure] 5.Set PTR:=LEFT[PTR] [End of step 3 Loop] 6.Set PTR:=STACK[Top], TOP := TOP-1. 7.Repeat while PTR>0: a)Apply PROCESS to INFO[PTR]. b)PTR:=STACK[Top], TOP := TOP-1. [End of Loop] 8.Repeat while PTR<0: a) Set PTR:= -PTR. b) Go to Step 2. [End of If structure] A B / C * D * E +