SlideShare ist ein Scribd-Unternehmen logo
1 von 57
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 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
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
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
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
Example 4
Answer
Example 5
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 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.
• 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

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
 
Tree
TreeTree
Tree
 
Mca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graphMca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graph
 
introduction to_trees
introduction to_treesintroduction to_trees
introduction to_trees
 
Ch13 Binary Search Tree
Ch13 Binary Search TreeCh13 Binary Search Tree
Ch13 Binary Search Tree
 
Trees
TreesTrees
Trees
 
Chapter 8 ds
Chapter 8 dsChapter 8 ds
Chapter 8 ds
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Data structures 3
Data structures 3Data structures 3
Data structures 3
 
Treesandgraphs
TreesandgraphsTreesandgraphs
Treesandgraphs
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Tree data structure
Tree data structureTree data structure
Tree data structure
 
Tree
TreeTree
Tree
 
Tree
TreeTree
Tree
 
Lecture 5 trees
Lecture 5 treesLecture 5 trees
Lecture 5 trees
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREES
 
Difference between complete,ordered,full,strict,perfect and balanced binary tree
Difference between complete,ordered,full,strict,perfect and balanced binary treeDifference between complete,ordered,full,strict,perfect and balanced binary tree
Difference between complete,ordered,full,strict,perfect and balanced binary tree
 
Trees
TreesTrees
Trees
 

Ähnlich wie Binary tree

Ähnlich wie Binary tree (20)

tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
 
Unit 3 trees
Unit 3   treesUnit 3   trees
Unit 3 trees
 
Search tree,Tree and binary tree and heap tree
Search tree,Tree  and binary tree and heap treeSearch tree,Tree  and binary tree and heap tree
Search tree,Tree and binary tree and heap tree
 
Creating a Binary tree from a General Tree.pptx
Creating a Binary tree from a General Tree.pptxCreating a Binary tree from a General Tree.pptx
Creating a Binary tree from a General Tree.pptx
 
C++ UNIT4.pptx
C++ UNIT4.pptxC++ UNIT4.pptx
C++ UNIT4.pptx
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 
Binary tree
Binary tree Binary tree
Binary tree
 
Unit III.ppt
Unit III.pptUnit III.ppt
Unit III.ppt
 
Data structure(Part 2)
Data structure(Part 2)Data structure(Part 2)
Data structure(Part 2)
 
Unit 5 Tree.pptx
Unit 5 Tree.pptxUnit 5 Tree.pptx
Unit 5 Tree.pptx
 
Binary tree
Binary treeBinary tree
Binary tree
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
lecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologylecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminology
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
Binary Search Tree.pptx
Binary Search Tree.pptxBinary Search Tree.pptx
Binary Search Tree.pptx
 
Binary tree
Binary treeBinary tree
Binary tree
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
 
TREES34.pptx
TREES34.pptxTREES34.pptx
TREES34.pptx
 
TERMINOLOGIES OF TREE, TYPES OF TREE.pptx
TERMINOLOGIES OF TREE, TYPES OF TREE.pptxTERMINOLOGIES OF TREE, TYPES OF TREE.pptx
TERMINOLOGIES OF TREE, TYPES OF TREE.pptx
 

Kürzlich hochgeladen

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIShubhangi Sonawane
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesShubhangi Sonawane
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 

Kürzlich hochgeladen (20)

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 

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.
  • 17.
  • 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
  • 21.
  • 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
  • 23.
  • 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
  • 25.
  • 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.
  • 31.
  • 35.
  • 36.
  • 37.
  • 38.
  • 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
  • 42.
  • 43.
  • 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.
  • 55.
  • 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.