SlideShare a Scribd company logo
TREES-BINARY TREE
REPRESENTATION
B Y
D . S E E T H A L A K S H M I
A S S I S TA N T P R O F E S S O R
D E PA R T M E N T O F C O M P U T E R
A P P L I C AT I O N S
B O N S E C O U R S C O L L E G E F O R
W O M E N
NON-LINEAR DATA STRUCTURE
• These data structures donot have their
elements in a sequence.
• Trees is an example.
• Trees are mainly used to represent data
containing a hierarchical relationship
between elements, ex : records, family
trees and table of contents.
TERMINOLOGY OF TREES
• The boxes on the tree are called nodes
• The nodes immediately below (to the left and right of) a given node
are called its children
• The node immediately above a given node is called its parent
• The (unique) node without a parent is called the root
• A node with no children is called a leaf
• Two children of the same parent are said to be siblings
• A node can be an ancestor (e.g. grandparent) or a descendant
(e.g. great-grandchild)
SOME TREE TERMINOLOGY
• root: node with no parent
– A non-empty tree has exactly one root
• leaf: node with no children
• siblings: two nodes with the same parent.
• path: a sequence of nodes n1, n2, … , nk such that ni is the parent of
ni+1 for 1  i < k
– in other words, a sequence of hops to get from one node to
another
– the length of a path is the number of edges in the path, or 1 less
than the number of nodes in it
DEPTH AND HEIGHT
• depth or level: length of the path from root to the current node
(depth of root = 0)
• height: length of the longest path from root to any leaf
– empty (null) tree's height: -1
– single-element tree's height: 0
– tree with a root with children: 1
TREE EXAMPLE
TREES
• Tree nodes contain two or more links
– All other data structures we have
discussed only contain one
• Binary trees
– All nodes contain two links
• None, one, or both of which may be NULL
– The root node is the first node in a tree.
– Each link in the root node refers to a child
– A node with no children is called a leaf node
BINARY TREES
• Special type of tree in which every node or
vertex has either no children, one child or
two children.
• Characteristics :
• Every binary tree has a root pointer which
points to the start of the tree.
• A binary tree can be empty.
• It consists of a node called root, a left subtree
and right subtree both of which are binary
trees themselves.
EXAMPLES : BINARY TREES
X X
Y Y
Z
(2) (3)
X X
Z
A
B C
(1) (4)
Root of tree is node having info as X.
(1) Only node is root.
(2) Root has left child Y.
(3) Root X has right child Z.
(4) Root X has left child Y and right child Z which
is again a binary tree with its parent as Z and
left child of Z is A, which in turn is parent for
left child B and right child C.
PROPERTIES OF BINARY TREE
• A tree with n nodes has exactly (n-1) edges or
branches.
• In a tree every node except the root has exactly
one parent (and the root node does not have a
parent).
• There is exactly one path connecting any two
nodes in a tree.
• The maximum number of nodes in a binary tree
of height K is 2K+1 -1 where K>=0.
REPRESENTATION OF BINARY TREE
• Array representation
– The root of the tree is
stored in position 0.
– The node in position p, is
the implicit father of nodes
2p+1 and 2p+2.
– Left child is at 2p+1 and
right at 2p+2.
X
Y Z
A
B
0 1 2 3 4 5 6 7 8 10
C
11 12
X Y Z A B C
9
REPRESENTATION OF BINARY TREE
• Linked List
• Every node will consists of information, and two
pointers left and right pointing to the left and right
child nodes.
struct node
{
int data;
struct node *left; struct node *right;
};
• The topmost node or first node is pointed by a root
pointer which will inform the start of the tree. Rest of
the nodes are attached either to left if less than parent
or right if more or equal to parent.
• Diagram of a binary tree
B
A D
C
OPERATIONS ON BINARY TREE
• Searching an existing node.
• Inserting a new node.
• Deleting an existing node.
• Traversing the tree.
• Preorder
• Inorder
• Postorder
SEARCH PROCESS
• Initialize a search pointer as the root pointer.
• All the data is compared with the data stored in each
node of the tree starting from root node.
• If the data to be searched is equal to the data of the
node then print “successful” search.
• Else if the data is less than node’s data move the pointer
to left subtree. Else data is more than node’s data move
the pointer to right subtree.
• Keep moving in the tree until the data is found or search
pointer comes to NULL in which case it is an
“unsuccessful” search.
EXAMPLE USED FOR SEARCH
21
18
19
7
6 9
8 11
14
13
Root
EXAMPLE : SEARCH
• Initialize temp as root pointer which is node having 21.
• Loop until temp!=NULL or ITEM found
• Compare item=9 with the root 21 of the tree, since
9<21, proceed temp to the left child of the tree.
• Compare ITEM=9 with 18 since 9<18 proceed temp
to the left subtree of 18, which is 7.
• Compare ITEM=9 with 7 since 9>7 proceed temp to
right subtree of the node which holds a value 7.
• Now ITEM=9 is compared with the node which holds
value 9 and since 9 is found the searching process
ens here.
INSERT PROCESS
• For node insertion in a binary search tree, initially the
data that is to be inserted is compared with the data of
the root data.
• If the data is found to be greater than or equal to the
data of the root node then the new node is inserted in
the right subtree of the root node, else left subtree.
• Now the parent of the right or left subtree is considered
and its data is compared with the data of the new node
and the same procedure is repeated again until a NULL
is found which will indicate a space when the new node
has to be attached. Thus finally the new node is made
the appropriate child of this current node.
EXAMPLE USED FOR INSERT
38
14
23
8
Root
56
82
45
18 70
20
EXAMPLE : INSERT
• Suppose the ITEM=20 is the data part of the new node
to be inserted in the tree and the root is pointing to the
start of the tree.
• Compare ITEM=20 with the root 38 of the tree. Since 20
< 38 proceed to the left child of 38, which is 14.
• Compare ITEM=20 with 14, since 20 > 14 proceed to the
right child of 14, which is 23.
• Compare ITEM=20 with 23 since 20 < 23 proceed to the
left child of 23 which is 18.
• Compare ITEM=20 with 18 since 20 > 18 and 18 does
not have right child, 10 is inserted as the right child of 18.
• Tree traversals:
– Inorder traversal – prints the node values in ascending order
1. Traverse the left subtree with an inorder traversal
2. Process the value in the node (i.e., print the node value)
3. Traverse the right subtree with an inorder traversal
– Preorder traversal
1. Process the value in the node
2. Traverse the left subtree with a preorder traversal
3. Traverse the right subtree with a preorder traversal
– Postorder traversal
1. Traverse the left subtree with a postorder traversal
2. Traverse the right subtree with a postorder traversal
3. Process the value in the node
BINARY TREE TRAVERSALS
• three common binary tree traversal orderings (each one begins at
the root):
– preorder traversal: the current node is processed, then the
node's left subtree is traversed, then the node's right subtree is
traversed (CURRENT-LEFT-RIGHT)
– in-order traversal: the node's left subtree is traversed, then the
current node itself is processed, then the node's right subtree is
traversed (LEFT-CURRENT-RIGHT)
– postorder traversal: the node's left subtree is traversed, then
the node's right subtree is traversed, and lastly the current node
is processed (LEFT-RIGHT-CURRENT)
BINARY TREE PREORDER
TRAVERSAL
"G"
"F"
• order: C F T B R K G
– The trick: Walk around the outside of the tree and emit a node's
value when you touch the left side of the node.
root
"C"
"T"
"R"
"B"
"K"
BINARY TREE IN-ORDER TRAVERSAL
order: B T R F K C G
– The trick: Walk around the outside of the tree and emit a node's value
when you touch the bottom side of the node.
root
"C"
"F"
"T"
"G"
"R"
"B"
"K"
BINARY TREE POSTORDER
TRAVERSAL
order: B R T K F G C
– The trick: Walk around the outside of the tree and emit a node's value
when you touch the right side of the node
root
"C"
"F"
"T" "G"
"R"
"B"
"K"
DELETE PROCESS
• There are four possible conditions are to be taken into account :
i. No node in the tree holds the specified data.
ii. The node containing the data has no children.
iii. The node containing the data has exactly one child.
iv. The node containing data has two children.
• Condition (i) In this case we simply print the message that the data
item is not present in the tree.
• Condition (ii) In this case since the node to be deleted has no
children the memory occupied by this should be freed and either
the left link or the right link of the parent of this node should be set
to NULL. Which of these is to be set to NULL depends upon
whether the node being deleted is a left child or right child of its
parent.
CONDITION (III)
• In this case since the node to be deleted has one child the solution is to
adjust the pointer of the parent of the node to be deleted such that after
deletion it points to the child of the node being deleted as in the diagram.
18
19
7
6 9
11
10
24
26
25 30
21 21
18
19
7
6 11
10
24
26
25 30
Root Root
CONDITION (IV)
In this case the node to be deleted has two children. Consider node 9 before deletion. The inorder successor of the
node 9 is node 10. The data of this inorder successor should now be copied into the node to be deleted and a pointer
should be set up pointing to the inorder successor (node 10). This inorder successor would always have one or zero
child. It should then be deleted using the same procedure as for deleting one child or a zero child node. Thus, the whole
logic of deleting a node with two children is to locate the inorder successor, copy its data and reduce the problem to a
simple deletion of a node with one or zero child. This is shown in the example.
18
19
7
6 9
11
10
24
26
25 30
8
21 21
18
19
7
6 10
11
Root Root
24
26
25 30
8
Inorder successor of node 9
DELETION
• Condition (i) Print message data not found.
• Condition (ii) Since no children so free the node
& make either parent->left=NULL or parent-
>right=NULL.
A A
B
Parent Parent
left right
C X
In the example2,
parent->right==x so free(x)
and make parent-
>right=NULL.
X
In the example1,
parent->left==x so free(x) and
make parent->left=NULL.
DELETION
• Condition (iii) Adjust parent to point to the child of the deleted node.
Node deleted(X) has only left child :
If(parent->left==x) parent->left=x->left. If(parent->right==x) parent-
>right=x->left.
I.
1)
2)
A A
B
Parent Parent
left right
X
B
C C
X
left
right
DELETION
• Condition (iii) Adjust parent to point to the child of the deleted node.
Node deleted(X) has only right child :
If(parent->left==x) parent->left=x->right. If(parent->right==x) parent-
>right=x->right.
II.
1)
2)
A A
B
Parent Parent
left right
X B
C C
right
X
right
DELETION
• Condition (iv) solution more complex as X has two children,
• Find inorder successor of the node X. Inorder successor of any node will be first go to right of the
node and then from that node keep going left until NULL encountered, that will be the inorder
successor.
• Copy data of inorder successor to node X’s data.
• Place pointer at the inorder successor node. Now the inorder succesor will have zero or one child
only (so the complex problem is reduced to condition (iii)).
• Delete the inorder successor node by using logic of condition (ii) if no children or condition (iiii) if one
right child.
B
Parent A
left
D
right
X
left
C
left
E
E
Parent A
left
D
right
X
left
C
left
E
Here inorder successor E has
no children condition (ii)
DELETION
Parent
X
left
C
left
B
left
E
right
left
G
A
right D
Inorder
Successor
F
right H
Parent
left C
X
left
E
left
E
right
left
G
A
right D
Inorder
Successor
F
right H
Parent A left
E
left right
C D
left
F
left right
G H
Here inorder successor E has
one right child condition (iii)
If(parent->left==x)
parent->left=x->right.
APPLICATION OF TREE
• Expression Tree
• Game Tree

