SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Andere mochten auch

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
 

Andere mochten auch (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
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Mehr von 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
 

Mehr von 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
 

Kürzlich hochgeladen

On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
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
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
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
 

Kürzlich hochgeladen (20)

On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
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
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
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
 

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