SlideShare une entreprise Scribd logo
1  sur  49
PRESENTED BY:
TABISH HAMID
PRIYANKA MEHTA
CONTACT NO: 08376023134
Submitted to:
MISS. RAJ BALA SIMON
TREES
SESSION OUTLINE:
• TREE
• BINARY TREE
• TREES TRAVERSAL
• BINARY SEARCH TREE
• INSERTION AND DELETION IN BINARY SEARCH TREE
• AVL TREE
TREES
• TREE DATA STRUCTURE IS MAINLY USED
TO REPRESENT DATA CONTAINING A
HIERARCHICAL RELATIONSHIP BETWEEN
ELEMENTS .
TREES
• COLLECTION OF NODESOR
FINITE SET OF NODES
• THIS COLLECTION CAN BE EMPTY.
• DEGREE OF A NODE IS NUMBER OF NODES
CONNECTED TO A PARTICULAR NODE.
TREES LEVELS
• A PATH FROM NODE N1 TO NK IS
DEFINED AS A SEQUENCE OF
NODES N1, N2, …….., NK.
• THE LENGTH OF THIS PATH IS THE
NUMBER OF EDGES ON THE PATH.
• THERE IS A PATH OF LENGTH ZERO
FROM EVERY NODE TO ITSELF.
• THERE IS EXACTLY ONE PATH
FROM THE ROOT TO EACH NODE IN
A TREE.
TREES
• HEIGHTOF A NODE IS THE LENGTH OF A
LONGEST PATH FROM THIS NODE TO A LEAF
• ALL LEAVES ARE AT HEIGHT ZERO
• HEIGHT OF A TREE IS THE HEIGHT OF ITS ROOT
(MAXIMUM LEVEL)
TREES
• DEPTHOF A NODE IS THE LENGTH OF PATH
FROM ROOT TO THIS NODE
• ROOT IS AT DEPTH ZERO
• DEPTH OF A TREE IS THE DEPTH OF ITS
DEEPEST LEAF THAT IS EQUAL TO THE
HEIGHT OF THIS TREE
TREES
BINARY TREE
Complete binary treeFull binary tree
FULL BINARY TREE
A binary tree is said to
be full if all its leaves
are at the same level
and every internal node
has two
children.
The full binary tree of
height h has
l = 2h
leaves
and
m = 2h
– 1 internal
nodes.
COMPLETE BINARY TREE
A complete binary tree is
either a full binary tree or one
that is full except for a
segment of missing leaves
on the right side of the
bottom level.
PICTURE OF A BINARY TREE
a
b c
d e
g h i
l
f
j k
TREE TRAVERSALS
• PRE-ORDER
• N L R
• IN-ORDER
• L N R
• POST-ORDER
• L R N
TREE TRAVERSALS
PRE-ORDER(NLR)
1, 3, 5, 9, 6, 8
TREE TRAVERSALS
IN-ORDER(LNR)
5, 3, 9, 1, 8, 6
TREE TRAVERSALS
POST-ORDER(LRN)
5, 9, 3, 8, 6, 1
TREE TRAVERSALS
IN-ORDER TRAVERSAL : 1,2,3,4,5,6,7,8,9
PREORDER TRAVERSAL:1,2,4,7,5,8,3,6,9
ARE GIVEN.
DRAW BINARY TREE FROM THESE TWO
TRAVERSALS?
In:4,7,2,8,5
Pre:2,4,7,5,8
In:6,9,3
Pre:3,6,9
1
7
6
1
2
5
4
8
3
9
REPRESENTATION OF BINARY TREE
i)SEQUENTIAL REPRESENTATION (ARRAYS)
ii)LINKED LIST REPRESENTATION
I) SEQUENTIAL REPRESENTATION
(ARRAYS)
REQUIRED THREE DIFFERENT ARRAYS
1.ARR (CONTAIN ITEMS OF TREE)
2.LC (REPRESENT LEFT CHILD)
3.RC (REPRESENT RIGHT CHILD)
SEQUENTIAL REPRESENTATION
(ARRAYS)
A B C D E ‘0’ F
1 3 -1 -1 -1 -1 -1
2 4 6 -1 -1 -1 -1
arr
lc
rc
LINKED LIST REPRESENTATION
IN LINKED LIST REPRESENTATION
EACH NODE HAS THREE FIELDS:-
DATA PART
ADDRESS OF THE LEFT SUBTREE.
ADDRESS OF THE RIGHT SUBTREE
EXTENDED BINARY TREE
A BINARY TREE CAN BE CONVERTED TO AN EXTENDED BINARY
TREE BY ADDING NEW NODES TO ITS LEAF NODES AND TO
THE NODES THAT HAVE ONLY ONE CHILD. THESE NEW NODES
ARE ADDED IN SUCH A WAY THAT ALL THE NODES IN
RESULTANT TREE HAVE ZERO OR TWO CHILDREN.
IT IS ALSO KNOWN AS 2-TREE.THE NODES OF THE ORIGINAL
TREE ARE CALLED INTERNAL NODES.
NEW NODES THAT ARE ADDED TO BINARY TREE ,TO MAKE IT
AN EXTENDED BINARY TREE ARE CALLED EXTERNAL NODES.
25
.
Binary Tree Extended Binary Tree
Binary Search tree
It is a tree that may be empty.
and non-empty binary Search tree satisfies the
following properties :-
(1)Every element has a key or value and no two
elements have the same key that is all keys are
unique.
(2) The keys if any in the left sub tree of the root
are smaller than the key in the node.
(3) The keys if any in the right sub tree of the root
are larger than the key in the node.
(4) Left and Right sub trees of the root are also
binary search tree.
Example of the Binary Search Tree :-
The input list is
20 17 6 8 10 7 18 13 12 5
20
17
6 18
5
8
7 10
13
12
Insertion in Binary Search Tree
Insert 53 in this binary search tree
Representation of Binary Search Tree :-
struct btreenode
{
struct btreenode *left;
int data;
struct btreenode *right;
};
Function code of Inorder :-
void inorder(struct btreenode *sr)
{
if(sr!=NULL)
{
inorder(sr->left);
printf(“%d”,sr->data);
inorder(sr->right);
}
}
Function code of Preorder :-
void preorder(struct btreenode *sr)
{
if(sr!=NULL)
{
printf(“%d”,sr->data);
preorder(sr->left);
preorder(sr->right);
}
}
Function code of Postorder :-
void postorder(struct btreenode *sr)
{
if(sr!=NULL)
{
postorder(sr->left);
postorder(sr->right);
printf(“%d”,sr->data);
}
}
Function code for Insertion of a node in
binary search tree-
void insert(struct btreenode **sr,int num)
{
if(*sr==NULL)
{
*sr=malloc (sizeof(struct btreenode))
(*sr)->left=NULL;
(*sr)->data=num;
(*sr)->right=NULL;
}
else
{
if(num<(sr)->data)
insert(&((*sr)->left),num);
else
insert(&((*sr)->right),num);
}
}
CASES FOR DELETION
• IF NODE HAS NO SPECIFIC DATA OR NO CHILD
• IF NODE HAS ONLY LEFT CHILD.
• IF NODE HAS ONLY RIGHT CHILD
• IF NODE HAS TWO CHILDREN
CASE 1
• IF NODE TO BE DELETED HAS NO CHILD
IF (( X->LEFT == NULL) && (X->RIGHT == NULL))
{
• IF ( PARENT ->RIGHT == X)
• PARENT -> RIGHT = NULL;
• ELSE
• PARENT -> LEFT = NULL;
• FREE (X);
}
X 14 200 X 15 X
X 18 X
parent
CASE 2
IF NODE TO BE DELETED HAS ONLY RIGHT CHILD
IF (( X-> LEFT==NULL) && (X-> RIGHT !=NULL))
{
• IF (PARENT -> LEFT == X)
• PARENT ->LEFT = X-> RIGHT;
• ELSE
• PARENT ->RIGHT = X-> RIGHT;
• FREE (X);
}
180 14 200 X 15 X
X 16 220 X 18 X
X 17 X
parent
X
220 14 200 X 15 X
X 18 XX 17 X
parent
CASE 3
IF NODE HAS ONLY LEFT CHILD
IF((X->LEFT!=NULL)&&(X->RIGHT==NULL))
{
IF (PARENT->LEFT==X)
PARENT->LEFT=X->LEFT;
ELSE
PARENT->RIGHT->X->LEFT;
FREE(X);
}
180 14 200 X 15 X
X 16 X
220 18 X
X 17 X
parent
X
180 14 220 X 15 X
X 16 220
X 17 X
parent
CASE 4
IF NODE TO BE DELETED HAS TWO CHILDREN
IF((X-> LEFT !=NULL) && (X-> RIGHT!=NULL))
{ PARENT = X;
XSUCC= X->RIGHT;
WHILE (XSUCC->LEFT!=NULL)
{
PARENT = XSUCC;
XSUCC =XSUCC-> LEFT;
}
X->DATA = XSUCC->DATA;
X=XSUCC }
X 15 X 180 14 200
X 16 X
240 18 220
X 17 X X 19 X
parent
X
XSUCC
X 15 X 180 17 200
X 16 X
X 18 220
X 19 X
Data Structure: TREES