More Related Content

What's hot (20)

PPTX
Threaded Binary Tree
khabbab_h
 
PPTX
Graph in data structure
Abrish06
 
PPTX
Priority Queue in Data Structure
Meghaj Mallick
 
PPSX
Data Structure (Queue)
Adam Mukharil Bachtiar
 
PPTX
Binary Tree Traversal
Dhrumil Panchal
 
PPTX
Hashing
Amar Jukuntla
 
PPTX
Tree in data structure
ghhgj jhgh
 
PPTX
Trees (data structure)
Trupti Agrawal
 
PPT
Binary tree
Vanitha Chandru
 
PPT
Binary Search
kunj desai
 
PDF
Binary tree
Rajendran
 
PPTX
Doubly Linked List
Ninad Mankar
 
PPTX
B and B+ tree
Ashish Arun
 
PDF
Applications of stack
eShikshak
 
PPTX
Binary Tree in Data Structure
Meghaj Mallick
 
PPTX
Tree Traversal
Md. Israil Fakir
 
PPTX
Tree in data structure
Äshïsh Jäïn
 
PPTX
Hashing Technique In Data Structures
SHAKOOR AB
 
PPT
Linked lists
SARITHA REDDY
 
PPTX
Binary search
AparnaKumari31
 
Threaded Binary Tree
khabbab_h
 
