SlideShare a Scribd company logo
Trees
• A tree is a data structure that is defined as a set of one or more nodes
which allow us to associate a parents child relationship.
• In trees one node is designated as the root node or parent node, and
all the remaining nodes can be partitioned into non-empty sets ,each
of which is a subtree of the root.
• A root node can only have child nodes. On the contrary, leave nodes
or leaves are those that have no children. When there are no nodes in
the tree then the tree is known as a null tree or empty tree.
Binary Search Tree.pptx
Definitions
• Node-A node is a main component of the tree data structure. It stores
the actual data along with the links to the other notes.
• Root- The root node is the topmost node of the tree. It does not have
a parent node. If the root node is empty then the tree is empty
• Parent- The parent of a node is the immediate predecessor of that
node. In the following figure X is the parent of the Y and Z nodes.
• Child- The child nodes are the immediate successors of a node. They
must have a parent node. A child node placed at the left side is called
the left child and similarly a child node placed at the right side is
called the right child.
• Leaf nodes- A leaf node is one which does not have any child nodes.
• Sub trees-The nodes B, X and Y from the left subtree of root A.
similarly the nodes C and Z from the right subtree of A.
Binary search tree
• A binary search tree (BST) is a variant of a binary tree. The special
property of a binary search tree is that all the nodes in the left
subtree have a value less than that of root node. Similarly, all the
nodes in the rights of tree have a value more than that of a root
node.
Operations on binary search tree
• Searching node
• inserting a node
• deleting a node
• determining the height of the binary search tree
• finding the largest node in the binary search tree
• finding the smallest node in the binary search tree
Algorithm for searching a node in the binary
search tree
• Search(Root, Value)
• Step 1: Start
• Step 2: If (Root==Null)
• Return Null
• Print “empty tree”
• Else if(Root—> info==Value)
• Return root
• Else if (root—>info>value)
• Search(root —>Lchild ,value)
• Else if(root—>info<value)
• Search(Root—>Rchild, value)
• Else
• Print “value not found”
• [end of if]———4
• Step 3: end
Example
Inserting a node in the binary search tree
• Insert (Root, Value)
• Step 1:start
• Step 2: if (root==null)
• Allocate memory for root node
• Set root—> info=value
• Set root—>Lchild=Root—>Rchild =null
• [end of if]
• Step 3: if(root—>info >value)
• Insert (root—>Lchild, value)
• Else
• Insert(root—>Rchild,value)
• [end of if]
• Step 4:end
Example
• Insert a new node with the value 7 in the binary search tree.
Binary Search Tree.pptx
Binary Search Tree.pptx
Deleting a node from the binary search tree
Deleting or not having one child
• Delete a node with the value 10
Deleting a note having two children
• For example delete a note with the value 42 from the binary search
tree
Algorithm for deleting node
• Delete_Node(Root,Value)
• Step 1: start
• Step 2: if(root== null)
• Print “error”
• [end of if]
• Step 3: if (root —>info>value)
• Delete_node(root—>Lchild, value)
• Else if (root—>info<value)
• Delete_node(root—>Rchild,value)
• Else
• If(root—> Lchild=null& Root —>Rchild=Null)
• Free(root)
• Else if( root—>Lchild &Root—>Rchild)
• Temp=find_largest(root—>Lchild)
• Or
• Temp=Find_smallest(root—>Rchild)
• Set root—>info=temp—>info
• Free(temp)
• Else
• If(root—>Lchild!=null)
• Set temp=root—>Lchild
• Set root—>info=temp—>info
• Free(temp)
• Set temp=root—>Rchild
• Set root—>info=temp—info
• Free(temp)
Deletion in BST
Delete a leaf node (no children)
Binary Search Tree.pptx
Delete a node
Binary tree traversal methods
• Pre-order traversal
• In order traversal
• Post order
Pre-order traversal
• In pre order traversal ,the following operations are performed
recursively at each node:
• Visit the root node
• Traverse the left subtree
• Traverse the right subtree
Binary Search Tree.pptx
Function for pre-order traversal
• Void pre-order (struct BST * root)
• {
• if root!= Null)
• { printf(“%d”,root->info);
• Pre-order(root->Lchild);
• Pre-order(root->Rchild);
• }
• }
In-order traversal
• In in-order traversal, the following operations are performed
recursively at each node:
• 1. Traverse the left subtree.
• 2. Visit the root node.
• 3. Traverse the right subtree
• Example
Function for an in order traversal
• Void in-order(struct BST*root)
• {if (root !=Null)
• {
• In-order(root->lchild);
• Printf(“%d”, root-info);
• In-order(root->Rchild);
• }
• }
Post order traversal
• In a post order traversal the following operations were performed
recursively at each node:
• 1. Traverse the left subtree
• 2. Traverse the right subtree
• 3. Visit the root node
• Example
Function for post order traversal
• Void post-order(struct BST*root)
• {
• If (root !=Null)
• {
• Post-order(root->lchild)
• Post-order(root->Rchild)
• Printf(“%d”,root->info);
• }
• }
Binary Search Tree.pptx
Binary Search Tree.pptx
Construct binary tree from in order and post
order traversal
• In order H,D,P,L,A,Z,C,E
• Postorder. H,P,L,D,E,C,Z,A
Binary Search Tree.pptx
Binary Search Tree.pptx
• In order H,D,P,L,A,Z,C,E
• Preorder A,D,H,L,P,Z,C,E
Binary Search Tree.pptx
AVL Tree introduction
• In binary search tree
How to calculate balance factor
How to create a AVL tree
• 55,25,65,9,8,15
• BST 8,9,10
• 10, 9,8
• 10,12,11
• 10,8,9
AVL
• 5,7,19,12,10,15,18,20,25,23
Binary Search Tree.pptx
Binary Search Tree.pptx
AVL

