SlideShare a Scribd company logo
TREE Data Structures
1- Linear Data Structures
Examples: Arrays, Link List, Stack, Queue
2- Non-Linear Data Structures
Examples: Tree, Graphs, etc.
Types of Tree
a) Natural Tree
b) Programming Tree
Types of Programming Tree
a) Binary Tree
b) Binary Search Tree
c) AVL/Balance Tree
d) Spanning Tree
e) 2/3 Tree
f) 2/3/4 Tree
g) B Tree
h) B+ Tree
What is a Tree?
• General Concepts
Tree Descriptions (Important Terms)
• Path − Path refers to the sequence of nodes along the edges of a tree.
• Root – The node at the top of the tree is called root. There is only one root per tree
and one path from the root node to any node.
• Parent − Any node except the root node has one edge upward to a node called parent.
• Child – The node below a given node connected by its edge downward is called its child
node.
• Leaf – The node which does not have any child node is called the leaf node.
• Subtree − Subtree represents the descendants of a node.
• Visiting − Visiting refers to checking the value of a node when control is on the node.
• Traversing − Traversing means passing through nodes in a specific order.
• Levels − Level of a node represents the generation of a node. If the root node is at
level 0, then its next child node is at level 1, its grandchild is at level 2, and so on.
• Keys − Key represents a value of a node based on which a search operation is to be
carried out for a node.
Binary Tree
Binary Tree
Binary Tree is a tree in which one parent node can have maximum 2 child
nodes and minimum 0 child node.
a) Strictly BT
Strictly Binary Tree is such a binary tree in which one parent node can have maximum
2 child nodes.
b) Complete Binary Tree
Complete Binary Tree is such a Binary Tree in which all the leaf nodes should be at the
same level and at each level there can be maximum 2level nodes.
Total number of nodes in complete binary tree = 2d+1 -1, where d represents the depth
of tree.
• Height of Tree:
The total number of levels from root node to leaf node is called height of tree.
Complete Binary Tree Representation
CBT exhibits a special behavior. A node's left child must have a value less than its parent's value and the node's right
child must have a value greater than its parent value.
Traversal of Tree
Whenever you visit each node of tree then this process is called
traversal or tree traversal.
Types of Tree Traversal
1- Pre-Ordered Traversal
2- In-Ordered Traversal
3- Post-Ordered Traversal
Pre-Ordered Traversal:
a) Visit the root node
b) Traverse the lefts of sub-tree
c) Traverse the rights of sub-tree
Traversal of Tree
In-Ordered Traversal:
a) Traverse the lefts of sub-tree
b) Visit the root node
c) Traverse the rights of sub-tree
Post-Ordered Traversal:
a) Traverse the lefts of sub-tree
b) Traverse the rights of sub-tree
c) Visit the root node
Binary Search Tree (BST)
Binary Search Tree:
A binary tree in which the left child node of a tree or sub-tree has lesser value than its
root node but the right child node has greater value than its root node, such a binary tree is
called Binary Search Tree or BST.
When a BST is traversed in-order and the data item of each node is printed/read, then the data
item is printed in ascending order.
Advantages of BST:
Easy to perform operations i.e. insertion, creating new tree, searching and deletion in BST
Example:
Construction of BST for the given values, such as
45 30 35 25 60 70 80 55
4 3 30 25 16 10 8 5
Structure for Node Creation
struct node
{
int data;
struct node *leftChild;
struct node *rightChild;
};
BST Basic Operations
• The basic operations that can be performed on a binary search tree
data structure, are the following −
• Insert ()− Inserts an element in a tree/create a tree.
• Search () − Searches an element in a tree.
• Delete () – Delete an element in a tree
• Pre-order Traversal () − Traverses a tree in a pre-order manner.
• In-order Traversal ()− Traverses a tree in an in-order manner.
• Post-order Traversal ()− Traverses a tree in a post-order manner.
Algorithm for insertion in BST
If root is NULL
then create root node
Return
If root exists then
compare the data with node.data
while until insertion position is located
If data is greater than node.data
goto right subtree
else
goto left subtree
endwhile
insert data
end If
BST Insertion Implementation
class CBST
{
private:
struct node *root;
public:
CBST(){root==NULL;}
void add_CBST_Node(int x);
void delete_CBST_Node(int x);
void Pre_Order();
void In_Order();
void Post_Order();
};
void CBST :: add_CBST_Node(int x) {
struct node *p, *s, *par;
p = new node;
p -> info = x;
p -> left == NULL; p -> right == NULL;
if(root == NULL)
root = p;
else {
s = root;
while(s != NULL) {
par = s;
if(x > s -> info)
s = s-> right;
else
s = s -> left;
}
if(x < par -> info)
par -> left = p;
else par -> right = p;
}
}
Algorithm for Searching in BST
If root.data is equal to search.data
return root
else
while data not found
If data is greater than node.data
goto right subtree
else
goto left subtree
If data found
return node
Endwhile
return data not found
end if
Searching in BST
struct node* search(int data) {
struct node *current = root;
printf("Visiting elements: ");
while(current->data != data) {
if(current != NULL)
printf("%d ",current->data);
//go to left tree
if(current->data > data) {
current = current->leftChild;
}
//else go to right tree
else {
current = current->rightChild;
}
//not found
if(current == NULL) {
return NULL;
}
return current;
}
}
Deletion in BST
Deletion of left most node (Minimum Data Item)
s = root;
while(s -> left != NULL)
{
par=s;
s=s->left;
}
par->left=s->right;
delete s;
Deletion in BST
Deletion of Right most node (Maximum Data Item)
s = root;
while(s -> right != NULL)
{
par=s;
s=s->right;
}
par->right=s->right;
delete s;
Binary Search Tree Deletion Cases
Different Cases for Deletion node
1- if deleted node has not null in its left or right link
2- if deleted node has left link null
3- if deleted node has right link null
BST Deletion Implementation
class CBST:: delete_tree_node(int x)
{
struct node *p, *s, *rp, *f, *par;
p=root;
par=NULL;
while((p!=NULL)&&(p->info!=x))
{
par=p;
if(x>p->info)
p=p->right;
else p=p->left;
}
if (p==NULL)
{
cout<<“ No Required Node exist
Tree”;
return;
}
if(p->left==NULL)
rp=p->right;
else if(p->right==NULL)
rp=p->left;
else
{
f=p;
rp=p->right;
s=rp->left;
while(s!=NULL)
{
f=rp;
rp=s;
s=rp->left;
}
if(f!=p)
{
f->left=rp->right;
rp->right=p->right;
}
}
if(par==NULL)
root=rp;
if(p==par->right)
par->right= rp;
else
par->left=rp;
delete p;
}
In-order Traversal
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Visit root node.
Step 3 − Recursively traverse right subtree.
void inorder_traversal(struct node* root) {
if(root != NULL) {
inorder_traversal(root->leftChild);
printf("%d ",root->data);
inorder_traversal(root->rightChild);
Pre-Order Traversal
Until all nodes are traversed −
Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.
void pre_order_traversal(struct node* root) {
if(root != NULL) {
printf("%d ",root->data);
pre_order_traversal(root->leftChild);
pre_order_traversal(root->rightChild);
}
}
Post-Order Traversal
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Recursively traverse right subtree.
Step 3 − Visit root node.
void post_order_traversal(struct node* root) {
if(root != NULL) {
post_order_traversal(root->leftChild);
post_order_traversal(root->rightChild);
printf("%d ", root->data);
}
}
AVL (Adelson Velski and landis)Tree / Balance
Tree
• Balance Factor:
BF of any node= Height of left subtree – Height of right subtree
• If BF = (-1, 0, 1) No rotation performed
• Height of Tree
• Reconstruction of Tree
• Left rotation
(left rotation is made when balance factor of any tree becomes -2)
• Right rotation
(right rotation is made when balance factor of any tree becomes +2)
Assignments:
Question#1:
Write a code for Pre-order, Post-order and In-order procedures of CBST to
display data in required formats.
Question#2:
Write a code which will create a CBST and a coy function which will create exact
copy of original CBST.
Question#3:
Write a code for CBST which will create a tree of even nodes and also for odd
nodes.
Question#4:
Write a code which will create AVL Tree and determine its balance factor, and if
balance factor is increasing from 2 or -2, then made its right/left rotations.
Ad

More Related Content

Similar to TREE DATA STRUCTURE SLIDES dsa dsa .pptx (20)

learn tree, linked list, queue, stack, and other algo
learn tree, linked list, queue, stack, and other algolearn tree, linked list, queue, stack, and other algo
learn tree, linked list, queue, stack, and other algo
unknowwqer
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptx
skilljiolms
 
Binary Tree - Algorithms
Binary Tree - Algorithms Binary Tree - Algorithms
Binary Tree - Algorithms
CourseHunt
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
Anil Dutt
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
Daniyal Khan
 
Binary Search Tree.pptx
Binary Search Tree.pptxBinary Search Tree.pptx
Binary Search Tree.pptx
RaaviKapoor
 
Treeeeeeeeeeeeeeeeereeeeeeeeeeeeeeee.pdf
Treeeeeeeeeeeeeeeeereeeeeeeeeeeeeeee.pdfTreeeeeeeeeeeeeeeeereeeeeeeeeeeeeeee.pdf
Treeeeeeeeeeeeeeeeereeeeeeeeeeeeeeee.pdf
timoemin50
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
sagar yadav
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
ER Punit Jain
 
Introduction to Tree_Data Structure.pptx
Introduction to Tree_Data Structure.pptxIntroduction to Tree_Data Structure.pptx
Introduction to Tree_Data Structure.pptx
PoojariniMitra1
 
Biary search Tree.docx
Biary search Tree.docxBiary search Tree.docx
Biary search Tree.docx
sowmya koneru
 
Binary search tree
Binary search treeBinary search tree
Binary search tree
Sana Yameen
 
Trees
TreesTrees
Trees
9590133127
 
Unit8 C
Unit8 CUnit8 C
Unit8 C
arnold 7490
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
Krish_ver2
 
Binary tree and operations
Binary tree and operations Binary tree and operations
Binary tree and operations
varagilavanya
 
Data Structure Lecture 3 Linked Lists.pdf
Data Structure Lecture 3 Linked Lists.pdfData Structure Lecture 3 Linked Lists.pdf
Data Structure Lecture 3 Linked Lists.pdf
donotreply20
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
Anusruti Mitra
 
Chapter 5_Trees.pdf
Chapter 5_Trees.pdfChapter 5_Trees.pdf
Chapter 5_Trees.pdf
ssuser50179b
 
Binary tree
Binary treeBinary tree
Binary tree
Maria Saleem
 
learn tree, linked list, queue, stack, and other algo
learn tree, linked list, queue, stack, and other algolearn tree, linked list, queue, stack, and other algo
learn tree, linked list, queue, stack, and other algo
unknowwqer
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptx
skilljiolms
 
Binary Tree - Algorithms
Binary Tree - Algorithms Binary Tree - Algorithms
Binary Tree - Algorithms
CourseHunt
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
Anil Dutt
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
Daniyal Khan
 
Binary Search Tree.pptx
Binary Search Tree.pptxBinary Search Tree.pptx
Binary Search Tree.pptx
RaaviKapoor
 
Treeeeeeeeeeeeeeeeereeeeeeeeeeeeeeee.pdf
Treeeeeeeeeeeeeeeeereeeeeeeeeeeeeeee.pdfTreeeeeeeeeeeeeeeeereeeeeeeeeeeeeeee.pdf
Treeeeeeeeeeeeeeeeereeeeeeeeeeeeeeee.pdf
timoemin50
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
sagar yadav
 
Introduction to Tree_Data Structure.pptx
Introduction to Tree_Data Structure.pptxIntroduction to Tree_Data Structure.pptx
Introduction to Tree_Data Structure.pptx
PoojariniMitra1
 
Biary search Tree.docx
Biary search Tree.docxBiary search Tree.docx
Biary search Tree.docx
sowmya koneru
 
Binary search tree
Binary search treeBinary search tree
Binary search tree
Sana Yameen
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
Krish_ver2
 
Binary tree and operations
Binary tree and operations Binary tree and operations
Binary tree and operations
varagilavanya
 
Data Structure Lecture 3 Linked Lists.pdf
Data Structure Lecture 3 Linked Lists.pdfData Structure Lecture 3 Linked Lists.pdf
Data Structure Lecture 3 Linked Lists.pdf
donotreply20
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
Anusruti Mitra
 
Chapter 5_Trees.pdf
Chapter 5_Trees.pdfChapter 5_Trees.pdf
Chapter 5_Trees.pdf
ssuser50179b
 

Recently uploaded (20)

Day 1 MS Excel Basics #.pptxDay 1 MS Excel Basics #.pptxDay 1 MS Excel Basics...
Day 1 MS Excel Basics #.pptxDay 1 MS Excel Basics #.pptxDay 1 MS Excel Basics...Day 1 MS Excel Basics #.pptxDay 1 MS Excel Basics #.pptxDay 1 MS Excel Basics...
Day 1 MS Excel Basics #.pptxDay 1 MS Excel Basics #.pptxDay 1 MS Excel Basics...
Jayantilal Bhanushali
 
indonesia-gen-z-report-2024 Gen Z (born between 1997 and 2012) is currently t...
indonesia-gen-z-report-2024 Gen Z (born between 1997 and 2012) is currently t...indonesia-gen-z-report-2024 Gen Z (born between 1997 and 2012) is currently t...
indonesia-gen-z-report-2024 Gen Z (born between 1997 and 2012) is currently t...
disnakertransjabarda
 
Time series for yotube_1_data anlysis.pdf
Time series for yotube_1_data anlysis.pdfTime series for yotube_1_data anlysis.pdf
Time series for yotube_1_data anlysis.pdf
asmaamahmoudsaeed
 
2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry
2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry
2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry
bastakwyry
 
Dynamics 365 Business Rules Dynamics Dynamics
Dynamics 365 Business Rules Dynamics DynamicsDynamics 365 Business Rules Dynamics Dynamics
Dynamics 365 Business Rules Dynamics Dynamics
heyoubro69
 
AWS RDS Presentation to make concepts easy.pptx
AWS RDS Presentation to make concepts easy.pptxAWS RDS Presentation to make concepts easy.pptx
AWS RDS Presentation to make concepts easy.pptx
bharatkumarbhojwani
 
Fundamentals of Data Analysis, its types, tools, algorithms
Fundamentals of Data Analysis, its types, tools, algorithmsFundamentals of Data Analysis, its types, tools, algorithms
Fundamentals of Data Analysis, its types, tools, algorithms
priyaiyerkbcsc
 
Sets theories and applications that can used to imporve knowledge
Sets theories and applications that can used to imporve knowledgeSets theories and applications that can used to imporve knowledge
Sets theories and applications that can used to imporve knowledge
saumyasl2020
 
Z14_IBM__APL_by_Christian_Demmer_IBM.pdf
Z14_IBM__APL_by_Christian_Demmer_IBM.pdfZ14_IBM__APL_by_Christian_Demmer_IBM.pdf
Z14_IBM__APL_by_Christian_Demmer_IBM.pdf
Fariborz Seyedloo
 
AI ------------------------------ W1L2.pptx
AI ------------------------------ W1L2.pptxAI ------------------------------ W1L2.pptx
AI ------------------------------ W1L2.pptx
AyeshaJalil6
 
Process Mining at Deutsche Bank - Journey
Process Mining at Deutsche Bank - JourneyProcess Mining at Deutsche Bank - Journey
Process Mining at Deutsche Bank - Journey
Process mining Evangelist
 
problem solving.presentation slideshow bsc nursing
problem solving.presentation slideshow bsc nursingproblem solving.presentation slideshow bsc nursing
problem solving.presentation slideshow bsc nursing
vishnudathas123
 
录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单
录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单
录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单
Taqyea
 
CS-404 COA COURSE FILE JAN JUN 2025.docx
CS-404 COA COURSE FILE JAN JUN 2025.docxCS-404 COA COURSE FILE JAN JUN 2025.docx
CS-404 COA COURSE FILE JAN JUN 2025.docx
nidarizvitit
 
L1_Slides_Foundational Concepts_508.pptx
L1_Slides_Foundational Concepts_508.pptxL1_Slides_Foundational Concepts_508.pptx
L1_Slides_Foundational Concepts_508.pptx
38NoopurPatel
 
Understanding Complex Development Processes
Understanding Complex Development ProcessesUnderstanding Complex Development Processes
Understanding Complex Development Processes
Process mining Evangelist
 
Analysis of Billboards hot 100 toop five hit makers on the chart.docx
Analysis of Billboards hot 100 toop five hit makers on the chart.docxAnalysis of Billboards hot 100 toop five hit makers on the chart.docx
Analysis of Billboards hot 100 toop five hit makers on the chart.docx
hershtara1
 
Dr. Robert Krug - Expert In Artificial Intelligence
Dr. Robert Krug - Expert In Artificial IntelligenceDr. Robert Krug - Expert In Artificial Intelligence
Dr. Robert Krug - Expert In Artificial Intelligence
Dr. Robert Krug
 
Ann Naser Nabil- Data Scientist Portfolio.pdf
Ann Naser Nabil- Data Scientist Portfolio.pdfAnn Naser Nabil- Data Scientist Portfolio.pdf
Ann Naser Nabil- Data Scientist Portfolio.pdf
আন্ নাসের নাবিল
 
national income & related aggregates (1)(1).pptx
national income & related aggregates (1)(1).pptxnational income & related aggregates (1)(1).pptx
national income & related aggregates (1)(1).pptx
j2492618
 
Day 1 MS Excel Basics #.pptxDay 1 MS Excel Basics #.pptxDay 1 MS Excel Basics...
Day 1 MS Excel Basics #.pptxDay 1 MS Excel Basics #.pptxDay 1 MS Excel Basics...Day 1 MS Excel Basics #.pptxDay 1 MS Excel Basics #.pptxDay 1 MS Excel Basics...
Day 1 MS Excel Basics #.pptxDay 1 MS Excel Basics #.pptxDay 1 MS Excel Basics...
Jayantilal Bhanushali
 
indonesia-gen-z-report-2024 Gen Z (born between 1997 and 2012) is currently t...
indonesia-gen-z-report-2024 Gen Z (born between 1997 and 2012) is currently t...indonesia-gen-z-report-2024 Gen Z (born between 1997 and 2012) is currently t...
indonesia-gen-z-report-2024 Gen Z (born between 1997 and 2012) is currently t...
disnakertransjabarda
 
Time series for yotube_1_data anlysis.pdf
Time series for yotube_1_data anlysis.pdfTime series for yotube_1_data anlysis.pdf
Time series for yotube_1_data anlysis.pdf
asmaamahmoudsaeed
 
2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry
2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry
2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry
bastakwyry
 
Dynamics 365 Business Rules Dynamics Dynamics
Dynamics 365 Business Rules Dynamics DynamicsDynamics 365 Business Rules Dynamics Dynamics
Dynamics 365 Business Rules Dynamics Dynamics
heyoubro69
 
AWS RDS Presentation to make concepts easy.pptx
AWS RDS Presentation to make concepts easy.pptxAWS RDS Presentation to make concepts easy.pptx
AWS RDS Presentation to make concepts easy.pptx
bharatkumarbhojwani
 
Fundamentals of Data Analysis, its types, tools, algorithms
Fundamentals of Data Analysis, its types, tools, algorithmsFundamentals of Data Analysis, its types, tools, algorithms
Fundamentals of Data Analysis, its types, tools, algorithms
priyaiyerkbcsc
 
Sets theories and applications that can used to imporve knowledge
Sets theories and applications that can used to imporve knowledgeSets theories and applications that can used to imporve knowledge
Sets theories and applications that can used to imporve knowledge
saumyasl2020
 
Z14_IBM__APL_by_Christian_Demmer_IBM.pdf
Z14_IBM__APL_by_Christian_Demmer_IBM.pdfZ14_IBM__APL_by_Christian_Demmer_IBM.pdf
Z14_IBM__APL_by_Christian_Demmer_IBM.pdf
Fariborz Seyedloo
 
AI ------------------------------ W1L2.pptx
AI ------------------------------ W1L2.pptxAI ------------------------------ W1L2.pptx
AI ------------------------------ W1L2.pptx
AyeshaJalil6
 
problem solving.presentation slideshow bsc nursing
problem solving.presentation slideshow bsc nursingproblem solving.presentation slideshow bsc nursing
problem solving.presentation slideshow bsc nursing
vishnudathas123
 
录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单
录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单
录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单
Taqyea
 
CS-404 COA COURSE FILE JAN JUN 2025.docx
CS-404 COA COURSE FILE JAN JUN 2025.docxCS-404 COA COURSE FILE JAN JUN 2025.docx
CS-404 COA COURSE FILE JAN JUN 2025.docx
nidarizvitit
 
L1_Slides_Foundational Concepts_508.pptx
L1_Slides_Foundational Concepts_508.pptxL1_Slides_Foundational Concepts_508.pptx
L1_Slides_Foundational Concepts_508.pptx
38NoopurPatel
 
Analysis of Billboards hot 100 toop five hit makers on the chart.docx
Analysis of Billboards hot 100 toop five hit makers on the chart.docxAnalysis of Billboards hot 100 toop five hit makers on the chart.docx
Analysis of Billboards hot 100 toop five hit makers on the chart.docx
hershtara1
 
Dr. Robert Krug - Expert In Artificial Intelligence
Dr. Robert Krug - Expert In Artificial IntelligenceDr. Robert Krug - Expert In Artificial Intelligence
Dr. Robert Krug - Expert In Artificial Intelligence
Dr. Robert Krug
 
national income & related aggregates (1)(1).pptx
national income & related aggregates (1)(1).pptxnational income & related aggregates (1)(1).pptx
national income & related aggregates (1)(1).pptx
j2492618
 
Ad

TREE DATA STRUCTURE SLIDES dsa dsa .pptx

  • 1. TREE Data Structures 1- Linear Data Structures Examples: Arrays, Link List, Stack, Queue 2- Non-Linear Data Structures Examples: Tree, Graphs, etc. Types of Tree a) Natural Tree b) Programming Tree Types of Programming Tree a) Binary Tree b) Binary Search Tree c) AVL/Balance Tree d) Spanning Tree e) 2/3 Tree f) 2/3/4 Tree g) B Tree h) B+ Tree
  • 2. What is a Tree? • General Concepts
  • 3. Tree Descriptions (Important Terms) • Path − Path refers to the sequence of nodes along the edges of a tree. • Root – The node at the top of the tree is called root. There is only one root per tree and one path from the root node to any node. • Parent − Any node except the root node has one edge upward to a node called parent. • Child – The node below a given node connected by its edge downward is called its child node. • Leaf – The node which does not have any child node is called the leaf node. • Subtree − Subtree represents the descendants of a node. • Visiting − Visiting refers to checking the value of a node when control is on the node. • Traversing − Traversing means passing through nodes in a specific order. • Levels − Level of a node represents the generation of a node. If the root node is at level 0, then its next child node is at level 1, its grandchild is at level 2, and so on. • Keys − Key represents a value of a node based on which a search operation is to be carried out for a node.
  • 4. Binary Tree Binary Tree Binary Tree is a tree in which one parent node can have maximum 2 child nodes and minimum 0 child node. a) Strictly BT Strictly Binary Tree is such a binary tree in which one parent node can have maximum 2 child nodes. b) Complete Binary Tree Complete Binary Tree is such a Binary Tree in which all the leaf nodes should be at the same level and at each level there can be maximum 2level nodes. Total number of nodes in complete binary tree = 2d+1 -1, where d represents the depth of tree. • Height of Tree: The total number of levels from root node to leaf node is called height of tree.
  • 5. Complete Binary Tree Representation CBT exhibits a special behavior. A node's left child must have a value less than its parent's value and the node's right child must have a value greater than its parent value.
  • 6. Traversal of Tree Whenever you visit each node of tree then this process is called traversal or tree traversal. Types of Tree Traversal 1- Pre-Ordered Traversal 2- In-Ordered Traversal 3- Post-Ordered Traversal Pre-Ordered Traversal: a) Visit the root node b) Traverse the lefts of sub-tree c) Traverse the rights of sub-tree
  • 7. Traversal of Tree In-Ordered Traversal: a) Traverse the lefts of sub-tree b) Visit the root node c) Traverse the rights of sub-tree Post-Ordered Traversal: a) Traverse the lefts of sub-tree b) Traverse the rights of sub-tree c) Visit the root node
  • 8. Binary Search Tree (BST) Binary Search Tree: A binary tree in which the left child node of a tree or sub-tree has lesser value than its root node but the right child node has greater value than its root node, such a binary tree is called Binary Search Tree or BST. When a BST is traversed in-order and the data item of each node is printed/read, then the data item is printed in ascending order. Advantages of BST: Easy to perform operations i.e. insertion, creating new tree, searching and deletion in BST Example: Construction of BST for the given values, such as 45 30 35 25 60 70 80 55 4 3 30 25 16 10 8 5
  • 9. Structure for Node Creation struct node { int data; struct node *leftChild; struct node *rightChild; };
  • 10. BST Basic Operations • The basic operations that can be performed on a binary search tree data structure, are the following − • Insert ()− Inserts an element in a tree/create a tree. • Search () − Searches an element in a tree. • Delete () – Delete an element in a tree • Pre-order Traversal () − Traverses a tree in a pre-order manner. • In-order Traversal ()− Traverses a tree in an in-order manner. • Post-order Traversal ()− Traverses a tree in a post-order manner.
  • 11. Algorithm for insertion in BST If root is NULL then create root node Return If root exists then compare the data with node.data while until insertion position is located If data is greater than node.data goto right subtree else goto left subtree endwhile insert data end If
  • 12. BST Insertion Implementation class CBST { private: struct node *root; public: CBST(){root==NULL;} void add_CBST_Node(int x); void delete_CBST_Node(int x); void Pre_Order(); void In_Order(); void Post_Order(); }; void CBST :: add_CBST_Node(int x) { struct node *p, *s, *par; p = new node; p -> info = x; p -> left == NULL; p -> right == NULL; if(root == NULL) root = p; else { s = root; while(s != NULL) { par = s; if(x > s -> info) s = s-> right; else s = s -> left; } if(x < par -> info) par -> left = p; else par -> right = p; } }
  • 13. Algorithm for Searching in BST If root.data is equal to search.data return root else while data not found If data is greater than node.data goto right subtree else goto left subtree If data found return node Endwhile return data not found end if
  • 14. Searching in BST struct node* search(int data) { struct node *current = root; printf("Visiting elements: "); while(current->data != data) { if(current != NULL) printf("%d ",current->data); //go to left tree if(current->data > data) { current = current->leftChild; } //else go to right tree else { current = current->rightChild; } //not found if(current == NULL) { return NULL; } return current; } }
  • 15. Deletion in BST Deletion of left most node (Minimum Data Item) s = root; while(s -> left != NULL) { par=s; s=s->left; } par->left=s->right; delete s;
  • 16. Deletion in BST Deletion of Right most node (Maximum Data Item) s = root; while(s -> right != NULL) { par=s; s=s->right; } par->right=s->right; delete s;
  • 17. Binary Search Tree Deletion Cases Different Cases for Deletion node 1- if deleted node has not null in its left or right link 2- if deleted node has left link null 3- if deleted node has right link null
  • 18. BST Deletion Implementation class CBST:: delete_tree_node(int x) { struct node *p, *s, *rp, *f, *par; p=root; par=NULL; while((p!=NULL)&&(p->info!=x)) { par=p; if(x>p->info) p=p->right; else p=p->left; } if (p==NULL) { cout<<“ No Required Node exist Tree”; return; } if(p->left==NULL) rp=p->right; else if(p->right==NULL) rp=p->left; else { f=p; rp=p->right; s=rp->left; while(s!=NULL) { f=rp; rp=s; s=rp->left; } if(f!=p) { f->left=rp->right; rp->right=p->right; } } if(par==NULL) root=rp; if(p==par->right) par->right= rp; else par->left=rp; delete p; }
  • 19. In-order Traversal Until all nodes are traversed − Step 1 − Recursively traverse left subtree. Step 2 − Visit root node. Step 3 − Recursively traverse right subtree. void inorder_traversal(struct node* root) { if(root != NULL) { inorder_traversal(root->leftChild); printf("%d ",root->data); inorder_traversal(root->rightChild);
  • 20. Pre-Order Traversal Until all nodes are traversed − Step 1 − Visit root node. Step 2 − Recursively traverse left subtree. Step 3 − Recursively traverse right subtree. void pre_order_traversal(struct node* root) { if(root != NULL) { printf("%d ",root->data); pre_order_traversal(root->leftChild); pre_order_traversal(root->rightChild); } }
  • 21. Post-Order Traversal Until all nodes are traversed − Step 1 − Recursively traverse left subtree. Step 2 − Recursively traverse right subtree. Step 3 − Visit root node. void post_order_traversal(struct node* root) { if(root != NULL) { post_order_traversal(root->leftChild); post_order_traversal(root->rightChild); printf("%d ", root->data); } }
  • 22. AVL (Adelson Velski and landis)Tree / Balance Tree • Balance Factor: BF of any node= Height of left subtree – Height of right subtree • If BF = (-1, 0, 1) No rotation performed • Height of Tree • Reconstruction of Tree • Left rotation (left rotation is made when balance factor of any tree becomes -2) • Right rotation (right rotation is made when balance factor of any tree becomes +2)
  • 23. Assignments: Question#1: Write a code for Pre-order, Post-order and In-order procedures of CBST to display data in required formats. Question#2: Write a code which will create a CBST and a coy function which will create exact copy of original CBST. Question#3: Write a code for CBST which will create a tree of even nodes and also for odd nodes. Question#4: Write a code which will create AVL Tree and determine its balance factor, and if balance factor is increasing from 2 or -2, then made its right/left rotations.
  翻译: