Binary Search Tree Binary Search Tree BST Binary
Binary Search Tree
Binary Search Tree BST Binary Search Tree is a binary tree in which elements having lesser values than root are in LST and having greater or equal values are in RST. insert. BST() find. Largest. BST() find. Smallest. BST() find. Largest. LST() find. Smallest. RST() delete. BST() 3/10/2021 Department of Information Technology , TCE 2
Binary Search Tree BST struct tree insert. BST( int x, struct tree *T ) { { int element; if( T == NULL ) struct tree *left; { /* Create and return a one-node tree */ struct tree * right; T = allocate memory; }; if( T == NULL ) fatal_error("Out of space!!!"); else { T->element = x; T->left = T->right = NULL; } } else if( x < T->element ) T->left = insert. BST( x, T->left ); else if( x >= T->element ) T->right = insert. BST( x, T->right ); /* else x is in the tree already. We'll do nothing */ return T; Department of Information Technology , TCE 3 } 3/10/2021
Binary Search Tree BST find. BST( int x, struct tree *T) { if( T == NULL ) return NULL; if( x < T->element ) return( find. BST( x, T->left ) ); else if( x > T->element ) return( find. BST( x, T->right ) ); else return T; } 3/10/2021 Department of Information Technology , TCE struct tree { int element; struct tree *left; struct tree * right; }; 4
Binary Search Tree BST find_min( struct tree *t ) { if( T == NULL ) return NULL; else if( T->left == NULL ) return( T ); else return( find_min ( T->left ) ); } 3/10/2021 Department of Information Technology , TCE struct tree { int element; struct tree *left; struct tree * right; }; 5
Exercises 1. Write procedure to find the largest element in BST. 2. Write procedure to find the largest element in LST of BST. 3. Write procedure to find the smallest element in RST of BST. 4. Write non-recursive procedures for above questions. 3/10/2021 Department of Information Technology , TCE 6
Recap – insert. BST Insert 13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18 in that order, starting from empty BST. 3/10/2021 Department of Information Technology , TCE 7
Binary Search Tree - delete There are three possible cases to consider: • Deleting a node with no children: simply remove the node from the tree. • Deleting a node with one child: remove the node and replace it with its child. • Deleting a node with two children: - call the node to be deleted N. Do not delete N. - instead, choose either largest element in LST or smallest element in RST, namely R. - copy the value of R to N, then recursively call delete on R until reaching one of the first two cases. 3/10/2021 Department of Information Technology , TCE 8
Binary Search Tree - delete 3/10/2021 Department of Information Technology , TCE 9
Binary Search Tree - delete 3/10/2021 Department of Information Technology , TCE 10
Quiz 3/10/2021 Department of Information Technology , TCE 11
Quiz 3/10/2021 Department of Information Technology , TCE 12
Quiz Method 2 Method 1 3/10/2021 Department of Information Technology , TCE 13
Binary Search - Delete struct tree delete. BST(int x, struct tree *T ) { { int element; struct tree *tmp_cell, *child; struct tree *left; if( T == NULL ) error("Element not found"); struct tree *right; else }; if( x < T->element ) T->left = delete. BST( x, T->left ); else if( x > T->element ) T->right = delete. BST( x, T->right ); else if( T->left && T->right ) /* Two children */ { tmp_cell = find_min( T->right ); T->element = tmp_cell->element; T->right = delete( T->element, T->right ); } else /* One child */ { tmp_cell = T; if( T->left == NULL ) child = T->right; if( T->right == NULL ) child = T->left; free( tmp_cell ); return child; } return T; 3/10/2021 Department of Information Technology , TCE 14 }
Exercises 1. Insert Jan to Dec items one by one into an empty BST. 2. Insert Sun to Sat items one by one into an empty BST. Sun Mon 3. Find k-th smallest element in BST. Given root of binary search tree and K as input, find K-th smallest element in BST. For example, in the following BST, if k = 3, then output should be 10, and if k = 5, then output should be 14. 3/10/2021 Department of Information Technology , TCE 15
Time Complexity 3/10/2021 Department of Information Technology , TCE 16
References http: //www. algolist. net/Data_structures/Binary_search_tree/Removal http: //www. geeksforgeeks. org/ 3/10/2021 Department of Information Technology , TCE 17
- Slides: 17