More Related Content

PPTX
Introduction to Tree_Data Structure.pptx
PoojariniMitra1
 
PPTX
Search tree,Tree and binary tree and heap tree
zia eagle
 
PPTX
tree-160731205832.pptx
MouDhara1
 
PPTX
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
asimshahzad8611
 
PPTX
BASIC TREE AND TYPES OF DI CONCEPTS.pptx
tpvvsreenivasarao
 
PPTX
learn tree, linked list, queue, stack, and other algo
unknowwqer
 
PPTX
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
DRCARIBOU
 
PPTX
presentation 1 binary search tree in data structures.pptx
TayybaGhaffar1
 
Introduction to Tree_Data Structure.pptx
PoojariniMitra1
 
Search tree,Tree and binary tree and heap tree
zia eagle
 
tree-160731205832.pptx
MouDhara1
 
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
asimshahzad8611
 
BASIC TREE AND TYPES OF DI CONCEPTS.pptx
tpvvsreenivasarao
 
learn tree, linked list, queue, stack, and other algo
unknowwqer
 
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
DRCARIBOU
 
presentation 1 binary search tree in data structures.pptx
TayybaGhaffar1
 

Similar to Binary Search Tree.pptx (20)

PPTX
Binary Search Tree
sagar yadav
 
PPTX
Data structure(Part 2)
Dr. SURBHI SAROHA
 
PDF
Binary tree
Rajendran
 
PPTX
Unit 3 trees
LavanyaJ28
 
PPT
BINARY SEARCH TREE
ER Punit Jain
 
PPTX
Binary search tree
Sana Yameen
 
PDF
Treeeeeeeeeeeeeeeeereeeeeeeeeeeeeeee.pdf
timoemin50
 
PPTX
Binary tree
Afaq Mansoor Khan
 
PPT
Trees
Shankar Bishnoi
 
PPTX
Tree.pptx
worldchannel
 
PPT
Binary Search Tree
AdityaK92
 
PPT
Tree
sbkbca
 
PPT
Tree 11.ppt
DEEPAK948083
 
PPTX
Tree
knightofheart
 
PDF
Data Structure Lecture 3 Linked Lists.pdf
donotreply20
 
PPT
Introduction to data structure by anil dutt
Anil Dutt
 
PPTX
Binary tree
MKalpanaDevi
 
PPT
bst-class-220902051152-cdddddddddddddddddd5e6c70f.ppt
shesnasuneer
 
PPTX
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
sahadevbkbiet2023
 
PPTX
UNIT III Non Linear Data Structures - Trees.pptx
kncetaruna
 
Binary Search Tree
sagar yadav
 
Data structure(Part 2)
Dr. SURBHI SAROHA
 
Binary tree
Rajendran
 
Unit 3 trees
LavanyaJ28
 
BINARY SEARCH TREE
ER Punit Jain
 
Binary search tree
Sana Yameen
 
Treeeeeeeeeeeeeeeeereeeeeeeeeeeeeeee.pdf
timoemin50
 
Binary tree
Afaq Mansoor Khan
 
Tree.pptx
worldchannel
 
Binary Search Tree
AdityaK92
 
Tree
sbkbca
 
Tree 11.ppt
DEEPAK948083
 
Data Structure Lecture 3 Linked Lists.pdf
donotreply20
 