Contenu connexe

Tendances

Tendances (20)

trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Terminology of tree
Terminology of treeTerminology of tree
Terminology of tree
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
 
Avl tree
Avl treeAvl tree
Avl tree
 
Tree
TreeTree
Tree
 
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 Structure (Circular Linked List)
Data Structure (Circular Linked List)Data Structure (Circular Linked List)
Data Structure (Circular Linked List)
 
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patil13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10
 
Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operations
 
Queues
QueuesQueues
Queues
 
Threaded binary tree
Threaded binary treeThreaded binary tree
Threaded binary tree
 
B+ trees and height balance tree
B+ trees and height balance treeB+ trees and height balance tree
B+ trees and height balance tree
 
Binary tree
Binary tree Binary tree
Binary tree
 
Tree-In Data Structure
Tree-In Data StructureTree-In Data Structure
Tree-In Data Structure
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
 
Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data Types
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 

En vedette

Graph data structure
Graph data structureGraph data structure
Graph data structure
Tech_MX
 
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data Structure
Anuj Modi
 
Geometric modeling111431635 geometric-modeling-glad (1)
Geometric modeling111431635 geometric-modeling-glad (1)Geometric modeling111431635 geometric-modeling-glad (1)
Geometric modeling111431635 geometric-modeling-glad (1)
manojg1990
 

