SlideShare une entreprise Scribd logo
1  sur  23
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

Contenu connexe

Tendances (20)

Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
 
Infix to postfix conversion
Infix to postfix conversionInfix to postfix conversion
Infix to postfix conversion
 
Selection sorting
Selection sortingSelection sorting
Selection sorting
 
Heap tree
Heap treeHeap tree
Heap tree
 
Splay Tree
Splay TreeSplay Tree
Splay Tree
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data Structure
 
Red Black Tree Insertion & Deletion
Red Black Tree Insertion & DeletionRed Black Tree Insertion & Deletion
Red Black Tree Insertion & Deletion
 
Binary tree
Binary tree Binary tree
Binary tree
 
Topological Sorting
Topological SortingTopological Sorting
Topological Sorting
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Red black tree
Red black treeRed black tree
Red black tree
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 
Tree and graph
Tree and graphTree and graph
Tree and graph
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
 

Similaire à Data Structure and Algorithms Binary Tree

ds 10-Binary Tree.ppt
ds 10-Binary Tree.pptds 10-Binary Tree.ppt
ds 10-Binary Tree.pptkhitishlpu
 
chapter5.PPT
chapter5.PPTchapter5.PPT
chapter5.PPTSaralaT3
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsAakash deep Singhal
 
non linear data structure -introduction of tree
non linear data structure -introduction of treenon linear data structure -introduction of tree
non linear data structure -introduction of treeSiddhi Viradiya
 
Admissions in india 2015
Admissions in india 2015Admissions in india 2015
Admissions in india 2015Edhole.com
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures treemaamir farooq
 
Review session2
Review session2Review session2
Review session2NEEDY12345
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data StructureOm Prakash
 
Tree terminology and introduction to binary tree
Tree terminology and introduction to binary treeTree terminology and introduction to binary tree
Tree terminology and introduction to binary treejyoti_lakhani
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binarySSE_AndyLi
 
Chapter 5_Trees.pdf
Chapter 5_Trees.pdfChapter 5_Trees.pdf
Chapter 5_Trees.pdfssuser50179b
 
Tree distance algorithm
Tree distance algorithmTree distance algorithm
Tree distance algorithmTrector Rancor
 

Similaire à Data Structure and Algorithms Binary Tree (20)

ds 10-Binary Tree.ppt
ds 10-Binary Tree.pptds 10-Binary Tree.ppt
ds 10-Binary Tree.ppt
 
Trees
TreesTrees
Trees
 
chapter5.PPT
chapter5.PPTchapter5.PPT
chapter5.PPT
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithms
 
non linear data structure -introduction of tree
non linear data structure -introduction of treenon linear data structure -introduction of tree
non linear data structure -introduction of tree
 
7.tree
7.tree7.tree
7.tree
 
Admissions in india 2015
Admissions in india 2015Admissions in india 2015
Admissions in india 2015
 
Unit 4 tree
Unit 4   treeUnit 4   tree
Unit 4 tree
 
Chap 5 Tree.ppt
Chap 5 Tree.pptChap 5 Tree.ppt
Chap 5 Tree.ppt
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
 
Review session2
Review session2Review session2
Review session2
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
 
Tree terminology and introduction to binary tree
Tree terminology and introduction to binary treeTree terminology and introduction to binary tree
Tree terminology and introduction to binary tree
 
Unit 3.ppt
Unit 3.pptUnit 3.ppt
Unit 3.ppt
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binary
 
Chapter 5_Trees.pdf
Chapter 5_Trees.pdfChapter 5_Trees.pdf
Chapter 5_Trees.pdf
 
Tree distance algorithm
Tree distance algorithmTree distance algorithm
Tree distance algorithm
 
Trees unit 3
Trees unit 3Trees unit 3
Trees unit 3
 
Chapter 09-Trees
Chapter 09-TreesChapter 09-Trees
Chapter 09-Trees
 

Plus de ManishPrajapati78

Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeManishPrajapati78
 