Introduction to data structure by anil dutt
Anil Dutt
 
Binary tree
MKalpanaDevi
 
bst-class-220902051152-cdddddddddddddddddd5e6c70f.ppt
shesnasuneer
 
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
sahadevbkbiet2023
 
UNIT III Non Linear Data Structures - Trees.pptx
kncetaruna
 
Ad

More from RaaviKapoor (10)

PPTX
CENSORSHIP OF MEDIA : PROS & CONS
RaaviKapoor
 
PPTX
B tree ,B plus and graph
RaaviKapoor
 
PPTX
PARTIES AND THE PARTY SYSTEM IN INDIA
RaaviKapoor
 
PPTX
Era of one party dominance
RaaviKapoor
 
PPTX
Politics of planned development
RaaviKapoor
 
PPT
Plant Presentation
RaaviKapoor
 
PPTX
Linked List
RaaviKapoor
 
PPT
Acid Rain
RaaviKapoor
 
PPTX
Instruction Formats
RaaviKapoor
 
PPTX
IT.pptx
RaaviKapoor
 
CENSORSHIP OF MEDIA : PROS & CONS
RaaviKapoor
 
B tree ,B plus and graph
RaaviKapoor
 
PARTIES AND THE PARTY SYSTEM IN INDIA
RaaviKapoor
 
Era of one party dominance
RaaviKapoor
 
Politics of planned development
RaaviKapoor
 
Plant Presentation
RaaviKapoor
 
Linked List
RaaviKapoor
 
Acid Rain
RaaviKapoor
 
Instruction Formats
RaaviKapoor
 
IT.pptx
RaaviKapoor
 
Ad

Recently uploaded (20)

PPTX
MULTI LEVEL DATA TRACKING USING COOJA.pptx
dollysharma12ab
 
PDF
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
PDF
Zero Carbon Building Performance standard
BassemOsman1
 
PDF
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
PDF
Packaging Tips for Stainless Steel Tubes and Pipes
heavymetalsandtubes
 
PPT
Understanding the Key Components and Parts of a Drone System.ppt
Siva Reddy
 
PPTX
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
PDF
Zero carbon Building Design Guidelines V4
BassemOsman1
 
PPTX
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
PDF
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
PDF
AI-Driven IoT-Enabled UAV Inspection Framework for Predictive Maintenance and...
ijcncjournal019
 
PPTX
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
PDF
The Effect of Artifact Removal from EEG Signals on the Detection of Epileptic...
Partho Prosad
 
PDF
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
PDF
top-5-use-cases-for-splunk-security-analytics.pdf
yaghutialireza
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PPTX
Civil Engineering Practices_BY Sh.JP Mishra 23.09.pptx
bineetmishra1990
 
PPTX
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
PPTX
FUNDAMENTALS OF ELECTRIC VEHICLES UNIT-1
MikkiliSuresh
 
MULTI LEVEL DATA TRACKING USING COOJA.pptx
dollysharma12ab
 
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
Zero Carbon Building Performance standard
BassemOsman1
 
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
Packaging Tips for Stainless Steel Tubes and Pipes
heavymetalsandtubes
 
Understanding the Key Components and Parts of a Drone System.ppt
Siva Reddy
 
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
Zero carbon Building Design Guidelines V4
BassemOsman1
 
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
AI-Driven IoT-Enabled UAV Inspection Framework for Predictive Maintenance and...
ijcncjournal019
 
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
The Effect of Artifact Removal from EEG Signals on the Detection of Epileptic...
Partho Prosad
 
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
top-5-use-cases-for-splunk-security-analytics.pdf
yaghutialireza
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
Civil Engineering Practices_BY Sh.JP Mishra 23.09.pptx
bineetmishra1990
 
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
FUNDAMENTALS OF ELECTRIC VEHICLES UNIT-1
MikkiliSuresh
 