Graph in data structure
Abrish06
 
Priority Queue in Data Structure
Meghaj Mallick
 
Data Structure (Queue)
Adam Mukharil Bachtiar
 
Binary Tree Traversal
Dhrumil Panchal
 
Hashing
Amar Jukuntla
 
Tree in data structure
ghhgj jhgh
 
Trees (data structure)
Trupti Agrawal
 
Binary tree
Vanitha Chandru
 
Binary Search
kunj desai
 
Binary tree
Rajendran
 
Doubly Linked List
Ninad Mankar
 
B and B+ tree
Ashish Arun
 
Applications of stack
eShikshak
 
Binary Tree in Data Structure
Meghaj Mallick
 
Tree Traversal
Md. Israil Fakir
 
Tree in data structure
Äshïsh Jäïn
 
Hashing Technique In Data Structures
SHAKOOR AB
 
Linked lists
SARITHA REDDY
 
Binary search
AparnaKumari31
 

Similar to BINARY TREE REPRESENTATION.ppt (20)

PPTX
Binary Search Tree.pptx
RaaviKapoor
 
PDF
Binary tree
Ssankett Negi
 
PPT
Binary trees
Abhishek Khune
 
PPT
Tree and Binary Search tree
Muhazzab Chouhadry
 
PPTX
Data Structures
Nitesh Bichwani
 
