SlideShare a Scribd company logo
TREE
REPRESENTATION
Tree Representation
• It can be represented in two methods
• List Representation
• Left Child - Right Sibling Representation
List Representation
• In this representation, two types of nodes are used
• Representing the node with data called 'data node' and
• Representing only references called 'reference node'.
• Start with a 'data node' from the root node in the tree.
• Then it is linked to an internal node through a 'reference node'
which is further linked to any other node directly.
• This process repeats for all the nodes in the tree.
Example of list representation
Left Child - Right Sibling Representation
• In this representation, a list with one type of node which
consists of three fields namely
• Data field,
• Left child reference field and
• Right sibling reference field.
• Data field stores the actual value of a node.
• Left reference field stores the address of the left child
otherwise NULL and
• Right reference field stores the address of the right sibling
node otherwise NULL.
Graphical representation of that node is
as follows...
Binary Tree Data structure
• A tree in which every node can have a maximum of
two children is called Binary Tree.
• In a binary tree, every node can have either 0 children or
1 child or 2 children but not more than 2 children.
Types of Binary Trees
• Strictly binary tree
• Complete binary tree
• Extended binary tree.
Strictly Binary Tree
• In a binary tree, every node can have a maximum of two
children.
• But in strictly binary tree, every node should have exactly
two children or none (i.e) every internal node must have
exactly two children. A strictly Binary Tree can be defined
as follows...
• Strictly binary tree is also called as Full Binary Tree or
Proper Binary Tree or 2-Tree
Example
Complete Binary Tree
• In a binary tree, every node can have a maximum of two
children.
• But in strictly binary tree, every node should have exactly two
children or none and
• In complete binary tree all the nodes must have exactly two
children and at every level of complete binary tree there must
be 2level number of nodes.
• For example at level 2 there must be 22 = 4 nodes and at level
3 there must be 23 = 8 nodes.
• A binary tree in which every internal node has exactly two
children and all leaf nodes are at same level is called
Complete Binary Tree.
• Complete binary tree is also called as Perfect Binary Tree.
Example
Extended Binary Tree
• The full binary tree obtained by adding dummy nodes to a
binary tree is called as Extended Binary Tree.
Binary Tree Representations
• A binary tree data structure is represented using two
methods. Those methods are as follows...
• Array Representation
• Linked List Representation
Array Representation of Binary Tree
• In array representation of a binary tree, we use one-
dimensional array (1-D Array) to represent a binary tree.
• To represent a binary tree of depth 'n' using array
representation, need one dimensional array with a
maximum size of 2n + 1.
Linked List Representation of Binary
Tree
• Use a doubly linked list to represent a binary tree.
• In a doubly linked list, every node consists of three fields.
• First field for storing left child address
• Second for storing actual data and
• Third for storing right child address.
Binary tree
Binary Tree Traversals
• To display a binary tree, need to follow some order in
which all the nodes of that binary tree must be displayed.
• In any binary tree, displaying order of nodes depends on
the traversal method.
• Displaying (or) visiting order of nodes in a binary tree
is called as Binary Tree Traversal.
Types of binary tree traversals
• There are three types of binary tree traversals.
• In - Order Traversal
• Pre - Order Traversal
• Post - Order Traversal
In - Order Traversal ( left Child - root –
right Child )
• In-Order traversal, the root node is
visited between the left child and right
child.
• In this traversal, the left child node is
visited first, then the root node is
visited and later visiting the right child
node.
• This in-order traversal is applicable
for every root node of all sub-trees in
the tree.
• This is performed recursively for all
nodes in the tree.
I - D - J - B - F - A - G - K - C - H
Binary tree
Pre - Order Traversal ( root – left Child
– right Child )
• Pre-Order traversal, the root
node is visited before the left
child and right child nodes.
• In this traversal, the root node is
visited first, then its left child and
later its right child.
• This pre-order traversal is
applicable for every root node of
all sub-trees in the tree.
A - B - D - I - J - F - C - G - K - H
Binary tree
Post - Order Traversal ( left Child –
right Child - root )
• In Post-Order traversal, the root
node is visited after left child and
right child. In this traversal, left
child node is visited first, then its
right child and then its root node.
• This is recursively performed until
the right most node is visited.
I - J - D - F - B - K - G - H - C - A
Binary tree
Example 1
Answers
• (a) Inorder (Left, Root, Right) : 4 2 5 1 3
(b) Preorder (Root, Left, Right) : 1 2 4 5 3
(c) Postorder (Left, Right, Root) : 4 5 2 3 1
Example 2
• Preorder = A B D C E G F H I.
• Inorder = B D A G E C H F I.
• Postorder = D B G E H I F C A.
Example 3
Binary tree
Example 4
Answer
Example 5
Binary tree
Binary tree
Binary tree
Binary tree
BINARY SEARCH TREE
Introduction
• Binary Search Tree is a binary tree in which every node
contains only smaller values in its left subtree and only
larger values in its right subtree.
Example
• Construct a Binary Search Tree by inserting the following
sequence of numbers...
10,12,5,4,20,8,7,15 and 13
Binary tree
Binary tree
Binary Search Tree
• Binary Search tree is a binary Tree with following
properties:
• Left sub tree of a node always contains lesser key
• Right sub tree of a node always contains greater key
• Equal valued keys are not allowed
Binary Search Tree Operations
• Commonly performed operations on binary search tree
are-
Search Operation-
• Search Operation is performed to search a particular
element in the Binary Search Tree.
• Rules-
• For searching a given key in the BST,
• Compare the key with the value of root node.
• If the key is present at the root node, then return the root
node.
• If the key is greater than the root node value, then recur
for the root node’s right subtree.
• If the key is smaller than the root node value, then recur
for the root node’s left subtree.
Example-
• Consider key = 45 has to be searched in the given BST
• We start our search from the root node 25.
• As 45 > 25, so we search in 25’s right subtree.
• As 45 < 50, so we search in 50’s left subtree.
• As 45 > 35, so we search in 35’s right subtree.
• As 45 > 44, so we search in 44’s right subtree but 44 has
no subtrees.
• So, we conclude that 45 is not present in the above BST.
Insertion Operation-
• Insertion Operation is performed to insert an element in
the Binary Search Tree.
• Rules-
• The insertion of a new key always takes place as the child
of some leaf node.
• For finding out the suitable leaf node,
• Search the key to be inserted from the root node till some
leaf node is reached.
• Once a leaf node is reached, insert the key as child of that
leaf node.
Example-
• Consider the following example where key = 40 is
inserted in the given BST
Deletion Operation
• Deletion Operation is performed to delete a particular
element from the Binary Search Tree.
• When it comes to deleting a node from the binary search
tree, following three cases are possible
• Case-01: Deletion Of A Node Having No Child (Leaf
Node)-
• Case-02: Deletion Of A Node Having Only One Child-
• Case-03: Deletion Of A Node Having Two Children-
Case-01: Deletion Of A Node Having
No Child (Leaf Node)-
• Just remove / disconnect the leaf node that is to deleted
from the tree.
Case-02: Deletion Of A Node Having
Only One Child-
• Just make the child of the deleting node, the child of its
grandparent.
Case-02: Deletion Of A Node Having
Two Children-
• A node with two children may be deleted from the BST in
the following two ways-
• Method-01:
• Visit to the right sub tree of the deleting node.
• Pluck the least value element called as in-order
successor.
• Replace the deleting element with its in-order successor.
Binary tree
• Method-02:
• Visit to the left sub-tree of the deleting node.
• Pluck the greatest value element called as in-order
predecessor.
• Replace the deleting element with its in-order
predecessor.
Binary tree

