SlideShare une entreprise Scribd logo
1  sur  34
.Binary Search Trees
Operations and Implementation
Definition
Binary search tree is a binary tree that
satisfies the following constraint.
•The key value of left child is smaller than its
parent key
•the key value of right child is greater than its
parent.
OPERATIONS
Insertion
Deletion
Search
Find Max
Find Min
Insertion
Steps
1.Locate the parent of the node to be inserted
2.create a binary tree node
3.insert the node (attach the node to the
parent)
Do by yourself
Insert the values 13, 3, 4,12, 14,
10, 5, 1, 8, 2, 7, 9,11, 6, 18 in
that order, starting from an
empty tree.
Deletion Operation
Case 1: Node to be deleted has no children
•simply delete the node.
Case 2: Node to be deleted has either left or right
empty subtree
•append nonempty subtree to its grand parent node
Case 3: Node to be deleted has both left and right
subtree
•Find the inorder successor node of the node to be
deleted
•Attach the right subtree of inorder successor node to its
grand parent
•Replace the node to be deleted by its inorder successor
9
7
5
64 8 10
9
7
5
6 8 10
Case 1: removing a node with 2 EMPTY SUBTREES
parent
cursor
Removal in BST: Example
Removing 4
replace the link in the parent with
null
Case 2: removing a node with 2 SUBTREES
9
7
5
6 8 10
9
6
5
8 10
cursor
cursor
- replace the node's value with the max value in the left subtree
- delete the max node in the left subtree
44
Removing 7
Removal in BST: Example
What other element
can be used as
replacement?
9
7
5
6 8 10
9
7
5
6 8 10
cursor
cursor
parent
parent
the node has no left child:
link the parent of the node to the right (non-empty) subtree
Case 3: removing a node with 1 EMPTY SUBTREE
Removal in BST: Example
9
7
5
8 10
9
7
5
8 10
cursor
cursor
parent
parent
the node has no right child:
link the parent of the node to the left (non-empty) subtree
Case 4: removing a node with 1 EMPTY SUBTREE
Removing 5
4 4
Removal in BST: Example
Deletion - Example
20
13 34
2815 36
22 29
332521
3
51
Search Operation
1. Start at the root node
2. Branch either left or right repeatedly until the
element is found
3. if the search ends with empty subtree, the
element is not present in the tree.
Do the following
operations
Delete 4,10, 27,13 from the tree
shown before and show the
step by step results.
IMPLEMENTATION
Definining the node
struct node
{
int key;
node *parent;
node *left;
node *right;
};
Insert operation
void insert_key(int key)
{
//scan through the binary tree
node *prev=NULL, *curr=root;
while (curr != NULL)
{
prev = curr;
if (key < curr->key)
curr = curr->left;
else if (key > curr->key)
curr = curr->right;
else
{
printf("Data Already Exists");
return ;
}
}
//create a node and assign the key
node *newnode = (struct node *)malloc(sizeof(struct node));
newnode->key = key;
//insert the node
if (prev==NULL)
root = newnode;
else
{
if (key < prev->key)
prev->left = newnode;
else
prev->right = newnode;
newnode->parent=prev;
}
}
In-order Traversal
void inorder_traversal(node *ptr)
{
if (ptr !=NULL)
{
inorder_traversal(ptr->left);
printf(" %d", ptr->key);
inorder_traversal(ptr->right);
}
}
Pre-order and Post-order traversal
void preorder_traversal(node *ptr)
{
if (ptr !=NULL)
{
printf(" %d", ptr->key);
preorder_traversal(ptr->left);
preorder_traversal(ptr->right);
}
}
void postorder_traversal(node *ptr)
{
if (ptr !=NULL)
{
postorder_traversal(ptr->left);
postorder_traversal(ptr->right);
Delete Operation
void delete_key(int key)
{
if(root==NULL)
{
printf("nTree is empty");
return;
}
//z -m node to be deleted
//y - succssor node - x successor subtree
node *z=search_key(key);
node *x,*y;
if(z==NULL)
printf("Key not foundn");
else
{
if(z->left==NULL || z->right==NULL)
y=z;
else
y=Tree_Successor(z);
if(y->left!=NULL)
x=y->left;
else
x=y->right;
x->parent=y->parent;
if(y->parent==NULL)
root=x;
else
{
if(y==y->parent->left)
y->parent->left=x;
else
y->parent->right=x;
}
if(y!=z)
z->key=y->key;
free(y);
}
Finding the inorder successor
node* Tree_Successor(node *temp)
{
node *y;
if(temp->right!=NULL)
{
temp=temp->right;
while(temp->left!=NULL)
temp=temp->left;
return temp;
}
else
{
y=temp->parent;
while(y!=NULL && temp==y->right)
{
temp=y;
y=y->parent;
}
return y;
}
}
Search Operation
node* search_key(int key)
{
node *prev=NULL, *curr=root;
while (curr != NULL)
{
prev = curr;
if (key < curr->key)
curr = curr->left;
else if (key > curr->key)
curr = curr->right;
else
{
return curr;//key found
}
}
return NULL;//key not found
}
1. Start with this grid of 12 matchsticks, remove
two of them so that there are only two
squares left.
Answer
3.Move two matchsticks to make only four
identical squares.
Answer
GUIDED READING
ASSESSMENT
1.Which traversal technique traverses the
elements of binary search tree in ascending
order.
A. pre-order traversal
B. post-order traversal
C. in-order traversal
D. converse post-order traversal.
Contd..
2. The height of the binary tree in the best and
worst case is
A. n and log n respectively
B. log n and n respectively
C. log n and n/2 respectively
D. n/2 and log n respectively
Contd..
3. Create a binary search tree using the
following operations: insert 3, insert 7, insert
8, insert 1, insert 5, insert 0, insert 4, insert 6,
insert 9. Then, the left and right child of the
inorder successor of node 5 is
o0 and 6 respectively
o4 and null respectively
o3 and 9 respectively
o4 and 6 respectively
Contd..
4.The data structure used in in-order traversal
is
A. list
B. stack
C. queue
D. linked list
Contd..
5. The successor used in deletion operation of
binary search tree is
A. inorder successor
B. preorder successor
C. postorder successor
D. converse preorder successor

Contenu connexe

En vedette

4.4 hashing ext
4.4 hashing  ext4.4 hashing  ext
4.4 hashing extKrish_ver2
 
3.8 quicksort 04
3.8 quicksort 043.8 quicksort 04
3.8 quicksort 04Krish_ver2
 
3.5 model based clustering
3.5 model based clustering3.5 model based clustering
3.5 model based clusteringKrish_ver2
 
2.2 topological sort 02
2.2 topological sort 022.2 topological sort 02
2.2 topological sort 02Krish_ver2
 
4.4 external hashing
4.4 external hashing4.4 external hashing
4.4 external hashingKrish_ver2
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquerKrish_ver2
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search treeKrish_ver2
 
3.3 shell sort
3.3 shell sort3.3 shell sort
3.3 shell sortKrish_ver2
 
2.3 bayesian classification
2.3 bayesian classification2.3 bayesian classification
2.3 bayesian classificationKrish_ver2
 
4.3 multimedia datamining
4.3 multimedia datamining4.3 multimedia datamining
4.3 multimedia dataminingKrish_ver2
 
4.2 spatial data mining
4.2 spatial data mining4.2 spatial data mining
4.2 spatial data miningKrish_ver2
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03Krish_ver2
 
5.1 mining data streams
5.1 mining data streams5.1 mining data streams
5.1 mining data streamsKrish_ver2
 

En vedette (16)

4.4 hashing ext
4.4 hashing  ext4.4 hashing  ext
4.4 hashing ext
 
4.4 hashing02
4.4 hashing024.4 hashing02
4.4 hashing02
 
3.8 quicksort 04
3.8 quicksort 043.8 quicksort 04
3.8 quicksort 04
 
3.5 model based clustering
3.5 model based clustering3.5 model based clustering
3.5 model based clustering
 
2.2 topological sort 02
2.2 topological sort 022.2 topological sort 02
2.2 topological sort 02
 
4.4 external hashing
4.4 external hashing4.4 external hashing
4.4 external hashing
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
 
1.7 avl tree
1.7 avl tree 1.7 avl tree
1.7 avl tree
 
4.4 hashing
4.4 hashing4.4 hashing
4.4 hashing
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
3.3 shell sort
3.3 shell sort3.3 shell sort
3.3 shell sort
 
2.3 bayesian classification
2.3 bayesian classification2.3 bayesian classification
2.3 bayesian classification
 
4.3 multimedia datamining
4.3 multimedia datamining4.3 multimedia datamining
4.3 multimedia datamining
 
4.2 spatial data mining
4.2 spatial data mining4.2 spatial data mining
4.2 spatial data mining
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03
 
5.1 mining data streams
5.1 mining data streams5.1 mining data streams
5.1 mining data streams
 

Similaire à 1.2 operations of tree representations

Lecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.pptLecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.pptDrBashirMSaad
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary treeKrish_ver2
 
Binary trees
Binary treesBinary trees
Binary treesAmit Vats
 
Binary Search Tree (BST)
Binary Search Tree (BST)Binary Search Tree (BST)
Binary Search Tree (BST)M Sajid R
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Treesagar yadav
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeAdityaK92
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)Trupti Agrawal
 
Lecture_10 - Revised.pptx
Lecture_10 - Revised.pptxLecture_10 - Revised.pptx
Lecture_10 - Revised.pptxRedHeart11
 
5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docx5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docxfredharris32
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeINAM352782
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data StructureMeghaj Mallick
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil duttAnil Dutt
 
8 chapter4 trees_bst
8 chapter4 trees_bst8 chapter4 trees_bst
8 chapter4 trees_bstSSE_AndyLi
 
Biary search Tree.docx
Biary search Tree.docxBiary search Tree.docx
Biary search Tree.docxsowmya koneru
 
Unit14_BinarySearchTree.ppt
Unit14_BinarySearchTree.pptUnit14_BinarySearchTree.ppt
Unit14_BinarySearchTree.pptplagcheck
 

Similaire à 1.2 operations of tree representations (20)

Lecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.pptLecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.ppt
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Binary trees
Binary treesBinary trees
Binary trees
 
Binary Search Tree (BST)
Binary Search Tree (BST)Binary Search Tree (BST)
Binary Search Tree (BST)
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Binary tree
Binary treeBinary tree
Binary tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Binary searchtrees
Binary searchtreesBinary searchtrees
Binary searchtrees
 
Lecture_10 - Revised.pptx
Lecture_10 - Revised.pptxLecture_10 - Revised.pptx
Lecture_10 - Revised.pptx
 
Binary tree
Binary treeBinary tree
Binary tree
 
5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docx5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docx
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
DAA PPT.pptx
DAA PPT.pptxDAA PPT.pptx
DAA PPT.pptx
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
8 chapter4 trees_bst
8 chapter4 trees_bst8 chapter4 trees_bst
8 chapter4 trees_bst
 
Biary search Tree.docx
Biary search Tree.docxBiary search Tree.docx
Biary search Tree.docx
 
Unit14_BinarySearchTree.ppt
Unit14_BinarySearchTree.pptUnit14_BinarySearchTree.ppt
Unit14_BinarySearchTree.ppt
 

Plus de Krish_ver2

5.5 back tracking
5.5 back tracking5.5 back tracking
5.5 back trackingKrish_ver2
 
5.5 back track
5.5 back track5.5 back track
5.5 back trackKrish_ver2
 
5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02Krish_ver2
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructuresKrish_ver2
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructuresKrish_ver2
 
5.3 dynamic programming
5.3 dynamic programming5.3 dynamic programming
5.3 dynamic programmingKrish_ver2
 
5.3 dyn algo-i
5.3 dyn algo-i5.3 dyn algo-i
5.3 dyn algo-iKrish_ver2
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03Krish_ver2
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03Krish_ver2
 
5.1 greedyyy 02
5.1 greedyyy 025.1 greedyyy 02
5.1 greedyyy 02Krish_ver2
 
4.1 sequentioal search
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal searchKrish_ver2
 
3.9 external sorting
3.9 external sorting3.9 external sorting
3.9 external sortingKrish_ver2
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sortKrish_ver2
 

Plus de Krish_ver2 (20)

5.5 back tracking
5.5 back tracking5.5 back tracking
5.5 back tracking
 
5.5 back track
5.5 back track5.5 back track
5.5 back track
 
5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructures
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructures
 
5.3 dynamic programming
5.3 dynamic programming5.3 dynamic programming
5.3 dynamic programming
 
5.3 dyn algo-i
5.3 dyn algo-i5.3 dyn algo-i
5.3 dyn algo-i
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
 
5.1 greedyyy 02
5.1 greedyyy 025.1 greedyyy 02
5.1 greedyyy 02
 
5.1 greedy
5.1 greedy5.1 greedy
5.1 greedy
 
5.1 greedy 03
5.1 greedy 035.1 greedy 03
5.1 greedy 03
 
4.2 bst
4.2 bst4.2 bst
4.2 bst
 
4.2 bst 03
4.2 bst 034.2 bst 03
4.2 bst 03
 
4.2 bst 02
4.2 bst 024.2 bst 02
4.2 bst 02
 
4.1 sequentioal search
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal search
 
3.9 external sorting
3.9 external sorting3.9 external sorting
3.9 external sorting
 
3.8 quicksort
3.8 quicksort3.8 quicksort
3.8 quicksort
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
 
3.7 heap sort
3.7 heap sort3.7 heap sort
3.7 heap sort
 

Dernier

Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
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
 
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
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
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
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
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
 

Dernier (20)

Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
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
 
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
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
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
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
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...
 

1.2 operations of tree representations

  • 1. .Binary Search Trees Operations and Implementation
  • 2. Definition Binary search tree is a binary tree that satisfies the following constraint. •The key value of left child is smaller than its parent key •the key value of right child is greater than its parent.
  • 4. Insertion Steps 1.Locate the parent of the node to be inserted 2.create a binary tree node 3.insert the node (attach the node to the parent)
  • 5. Do by yourself Insert the values 13, 3, 4,12, 14, 10, 5, 1, 8, 2, 7, 9,11, 6, 18 in that order, starting from an empty tree.
  • 6. Deletion Operation Case 1: Node to be deleted has no children •simply delete the node. Case 2: Node to be deleted has either left or right empty subtree •append nonempty subtree to its grand parent node Case 3: Node to be deleted has both left and right subtree •Find the inorder successor node of the node to be deleted •Attach the right subtree of inorder successor node to its grand parent •Replace the node to be deleted by its inorder successor
  • 7. 9 7 5 64 8 10 9 7 5 6 8 10 Case 1: removing a node with 2 EMPTY SUBTREES parent cursor Removal in BST: Example Removing 4 replace the link in the parent with null
  • 8. Case 2: removing a node with 2 SUBTREES 9 7 5 6 8 10 9 6 5 8 10 cursor cursor - replace the node's value with the max value in the left subtree - delete the max node in the left subtree 44 Removing 7 Removal in BST: Example What other element can be used as replacement?
  • 9. 9 7 5 6 8 10 9 7 5 6 8 10 cursor cursor parent parent the node has no left child: link the parent of the node to the right (non-empty) subtree Case 3: removing a node with 1 EMPTY SUBTREE Removal in BST: Example
  • 10. 9 7 5 8 10 9 7 5 8 10 cursor cursor parent parent the node has no right child: link the parent of the node to the left (non-empty) subtree Case 4: removing a node with 1 EMPTY SUBTREE Removing 5 4 4 Removal in BST: Example
  • 11. Deletion - Example 20 13 34 2815 36 22 29 332521 3 51
  • 12. Search Operation 1. Start at the root node 2. Branch either left or right repeatedly until the element is found 3. if the search ends with empty subtree, the element is not present in the tree.
  • 13.
  • 14. Do the following operations Delete 4,10, 27,13 from the tree shown before and show the step by step results.
  • 15. IMPLEMENTATION Definining the node struct node { int key; node *parent; node *left; node *right; };
  • 16. Insert operation void insert_key(int key) { //scan through the binary tree node *prev=NULL, *curr=root; while (curr != NULL) { prev = curr; if (key < curr->key) curr = curr->left; else if (key > curr->key) curr = curr->right; else { printf("Data Already Exists"); return ; } } //create a node and assign the key node *newnode = (struct node *)malloc(sizeof(struct node)); newnode->key = key;
  • 17. //insert the node if (prev==NULL) root = newnode; else { if (key < prev->key) prev->left = newnode; else prev->right = newnode; newnode->parent=prev; } }
  • 18. In-order Traversal void inorder_traversal(node *ptr) { if (ptr !=NULL) { inorder_traversal(ptr->left); printf(" %d", ptr->key); inorder_traversal(ptr->right); } }
  • 19. Pre-order and Post-order traversal void preorder_traversal(node *ptr) { if (ptr !=NULL) { printf(" %d", ptr->key); preorder_traversal(ptr->left); preorder_traversal(ptr->right); } } void postorder_traversal(node *ptr) { if (ptr !=NULL) { postorder_traversal(ptr->left); postorder_traversal(ptr->right);
  • 20. Delete Operation void delete_key(int key) { if(root==NULL) { printf("nTree is empty"); return; } //z -m node to be deleted //y - succssor node - x successor subtree node *z=search_key(key); node *x,*y; if(z==NULL) printf("Key not foundn"); else { if(z->left==NULL || z->right==NULL) y=z; else y=Tree_Successor(z); if(y->left!=NULL) x=y->left; else x=y->right;
  • 22. Finding the inorder successor node* Tree_Successor(node *temp) { node *y; if(temp->right!=NULL) { temp=temp->right; while(temp->left!=NULL) temp=temp->left; return temp; } else { y=temp->parent; while(y!=NULL && temp==y->right) { temp=y; y=y->parent; } return y; } }
  • 23. Search Operation node* search_key(int key) { node *prev=NULL, *curr=root; while (curr != NULL) { prev = curr; if (key < curr->key) curr = curr->left; else if (key > curr->key) curr = curr->right; else { return curr;//key found } } return NULL;//key not found }
  • 24. 1. Start with this grid of 12 matchsticks, remove two of them so that there are only two squares left.
  • 26. 3.Move two matchsticks to make only four identical squares.
  • 29.
  • 30. ASSESSMENT 1.Which traversal technique traverses the elements of binary search tree in ascending order. A. pre-order traversal B. post-order traversal C. in-order traversal D. converse post-order traversal.
  • 31. Contd.. 2. The height of the binary tree in the best and worst case is A. n and log n respectively B. log n and n respectively C. log n and n/2 respectively D. n/2 and log n respectively
  • 32. Contd.. 3. Create a binary search tree using the following operations: insert 3, insert 7, insert 8, insert 1, insert 5, insert 0, insert 4, insert 6, insert 9. Then, the left and right child of the inorder successor of node 5 is o0 and 6 respectively o4 and null respectively o3 and 9 respectively o4 and 6 respectively
  • 33. Contd.. 4.The data structure used in in-order traversal is A. list B. stack C. queue D. linked list
  • 34. Contd.. 5. The successor used in deletion operation of binary search tree is A. inorder successor B. preorder successor C. postorder successor D. converse preorder successor