PPTX
Introduction to Tree_Data Structure.pptx
PoojariniMitra1
 
PPTX
Tree.pptx
worldchannel
 
PPTX
NON-LINEAR DATA STRUCTURE-TREES.pptx
Rajitha Reddy Alugati
 
PPTX
Tree structure and its definitions with an example
prathwinidevadiga1
 
PPTX
Data Structure in Tree form. .pptx
mhumza1976
 
PPTX
lecture 17,18. .pptx
mhumza1976
 
PPTX
lecture 17,18. .pptx
mhumza1976
 
PPT
Data Structure And Algorithms for Computer Science
ManishShukla712917
 
PPTX
UNIT III Non Linear Data Structures - Trees.pptx
kncetaruna
 
PPT
Unit 3.ppt
JITTAYASHWANTHREDDY
 
PDF
Lecture notes data structures tree
maamir farooq
 
PPTX
trees in data structure
shameen khan
 
PPT
9. TREE Data Structure Non Linear Data Structure
kejika1215
 
PDF
Chapter 7 - Binary Search Tree in the context of DSA.pdf
Dibyesh1
 
PPTX
Trees.pptx
REMEGIUSPRAVEENSAHAY
 
Binary Search Tree.pptx
RaaviKapoor
 
Binary tree
Ssankett Negi
 
Binary trees
Abhishek Khune
 
Tree and Binary Search tree
Muhazzab Chouhadry
 
Data Structures
Nitesh Bichwani
 
Introduction to Tree_Data Structure.pptx
PoojariniMitra1
 
Tree.pptx
worldchannel
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
Rajitha Reddy Alugati
 
Tree structure and its definitions with an example
prathwinidevadiga1
 
Data Structure in Tree form. .pptx
mhumza1976
 
lecture 17,18. .pptx
mhumza1976
 
lecture 17,18. .pptx
mhumza1976
 
Data Structure And Algorithms for Computer Science
ManishShukla712917
 
UNIT III Non Linear Data Structures - Trees.pptx
kncetaruna
 
Lecture notes data structures tree
maamir farooq
 
trees in data structure
shameen khan
 
9. TREE Data Structure Non Linear Data Structure
kejika1215
 
Chapter 7 - Binary Search Tree in the context of DSA.pdf
Dibyesh1
 
Ad

More from SeethaDinesh (20)

PPTX
Input Devices.pptx
SeethaDinesh
 
PPT
Generations of Computers.ppt
SeethaDinesh
 
PPT
Inheritance in java.ppt
SeethaDinesh
 
PPT
Arrays in JAVA.ppt
SeethaDinesh
 
PPTX
PROGRAMMING IN JAVA unit 1.pptx
SeethaDinesh
 
PPTX
Cloud Computing Basics.pptx
SeethaDinesh
 
PPT
unit 5 stack & queue.ppt
SeethaDinesh
 
PPT
Greedy_Backtracking graph coloring.ppt
SeethaDinesh
 
PPT
Structure and Union.ppt
SeethaDinesh
 
DOCX
Shortest Path Problem.docx
SeethaDinesh
 
PPT
NME UNIT II.ppt
SeethaDinesh
 
PDF
DS UNIT 1.pdf
SeethaDinesh
 