More Related Content

PPTX
Trees data structure
PPTX
Binary search tree
PPT
358 33 powerpoint-slides_10-trees_chapter-10
PPT
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
PPTX
Tree
PPTX
Unit – vi tree
PDF
Lecture notes data structures tree
Trees data structure
Binary search tree
358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
Tree
Unit – vi tree
Lecture notes data structures tree

What's hot (20)

PPTX
Unit 6 tree
PPTX
PPTX
Mca iii dfs u-4 tree and graph
PPT
introduction to_trees
PPT
Ch13 Binary Search Tree
PPT
Chapter 8 ds
PDF
Trees, Binary Search Tree, AVL Tree in Data Structures
PPTX
Data structures 3
PPTX
Treesandgraphs
PPTX
Tree - Data Structure
PPTX
Tree data structure
PPT
PPT
Tree
PPT
Lecture 5 trees
PPSX
Data Structure (Tree)
PPT
Binary tree
PPT
Data Structure: TREES
PDF
Difference between complete,ordered,full,strict,perfect and balanced binary tree
PPT
Trees
Unit 6 tree
Mca iii dfs u-4 tree and graph
introduction to_trees
Ch13 Binary Search Tree
Chapter 8 ds
Trees, Binary Search Tree, AVL Tree in Data Structures
Data structures 3
Treesandgraphs
Tree - Data Structure
Tree data structure
Tree
Lecture 5 trees
Data Structure (Tree)
Binary tree
Data Structure: TREES
Difference between complete,ordered,full,strict,perfect and balanced binary tree
Trees
Ad