En vedette (20)

Data structure unitfirst part1
Data structure unitfirst part1Data structure unitfirst part1
Data structure unitfirst part1
 
Entity relationship modeling
Entity relationship modelingEntity relationship modeling
Entity relationship modeling
 
Data mining 1
Data mining 1Data mining 1
Data mining 1
 
datamodel_vector
datamodel_vectordatamodel_vector
datamodel_vector
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 
Graphs data Structure
Graphs data StructureGraphs data Structure
Graphs data Structure
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Radix sort presentation
Radix sort presentationRadix sort presentation
Radix sort presentation
 
Knowledgebase vs Database
Knowledgebase vs DatabaseKnowledgebase vs Database
Knowledgebase vs Database
 
Problem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsProblem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - Graphs
 
B Trees
B TreesB Trees
B Trees
 
Graph data structure
Graph data structureGraph data structure
Graph data structure
 
Data structures
Data structuresData structures
Data structures
 
Dbms mca-section a
Dbms mca-section aDbms mca-section a
Dbms mca-section a
 
B tree
B treeB tree
B tree
 
Radix sorting
Radix sortingRadix sorting
Radix sorting
 
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data Structure
 
Geometric modeling111431635 geometric-modeling-glad (1)
Geometric modeling111431635 geometric-modeling-glad (1)Geometric modeling111431635 geometric-modeling-glad (1)
Geometric modeling111431635 geometric-modeling-glad (1)
 
Files Vs DataBase
Files Vs DataBaseFiles Vs DataBase
Files Vs DataBase
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 

Similaire à Data Structure: TREES

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
KamranAli649587
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
MouDhara1
 

Similaire à Data Structure: TREES (20)

BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Unit 3 dsuc
Unit 3 dsucUnit 3 dsuc
Unit 3 dsuc
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
LEC 5-DS ALGO(updated).pdf
LEC 5-DS  ALGO(updated).pdfLEC 5-DS  ALGO(updated).pdf
LEC 5-DS ALGO(updated).pdf
 
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-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
 
Binary tree
Binary treeBinary tree
Binary tree
 
Algorithms summary (English version)
Algorithms summary (English version)Algorithms summary (English version)
Algorithms summary (English version)
 
DSA-Unit-2.pptx
DSA-Unit-2.pptxDSA-Unit-2.pptx
DSA-Unit-2.pptx
 
Trees
TreesTrees
Trees
 
Red black trees
Red black treesRed black trees
Red black trees
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
Tree
TreeTree
Tree
 
6_1 (1).ppt
6_1 (1).ppt6_1 (1).ppt
6_1 (1).ppt
 
Unit 3 trees
Unit 3   treesUnit 3   trees
Unit 3 trees
 
Tree chapter
Tree chapterTree chapter
Tree chapter
 