PPT
Basics of C.ppt
SeethaDinesh
 
PPTX
chapter 1.pptx
SeethaDinesh
 
PPTX
DW unit 3.pptx
SeethaDinesh
 
PDF
DW unit 2.pdf
SeethaDinesh
 
PDF
DW Unit 1.pdf
SeethaDinesh
 
PPTX
NME WPI UNIt 3.pptx
SeethaDinesh
 
PDF
NME UNIT I & II MATERIAL.pdf
SeethaDinesh
 
PDF
graphtraversals.pdf
SeethaDinesh
 
Input Devices.pptx
SeethaDinesh
 
Generations of Computers.ppt
SeethaDinesh
 
Inheritance in java.ppt
SeethaDinesh
 
Arrays in JAVA.ppt
SeethaDinesh
 
PROGRAMMING IN JAVA unit 1.pptx
SeethaDinesh
 
Cloud Computing Basics.pptx
SeethaDinesh
 
unit 5 stack & queue.ppt
SeethaDinesh
 
Greedy_Backtracking graph coloring.ppt
SeethaDinesh
 
Structure and Union.ppt
SeethaDinesh
 
Shortest Path Problem.docx
SeethaDinesh
 
NME UNIT II.ppt
SeethaDinesh
 
DS UNIT 1.pdf
SeethaDinesh
 
Basics of C.ppt
SeethaDinesh
 
chapter 1.pptx
SeethaDinesh
 
DW unit 3.pptx
SeethaDinesh
 
DW unit 2.pdf
SeethaDinesh
 
DW Unit 1.pdf
SeethaDinesh
 
NME WPI UNIt 3.pptx
SeethaDinesh
 
NME UNIT I & II MATERIAL.pdf
SeethaDinesh
 
graphtraversals.pdf
SeethaDinesh
 
Ad

Recently uploaded (20)

PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
Dimensions of Societal Planning in Commonism
StefanMz
 