Similar to Binary tree (20)

PPTX
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
PPTX
Introduction to Tree_Data Structure.pptx
PPTX
learn tree, linked list, queue, stack, and other algo
PPTX
tree-160731205832.pptx
PPTX
Unit 3 trees
PPTX
BASIC TREE AND TYPES OF DI CONCEPTS.pptx
PPTX
Group 5-DSA.pptx........................
PPTX
Search tree,Tree and binary tree and heap tree
PPTX
Creating a Binary tree from a General Tree.pptx
PPT
BINARY SEARCH TREE
PPTX
Presentation1-Data structure S-Tree.pptx
PPTX
Data Structures using Python(generic elective).pptx
PPT
BINARY TREE REPRESENTATION.ppt
PDF
B Tree, Introduction ,example,Splay tree
PPTX
Tree Data Structure in Advanced Data Structure
PPTX
unit-2-data structure and algorithms-tree-2024-1.pptx
PDF
Binary tree
PDF
Trees second part in data structures with examples
PDF
unit-2-dsa-tree introduction of tree and terninology
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
Introduction to Tree_Data Structure.pptx
learn tree, linked list, queue, stack, and other algo
tree-160731205832.pptx
Unit 3 trees
BASIC TREE AND TYPES OF DI CONCEPTS.pptx
Group 5-DSA.pptx........................
Search tree,Tree and binary tree and heap tree
Creating a Binary tree from a General Tree.pptx
BINARY SEARCH TREE
Presentation1-Data structure S-Tree.pptx
Data Structures using Python(generic elective).pptx
BINARY TREE REPRESENTATION.ppt
B Tree, Introduction ,example,Splay tree
Tree Data Structure in Advanced Data Structure
unit-2-data structure and algorithms-tree-2024-1.pptx
Binary tree
Trees second part in data structures with examples
unit-2-dsa-tree introduction of tree and terninology
Ad

Recently uploaded (20)

PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Computing-Curriculum for Schools in Ghana
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Pharma ospi slides which help in ospi learning
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Cell Types and Its function , kingdom of life
PDF
Trump Administration's workforce development strategy
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Classroom Observation Tools for Teachers
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Computing-Curriculum for Schools in Ghana
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Yogi Goddess Pres Conference Studio Updates
A systematic review of self-coping strategies used by university students to ...
Final Presentation General Medicine 03-08-2024.pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Microbial diseases, their pathogenesis and prophylaxis
Pharma ospi slides which help in ospi learning
Module 4: Burden of Disease Tutorial Slides S2 2025
Cell Types and Its function , kingdom of life
Trump Administration's workforce development strategy
VCE English Exam - Section C Student Revision Booklet
Classroom Observation Tools for Teachers
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
GDM (1) (1).pptx small presentation for students
Final Presentation General Medicine 03-08-2024.pptx
O5-L3 Freight Transport Ops (International) V1.pdf