Binary Search Tree.pptx

  • 2. • A tree is a data structure that is defined as a set of one or more nodes which allow us to associate a parents child relationship. • In trees one node is designated as the root node or parent node, and all the remaining nodes can be partitioned into non-empty sets ,each of which is a subtree of the root. • A root node can only have child nodes. On the contrary, leave nodes or leaves are those that have no children. When there are no nodes in the tree then the tree is known as a null tree or empty tree.
  • 4. Definitions • Node-A node is a main component of the tree data structure. It stores the actual data along with the links to the other notes.
  • 5. • Root- The root node is the topmost node of the tree. It does not have a parent node. If the root node is empty then the tree is empty • Parent- The parent of a node is the immediate predecessor of that node. In the following figure X is the parent of the Y and Z nodes.
  • 6. • Child- The child nodes are the immediate successors of a node. They must have a parent node. A child node placed at the left side is called the left child and similarly a child node placed at the right side is called the right child.
  • 7. • Leaf nodes- A leaf node is one which does not have any child nodes. • Sub trees-The nodes B, X and Y from the left subtree of root A. similarly the nodes C and Z from the right subtree of A.
  • 8. Binary search tree • A binary search tree (BST) is a variant of a binary tree. The special property of a binary search tree is that all the nodes in the left subtree have a value less than that of root node. Similarly, all the nodes in the rights of tree have a value more than that of a root node.
  • 9. Operations on binary search tree • Searching node • inserting a node • deleting a node • determining the height of the binary search tree • finding the largest node in the binary search tree • finding the smallest node in the binary search tree
  • 10. Algorithm for searching a node in the binary search tree • Search(Root, Value) • Step 1: Start • Step 2: If (Root==Null) • Return Null • Print “empty tree” • Else if(Root—> info==Value) • Return root • Else if (root—>info>value) • Search(root —>Lchild ,value) • Else if(root—>info<value) • Search(Root—>Rchild, value) • Else • Print “value not found” • [end of if]———4 • Step 3: end
  • 12. Inserting a node in the binary search tree • Insert (Root, Value) • Step 1:start • Step 2: if (root==null) • Allocate memory for root node • Set root—> info=value • Set root—>Lchild=Root—>Rchild =null • [end of if] • Step 3: if(root—>info >value) • Insert (root—>Lchild, value) • Else • Insert(root—>Rchild,value) • [end of if] • Step 4:end
  • 13. Example • Insert a new node with the value 7 in the binary search tree.
  • 16. Deleting a node from the binary search tree
  • 17. Deleting or not having one child • Delete a node with the value 10
  • 18. Deleting a note having two children • For example delete a note with the value 42 from the binary search tree
  • 19. Algorithm for deleting node • Delete_Node(Root,Value) • Step 1: start • Step 2: if(root== null) • Print “error” • [end of if] • Step 3: if (root —>info>value) • Delete_node(root—>Lchild, value) • Else if (root—>info<value) • Delete_node(root—>Rchild,value)
  • 20. • Else • If(root—> Lchild=null& Root —>Rchild=Null) • Free(root) • Else if( root—>Lchild &Root—>Rchild) • Temp=find_largest(root—>Lchild) • Or • Temp=Find_smallest(root—>Rchild) • Set root—>info=temp—>info • Free(temp) • Else • If(root—>Lchild!=null) • Set temp=root—>Lchild • Set root—>info=temp—>info • Free(temp) • Set temp=root—>Rchild • Set root—>info=temp—info • Free(temp)
  • 22. Delete a leaf node (no children)
  • 25. Binary tree traversal methods • Pre-order traversal • In order traversal • Post order
  • 26. Pre-order traversal • In pre order traversal ,the following operations are performed recursively at each node: • Visit the root node • Traverse the left subtree • Traverse the right subtree
  • 28. Function for pre-order traversal • Void pre-order (struct BST * root) • { • if root!= Null) • { printf(“%d”,root->info); • Pre-order(root->Lchild); • Pre-order(root->Rchild); • } • }
  • 29. In-order traversal • In in-order traversal, the following operations are performed recursively at each node: • 1. Traverse the left subtree. • 2. Visit the root node. • 3. Traverse the right subtree
  • 31. Function for an in order traversal • Void in-order(struct BST*root) • {if (root !=Null) • { • In-order(root->lchild); • Printf(“%d”, root-info); • In-order(root->Rchild); • } • }
  • 32. Post order traversal • In a post order traversal the following operations were performed recursively at each node: • 1. Traverse the left subtree • 2. Traverse the right subtree • 3. Visit the root node
  • 34. Function for post order traversal • Void post-order(struct BST*root) • { • If (root !=Null) • { • Post-order(root->lchild) • Post-order(root->Rchild) • Printf(“%d”,root->info); • } • }
  • 37. Construct binary tree from in order and post order traversal • In order H,D,P,L,A,Z,C,E • Postorder. H,P,L,D,E,C,Z,A
  • 40. • In order H,D,P,L,A,Z,C,E • Preorder A,D,H,L,P,Z,C,E
  • 42. AVL Tree introduction • In binary search tree
  • 43. How to calculate balance factor
  • 44. How to create a AVL tree • 55,25,65,9,8,15 • BST 8,9,10
  • 51. AVL