BINARY TREE REPRESENTATION.ppt

  • 1. TREES-BINARY TREE REPRESENTATION B Y D . S E E T H A L A K S H M I A S S I S TA N T P R O F E S S O R D E PA R T M E N T O F C O M P U T E R A P P L I C AT I O N S B O N S E C O U R S C O L L E G E F O R W O M E N
  • 2. NON-LINEAR DATA STRUCTURE • These data structures donot have their elements in a sequence. • Trees is an example. • Trees are mainly used to represent data containing a hierarchical relationship between elements, ex : records, family trees and table of contents.
  • 3. TERMINOLOGY OF TREES • The boxes on the tree are called nodes • The nodes immediately below (to the left and right of) a given node are called its children • The node immediately above a given node is called its parent • The (unique) node without a parent is called the root • A node with no children is called a leaf • Two children of the same parent are said to be siblings • A node can be an ancestor (e.g. grandparent) or a descendant (e.g. great-grandchild)
  • 4. SOME TREE TERMINOLOGY • root: node with no parent – A non-empty tree has exactly one root • leaf: node with no children • siblings: two nodes with the same parent. • path: a sequence of nodes n1, n2, … , nk such that ni is the parent of ni+1 for 1  i < k – in other words, a sequence of hops to get from one node to another – the length of a path is the number of edges in the path, or 1 less than the number of nodes in it
  • 5. DEPTH AND HEIGHT • depth or level: length of the path from root to the current node (depth of root = 0) • height: length of the longest path from root to any leaf – empty (null) tree's height: -1 – single-element tree's height: 0 – tree with a root with children: 1
  • 7. TREES • Tree nodes contain two or more links – All other data structures we have discussed only contain one • Binary trees – All nodes contain two links • None, one, or both of which may be NULL – The root node is the first node in a tree. – Each link in the root node refers to a child – A node with no children is called a leaf node
  • 8. BINARY TREES • Special type of tree in which every node or vertex has either no children, one child or two children. • Characteristics : • Every binary tree has a root pointer which points to the start of the tree. • A binary tree can be empty. • It consists of a node called root, a left subtree and right subtree both of which are binary trees themselves.
  • 9. EXAMPLES : BINARY TREES X X Y Y Z (2) (3) X X Z A B C (1) (4) Root of tree is node having info as X. (1) Only node is root. (2) Root has left child Y. (3) Root X has right child Z. (4) Root X has left child Y and right child Z which is again a binary tree with its parent as Z and left child of Z is A, which in turn is parent for left child B and right child C.
  • 10. PROPERTIES OF BINARY TREE • A tree with n nodes has exactly (n-1) edges or branches. • In a tree every node except the root has exactly one parent (and the root node does not have a parent). • There is exactly one path connecting any two nodes in a tree. • The maximum number of nodes in a binary tree of height K is 2K+1 -1 where K>=0.
  • 11. REPRESENTATION OF BINARY TREE • Array representation – The root of the tree is stored in position 0. – The node in position p, is the implicit father of nodes 2p+1 and 2p+2. – Left child is at 2p+1 and right at 2p+2. X Y Z A B 0 1 2 3 4 5 6 7 8 10 C 11 12 X Y Z A B C 9
  • 12. REPRESENTATION OF BINARY TREE • Linked List • Every node will consists of information, and two pointers left and right pointing to the left and right child nodes. struct node { int data; struct node *left; struct node *right; }; • The topmost node or first node is pointed by a root pointer which will inform the start of the tree. Rest of the nodes are attached either to left if less than parent or right if more or equal to parent.
  • 13. • Diagram of a binary tree B A D C
  • 14. OPERATIONS ON BINARY TREE • Searching an existing node. • Inserting a new node. • Deleting an existing node. • Traversing the tree. • Preorder • Inorder • Postorder
  • 15. SEARCH PROCESS • Initialize a search pointer as the root pointer. • All the data is compared with the data stored in each node of the tree starting from root node. • If the data to be searched is equal to the data of the node then print “successful” search. • Else if the data is less than node’s data move the pointer to left subtree. Else data is more than node’s data move the pointer to right subtree. • Keep moving in the tree until the data is found or search pointer comes to NULL in which case it is an “unsuccessful” search.
  • 16. EXAMPLE USED FOR SEARCH 21 18 19 7 6 9 8 11 14 13 Root
  • 17. EXAMPLE : SEARCH • Initialize temp as root pointer which is node having 21. • Loop until temp!=NULL or ITEM found • Compare item=9 with the root 21 of the tree, since 9<21, proceed temp to the left child of the tree. • Compare ITEM=9 with 18 since 9<18 proceed temp to the left subtree of 18, which is 7. • Compare ITEM=9 with 7 since 9>7 proceed temp to right subtree of the node which holds a value 7. • Now ITEM=9 is compared with the node which holds value 9 and since 9 is found the searching process ens here.
  • 18. INSERT PROCESS • For node insertion in a binary search tree, initially the data that is to be inserted is compared with the data of the root data. • If the data is found to be greater than or equal to the data of the root node then the new node is inserted in the right subtree of the root node, else left subtree. • Now the parent of the right or left subtree is considered and its data is compared with the data of the new node and the same procedure is repeated again until a NULL is found which will indicate a space when the new node has to be attached. Thus finally the new node is made the appropriate child of this current node.
  • 19. EXAMPLE USED FOR INSERT 38 14 23 8 Root 56 82 45 18 70 20
  • 20. EXAMPLE : INSERT • Suppose the ITEM=20 is the data part of the new node to be inserted in the tree and the root is pointing to the start of the tree. • Compare ITEM=20 with the root 38 of the tree. Since 20 < 38 proceed to the left child of 38, which is 14. • Compare ITEM=20 with 14, since 20 > 14 proceed to the right child of 14, which is 23. • Compare ITEM=20 with 23 since 20 < 23 proceed to the left child of 23 which is 18. • Compare ITEM=20 with 18 since 20 > 18 and 18 does not have right child, 10 is inserted as the right child of 18.
  • 21. • Tree traversals: – Inorder traversal – prints the node values in ascending order 1. Traverse the left subtree with an inorder traversal 2. Process the value in the node (i.e., print the node value) 3. Traverse the right subtree with an inorder traversal – Preorder traversal 1. Process the value in the node 2. Traverse the left subtree with a preorder traversal 3. Traverse the right subtree with a preorder traversal – Postorder traversal 1. Traverse the left subtree with a postorder traversal 2. Traverse the right subtree with a postorder traversal 3. Process the value in the node
  • 22. BINARY TREE TRAVERSALS • three common binary tree traversal orderings (each one begins at the root): – preorder traversal: the current node is processed, then the node's left subtree is traversed, then the node's right subtree is traversed (CURRENT-LEFT-RIGHT) – in-order traversal: the node's left subtree is traversed, then the current node itself is processed, then the node's right subtree is traversed (LEFT-CURRENT-RIGHT) – postorder traversal: the node's left subtree is traversed, then the node's right subtree is traversed, and lastly the current node is processed (LEFT-RIGHT-CURRENT)
  • 23. BINARY TREE PREORDER TRAVERSAL "G" "F" • order: C F T B R K G – The trick: Walk around the outside of the tree and emit a node's value when you touch the left side of the node. root "C" "T" "R" "B" "K"
  • 24. BINARY TREE IN-ORDER TRAVERSAL order: B T R F K C G – The trick: Walk around the outside of the tree and emit a node's value when you touch the bottom side of the node. root "C" "F" "T" "G" "R" "B" "K"
  • 25. BINARY TREE POSTORDER TRAVERSAL order: B R T K F G C – The trick: Walk around the outside of the tree and emit a node's value when you touch the right side of the node root "C" "F" "T" "G" "R" "B" "K"
  • 26. DELETE PROCESS • There are four possible conditions are to be taken into account : i. No node in the tree holds the specified data. ii. The node containing the data has no children. iii. The node containing the data has exactly one child. iv. The node containing data has two children. • Condition (i) In this case we simply print the message that the data item is not present in the tree. • Condition (ii) In this case since the node to be deleted has no children the memory occupied by this should be freed and either the left link or the right link of the parent of this node should be set to NULL. Which of these is to be set to NULL depends upon whether the node being deleted is a left child or right child of its parent.
  • 27. CONDITION (III) • In this case since the node to be deleted has one child the solution is to adjust the pointer of the parent of the node to be deleted such that after deletion it points to the child of the node being deleted as in the diagram. 18 19 7 6 9 11 10 24 26 25 30 21 21 18 19 7 6 11 10 24 26 25 30 Root Root
  • 28. CONDITION (IV) In this case the node to be deleted has two children. Consider node 9 before deletion. The inorder successor of the node 9 is node 10. The data of this inorder successor should now be copied into the node to be deleted and a pointer should be set up pointing to the inorder successor (node 10). This inorder successor would always have one or zero child. It should then be deleted using the same procedure as for deleting one child or a zero child node. Thus, the whole logic of deleting a node with two children is to locate the inorder successor, copy its data and reduce the problem to a simple deletion of a node with one or zero child. This is shown in the example. 18 19 7 6 9 11 10 24 26 25 30 8 21 21 18 19 7 6 10 11 Root Root 24 26 25 30 8 Inorder successor of node 9
  • 29. DELETION • Condition (i) Print message data not found. • Condition (ii) Since no children so free the node & make either parent->left=NULL or parent- >right=NULL. A A B Parent Parent left right C X In the example2, parent->right==x so free(x) and make parent- >right=NULL. X In the example1, parent->left==x so free(x) and make parent->left=NULL.
  • 30. DELETION • Condition (iii) Adjust parent to point to the child of the deleted node. Node deleted(X) has only left child : If(parent->left==x) parent->left=x->left. If(parent->right==x) parent- >right=x->left. I. 1) 2) A A B Parent Parent left right X B C C X left right
  • 31. DELETION • Condition (iii) Adjust parent to point to the child of the deleted node. Node deleted(X) has only right child : If(parent->left==x) parent->left=x->right. If(parent->right==x) parent- >right=x->right. II. 1) 2) A A B Parent Parent left right X B C C right X right
  • 32. DELETION • Condition (iv) solution more complex as X has two children, • Find inorder successor of the node X. Inorder successor of any node will be first go to right of the node and then from that node keep going left until NULL encountered, that will be the inorder successor. • Copy data of inorder successor to node X’s data. • Place pointer at the inorder successor node. Now the inorder succesor will have zero or one child only (so the complex problem is reduced to condition (iii)). • Delete the inorder successor node by using logic of condition (ii) if no children or condition (iiii) if one right child. B Parent A left D right X left C left E E Parent A left D right X left C left E Here inorder successor E has no children condition (ii)
  • 33. DELETION Parent X left C left B left E right left G A right D Inorder Successor F right H Parent left C X left E left E right left G A right D Inorder Successor F right H Parent A left E left right C D left F left right G H Here inorder successor E has one right child condition (iii) If(parent->left==x) parent->left=x->right.
  • 34. APPLICATION OF TREE • Expression Tree • Game Tree