Data Structure and Algorithms Queues
Data Structure and Algorithms QueuesData Structure and Algorithms Queues
Data Structure and Algorithms QueuesManishPrajapati78
 
Data Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge SortData Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge SortManishPrajapati78
 
Data Structure and Algorithms The Tower of Hanoi
Data Structure and Algorithms The Tower of HanoiData Structure and Algorithms The Tower of Hanoi
Data Structure and Algorithms The Tower of HanoiManishPrajapati78
 
Data Structure and Algorithms Stacks
Data Structure and Algorithms StacksData Structure and Algorithms Stacks
Data Structure and Algorithms StacksManishPrajapati78
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListManishPrajapati78
 
Data Structure and Algorithms Sorting
Data Structure and Algorithms SortingData Structure and Algorithms Sorting
Data Structure and Algorithms SortingManishPrajapati78
 
Data Structure and Algorithms Arrays
Data Structure and Algorithms ArraysData Structure and Algorithms Arrays
Data Structure and Algorithms ArraysManishPrajapati78
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms ManishPrajapati78
 
Data Structure and Algorithms Hashing
Data Structure and Algorithms HashingData Structure and Algorithms Hashing
Data Structure and Algorithms HashingManishPrajapati78
 
Data Structure and Algorithms Graph Traversal
Data Structure and Algorithms Graph TraversalData Structure and Algorithms Graph Traversal
Data Structure and Algorithms Graph TraversalManishPrajapati78
 
Data Structure and Algorithms Graphs
Data Structure and Algorithms GraphsData Structure and Algorithms Graphs
Data Structure and Algorithms GraphsManishPrajapati78
 
Data Structure and Algorithms Huffman Coding Algorithm
Data Structure and Algorithms Huffman Coding AlgorithmData Structure and Algorithms Huffman Coding Algorithm
Data Structure and Algorithms Huffman Coding AlgorithmManishPrajapati78
 
Data Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and TreesData Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and TreesManishPrajapati78
 
Data Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL TreesData Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL TreesManishPrajapati78
 

Plus de ManishPrajapati78 (15)

Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
Data Structure and Algorithms Queues
Data Structure and Algorithms QueuesData Structure and Algorithms Queues
Data Structure and Algorithms Queues
 
Data Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge SortData Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge Sort
 
Data Structure and Algorithms The Tower of Hanoi
Data Structure and Algorithms The Tower of HanoiData Structure and Algorithms The Tower of Hanoi
Data Structure and Algorithms The Tower of Hanoi
 
Data Structure and Algorithms Stacks
Data Structure and Algorithms StacksData Structure and Algorithms Stacks
Data Structure and Algorithms Stacks
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Data Structure and Algorithms Sorting
Data Structure and Algorithms SortingData Structure and Algorithms Sorting
Data Structure and Algorithms Sorting
 
Data Structure and Algorithms Arrays
Data Structure and Algorithms ArraysData Structure and Algorithms Arrays
Data Structure and Algorithms Arrays
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms
 
Data Structure and Algorithms Hashing
Data Structure and Algorithms HashingData Structure and Algorithms Hashing
Data Structure and Algorithms Hashing
 
Data Structure and Algorithms Graph Traversal
Data Structure and Algorithms Graph TraversalData Structure and Algorithms Graph Traversal
Data Structure and Algorithms Graph Traversal
 
Data Structure and Algorithms Graphs
Data Structure and Algorithms GraphsData Structure and Algorithms Graphs
Data Structure and Algorithms Graphs
 
Data Structure and Algorithms Huffman Coding Algorithm
Data Structure and Algorithms Huffman Coding AlgorithmData Structure and Algorithms Huffman Coding Algorithm
Data Structure and Algorithms Huffman Coding Algorithm
 
Data Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and TreesData Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and Trees
 
Data Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL TreesData Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL Trees
 

Dernier

Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfLivetecs LLC
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 

Dernier (20)

Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdf
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 

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 +