Binary tree

  • 2. Tree Representation • It can be represented in two methods • List Representation • Left Child - Right Sibling Representation
  • 3. List Representation • In this representation, two types of nodes are used • Representing the node with data called 'data node' and • Representing only references called 'reference node'. • Start with a 'data node' from the root node in the tree. • Then it is linked to an internal node through a 'reference node' which is further linked to any other node directly. • This process repeats for all the nodes in the tree.
  • 4. Example of list representation
  • 5. Left Child - Right Sibling Representation • In this representation, a list with one type of node which consists of three fields namely • Data field, • Left child reference field and • Right sibling reference field. • Data field stores the actual value of a node. • Left reference field stores the address of the left child otherwise NULL and • Right reference field stores the address of the right sibling node otherwise NULL.
  • 6. Graphical representation of that node is as follows...
  • 7. Binary Tree Data structure • A tree in which every node can have a maximum of two children is called Binary Tree. • In a binary tree, every node can have either 0 children or 1 child or 2 children but not more than 2 children.
  • 8. Types of Binary Trees • Strictly binary tree • Complete binary tree • Extended binary tree.
  • 9. Strictly Binary Tree • In a binary tree, every node can have a maximum of two children. • But in strictly binary tree, every node should have exactly two children or none (i.e) every internal node must have exactly two children. A strictly Binary Tree can be defined as follows... • Strictly binary tree is also called as Full Binary Tree or Proper Binary Tree or 2-Tree
  • 11. Complete Binary Tree • In a binary tree, every node can have a maximum of two children. • But in strictly binary tree, every node should have exactly two children or none and • In complete binary tree all the nodes must have exactly two children and at every level of complete binary tree there must be 2level number of nodes. • For example at level 2 there must be 22 = 4 nodes and at level 3 there must be 23 = 8 nodes. • A binary tree in which every internal node has exactly two children and all leaf nodes are at same level is called Complete Binary Tree. • Complete binary tree is also called as Perfect Binary Tree.
  • 13. Extended Binary Tree • The full binary tree obtained by adding dummy nodes to a binary tree is called as Extended Binary Tree.
  • 14. Binary Tree Representations • A binary tree data structure is represented using two methods. Those methods are as follows... • Array Representation • Linked List Representation
  • 15. Array Representation of Binary Tree • In array representation of a binary tree, we use one- dimensional array (1-D Array) to represent a binary tree. • To represent a binary tree of depth 'n' using array representation, need one dimensional array with a maximum size of 2n + 1.
  • 16. Linked List Representation of Binary Tree • Use a doubly linked list to represent a binary tree. • In a doubly linked list, every node consists of three fields. • First field for storing left child address • Second for storing actual data and • Third for storing right child address.
  • 18. Binary Tree Traversals • To display a binary tree, need to follow some order in which all the nodes of that binary tree must be displayed. • In any binary tree, displaying order of nodes depends on the traversal method. • Displaying (or) visiting order of nodes in a binary tree is called as Binary Tree Traversal.
  • 19. Types of binary tree traversals • There are three types of binary tree traversals. • In - Order Traversal • Pre - Order Traversal • Post - Order Traversal
  • 20. In - Order Traversal ( left Child - root – right Child ) • In-Order traversal, the root node is visited between the left child and right child. • In this traversal, the left child node is visited first, then the root node is visited and later visiting the right child node. • This in-order traversal is applicable for every root node of all sub-trees in the tree. • This is performed recursively for all nodes in the tree. I - D - J - B - F - A - G - K - C - H
  • 22. Pre - Order Traversal ( root – left Child – right Child ) • Pre-Order traversal, the root node is visited before the left child and right child nodes. • In this traversal, the root node is visited first, then its left child and later its right child. • This pre-order traversal is applicable for every root node of all sub-trees in the tree. A - B - D - I - J - F - C - G - K - H
  • 24. Post - Order Traversal ( left Child – right Child - root ) • In Post-Order traversal, the root node is visited after left child and right child. In this traversal, left child node is visited first, then its right child and then its root node. • This is recursively performed until the right most node is visited. I - J - D - F - B - K - G - H - C - A
  • 27. Answers • (a) Inorder (Left, Root, Right) : 4 2 5 1 3 (b) Preorder (Root, Left, Right) : 1 2 4 5 3 (c) Postorder (Left, Right, Root) : 4 5 2 3 1
  • 29. • Preorder = A B D C E G F H I. • Inorder = B D A G E C H F I. • Postorder = D B G E H I F C A.
  • 40. Introduction • Binary Search Tree is a binary tree in which every node contains only smaller values in its left subtree and only larger values in its right subtree.
  • 41. Example • Construct a Binary Search Tree by inserting the following sequence of numbers... 10,12,5,4,20,8,7,15 and 13
  • 44. Binary Search Tree • Binary Search tree is a binary Tree with following properties: • Left sub tree of a node always contains lesser key • Right sub tree of a node always contains greater key • Equal valued keys are not allowed
  • 45. Binary Search Tree Operations • Commonly performed operations on binary search tree are-
  • 46. Search Operation- • Search Operation is performed to search a particular element in the Binary Search Tree. • Rules- • For searching a given key in the BST, • Compare the key with the value of root node. • If the key is present at the root node, then return the root node. • If the key is greater than the root node value, then recur for the root node’s right subtree. • If the key is smaller than the root node value, then recur for the root node’s left subtree.
  • 47. Example- • Consider key = 45 has to be searched in the given BST
  • 48. • We start our search from the root node 25. • As 45 > 25, so we search in 25’s right subtree. • As 45 < 50, so we search in 50’s left subtree. • As 45 > 35, so we search in 35’s right subtree. • As 45 > 44, so we search in 44’s right subtree but 44 has no subtrees. • So, we conclude that 45 is not present in the above BST.
  • 49. Insertion Operation- • Insertion Operation is performed to insert an element in the Binary Search Tree. • Rules- • The insertion of a new key always takes place as the child of some leaf node. • For finding out the suitable leaf node, • Search the key to be inserted from the root node till some leaf node is reached. • Once a leaf node is reached, insert the key as child of that leaf node.
  • 50. Example- • Consider the following example where key = 40 is inserted in the given BST
  • 51. Deletion Operation • Deletion Operation is performed to delete a particular element from the Binary Search Tree. • When it comes to deleting a node from the binary search tree, following three cases are possible • Case-01: Deletion Of A Node Having No Child (Leaf Node)- • Case-02: Deletion Of A Node Having Only One Child- • Case-03: Deletion Of A Node Having Two Children-
  • 52. Case-01: Deletion Of A Node Having No Child (Leaf Node)- • Just remove / disconnect the leaf node that is to deleted from the tree.
  • 53. Case-02: Deletion Of A Node Having Only One Child- • Just make the child of the deleting node, the child of its grandparent.
  • 54. Case-02: Deletion Of A Node Having Two Children- • A node with two children may be deleted from the BST in the following two ways- • Method-01: • Visit to the right sub tree of the deleting node. • Pluck the least value element called as in-order successor. • Replace the deleting element with its in-order successor.
  • 56. • Method-02: • Visit to the left sub-tree of the deleting node. • Pluck the greatest value element called as in-order predecessor. • Replace the deleting element with its in-order predecessor.