tutorial-tree (3).ppt
tutorial-tree (3).ppttutorial-tree (3).ppt
tutorial-tree (3).ppt
 
Tree
TreeTree
Tree
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 

Dernier

notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
MayuraD1
 

Dernier (20)

Learn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic MarksLearn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic Marks
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Bridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptxBridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptx
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 

Data Structure: TREES

  • 1. PRESENTED BY: TABISH HAMID PRIYANKA MEHTA CONTACT NO: 08376023134 Submitted to: MISS. RAJ BALA SIMON
  • 2. TREES SESSION OUTLINE: • TREE • BINARY TREE • TREES TRAVERSAL • BINARY SEARCH TREE • INSERTION AND DELETION IN BINARY SEARCH TREE • AVL TREE
  • 3. TREES • TREE DATA STRUCTURE IS MAINLY USED TO REPRESENT DATA CONTAINING A HIERARCHICAL RELATIONSHIP BETWEEN ELEMENTS .
  • 4. TREES • COLLECTION OF NODESOR FINITE SET OF NODES • THIS COLLECTION CAN BE EMPTY. • DEGREE OF A NODE IS NUMBER OF NODES CONNECTED TO A PARTICULAR NODE.
  • 6. • A PATH FROM NODE N1 TO NK IS DEFINED AS A SEQUENCE OF NODES N1, N2, …….., NK. • THE LENGTH OF THIS PATH IS THE NUMBER OF EDGES ON THE PATH. • THERE IS A PATH OF LENGTH ZERO FROM EVERY NODE TO ITSELF. • THERE IS EXACTLY ONE PATH FROM THE ROOT TO EACH NODE IN A TREE. TREES
  • 7. • HEIGHTOF A NODE IS THE LENGTH OF A LONGEST PATH FROM THIS NODE TO A LEAF • ALL LEAVES ARE AT HEIGHT ZERO • HEIGHT OF A TREE IS THE HEIGHT OF ITS ROOT (MAXIMUM LEVEL) TREES
  • 8. • DEPTHOF A NODE IS THE LENGTH OF PATH FROM ROOT TO THIS NODE • ROOT IS AT DEPTH ZERO • DEPTH OF A TREE IS THE DEPTH OF ITS DEEPEST LEAF THAT IS EQUAL TO THE HEIGHT OF THIS TREE TREES
  • 9. BINARY TREE Complete binary treeFull binary tree
  • 10. FULL BINARY TREE A binary tree is said to be full if all its leaves are at the same level and every internal node has two children. The full binary tree of height h has l = 2h leaves and m = 2h – 1 internal nodes.
  • 11. COMPLETE BINARY TREE A complete binary tree is either a full binary tree or one that is full except for a segment of missing leaves on the right side of the bottom level.
  • 12. PICTURE OF A BINARY TREE a b c d e g h i l f j k
  • 13. TREE TRAVERSALS • PRE-ORDER • N L R • IN-ORDER • L N R • POST-ORDER • L R N
  • 17. TREE TRAVERSALS IN-ORDER TRAVERSAL : 1,2,3,4,5,6,7,8,9 PREORDER TRAVERSAL:1,2,4,7,5,8,3,6,9 ARE GIVEN. DRAW BINARY TREE FROM THESE TWO TRAVERSALS?
  • 20. REPRESENTATION OF BINARY TREE i)SEQUENTIAL REPRESENTATION (ARRAYS) ii)LINKED LIST REPRESENTATION
  • 21. I) SEQUENTIAL REPRESENTATION (ARRAYS) REQUIRED THREE DIFFERENT ARRAYS 1.ARR (CONTAIN ITEMS OF TREE) 2.LC (REPRESENT LEFT CHILD) 3.RC (REPRESENT RIGHT CHILD)
  • 22. SEQUENTIAL REPRESENTATION (ARRAYS) A B C D E ‘0’ F 1 3 -1 -1 -1 -1 -1 2 4 6 -1 -1 -1 -1 arr lc rc
  • 24. IN LINKED LIST REPRESENTATION EACH NODE HAS THREE FIELDS:- DATA PART ADDRESS OF THE LEFT SUBTREE. ADDRESS OF THE RIGHT SUBTREE
  • 25. EXTENDED BINARY TREE A BINARY TREE CAN BE CONVERTED TO AN EXTENDED BINARY TREE BY ADDING NEW NODES TO ITS LEAF NODES AND TO THE NODES THAT HAVE ONLY ONE CHILD. THESE NEW NODES ARE ADDED IN SUCH A WAY THAT ALL THE NODES IN RESULTANT TREE HAVE ZERO OR TWO CHILDREN. IT IS ALSO KNOWN AS 2-TREE.THE NODES OF THE ORIGINAL TREE ARE CALLED INTERNAL NODES. NEW NODES THAT ARE ADDED TO BINARY TREE ,TO MAKE IT AN EXTENDED BINARY TREE ARE CALLED EXTERNAL NODES. 25
  • 26. . Binary Tree Extended Binary Tree
  • 27. Binary Search tree It is a tree that may be empty. and non-empty binary Search tree satisfies the following properties :- (1)Every element has a key or value and no two elements have the same key that is all keys are unique. (2) The keys if any in the left sub tree of the root are smaller than the key in the node. (3) The keys if any in the right sub tree of the root are larger than the key in the node. (4) Left and Right sub trees of the root are also binary search tree.
  • 28. Example of the Binary Search Tree :- The input list is 20 17 6 8 10 7 18 13 12 5 20 17 6 18 5 8 7 10 13 12
  • 29. Insertion in Binary Search Tree Insert 53 in this binary search tree
  • 30. Representation of Binary Search Tree :- struct btreenode { struct btreenode *left; int data; struct btreenode *right; };
  • 31. Function code of Inorder :- void inorder(struct btreenode *sr) { if(sr!=NULL) { inorder(sr->left); printf(“%d”,sr->data); inorder(sr->right); } }
  • 32. Function code of Preorder :- void preorder(struct btreenode *sr) { if(sr!=NULL) { printf(“%d”,sr->data); preorder(sr->left); preorder(sr->right); } }
  • 33. Function code of Postorder :- void postorder(struct btreenode *sr) { if(sr!=NULL) { postorder(sr->left); postorder(sr->right); printf(“%d”,sr->data); } }
  • 34. Function code for Insertion of a node in binary search tree- void insert(struct btreenode **sr,int num) { if(*sr==NULL) { *sr=malloc (sizeof(struct btreenode)) (*sr)->left=NULL; (*sr)->data=num; (*sr)->right=NULL; }
  • 36. CASES FOR DELETION • IF NODE HAS NO SPECIFIC DATA OR NO CHILD • IF NODE HAS ONLY LEFT CHILD. • IF NODE HAS ONLY RIGHT CHILD • IF NODE HAS TWO CHILDREN
  • 37. CASE 1 • IF NODE TO BE DELETED HAS NO CHILD IF (( X->LEFT == NULL) && (X->RIGHT == NULL)) { • IF ( PARENT ->RIGHT == X) • PARENT -> RIGHT = NULL; • ELSE • PARENT -> LEFT = NULL; • FREE (X); }
  • 38.
  • 39. X 14 200 X 15 X X 18 X parent
  • 40. CASE 2 IF NODE TO BE DELETED HAS ONLY RIGHT CHILD IF (( X-> LEFT==NULL) && (X-> RIGHT !=NULL)) { • IF (PARENT -> LEFT == X) • PARENT ->LEFT = X-> RIGHT; • ELSE • PARENT ->RIGHT = X-> RIGHT; • FREE (X); }
  • 41. 180 14 200 X 15 X X 16 220 X 18 X X 17 X parent X
  • 42. 220 14 200 X 15 X X 18 XX 17 X parent
  • 43. CASE 3 IF NODE HAS ONLY LEFT CHILD IF((X->LEFT!=NULL)&&(X->RIGHT==NULL)) { IF (PARENT->LEFT==X) PARENT->LEFT=X->LEFT; ELSE PARENT->RIGHT->X->LEFT; FREE(X); }
  • 44. 180 14 200 X 15 X X 16 X 220 18 X X 17 X parent X
  • 45. 180 14 220 X 15 X X 16 220 X 17 X parent
  • 46. CASE 4 IF NODE TO BE DELETED HAS TWO CHILDREN IF((X-> LEFT !=NULL) && (X-> RIGHT!=NULL)) { PARENT = X; XSUCC= X->RIGHT; WHILE (XSUCC->LEFT!=NULL) { PARENT = XSUCC; XSUCC =XSUCC-> LEFT; } X->DATA = XSUCC->DATA; X=XSUCC }
  • 47. X 15 X 180 14 200 X 16 X 240 18 220 X 17 X X 19 X parent X XSUCC
  • 48. X 15 X 180 17 200 X 16 X X 18 220 X 19 X