National University of Singapore**We aren't endorsed by this school
Course
COMPUTING CS2040DE
Subject
Electrical Engineering
Date
Jan 3, 2025
Pages
26
Uploaded by ColonelShark3704
BST - Search & InsertData Structures and AlgorithmsDr. Sangit SasidharDepartment of Electrical and Computer EngineeringStarting…1https://visualgo.net/en/bst
Previous Video: Binary Search Trees2
BST Operations: Search Minimum3•All in left sub-tree < key < all in right sub-tree
BST Operations: Search Minimum4•All in left sub-tree < key < all in right sub-tree•Define base Case: node (t) does not have a left child →return the node (t)as minimum•Recursively call the search minimum method
BST Operations: Search Minimum5•All in left sub-tree < key < all in right sub-treepublic TreeNode bstMin(){if(leftTree!=null)return leftTree.bstMin();else return this}Search Minimum of a BST
BST Operations: Search Maximum6•All in left sub-tree < key < all in right sub-tree
BST Operations: Search Maximum7•All in left sub-tree < key < all in right sub-tree•Define base Case: node (t) does not have a right child →return the node (t)as maximum•Recursively call the search maximum method
BST Operations: Search Maximum8•All in left sub-tree < key < all in right sub-treepublic TreeNode bstMax(){if(rightTree!=null)return rightTree.bstMax();else return this}Search Maximum of a BST
BST Operations: Search9•All in left sub-tree < key < all in right sub-tree
BST Operations: Search10•Designate ‘root’node as “t” node•If the queryKey < cur.key, search for the value in the left sub-tree•If the queryKey > cur.key, search for the value in the right sub-tree •If the queryKey == cur.key, return the current node•Repeat the above steps till we find the queryKey or reach the bottom of the tree
BST Operations: Insert14•All in left sub-tree < key < all in right sub-tree
BST Operations: Insert15•Designate ‘root’node as “t” node•If the insKey < cur.key, insert the value in the left sub-tree•If the insKey > cur.key, insert the value in the right sub-tree •If the insKey == cur.key, return as key is already in the tree•Repeat the above steps till we insert the new node
BST Operations: Insert16public TreeNode bstInsert(int insKey int intValue){if(insKey<key){if(leftTree!=null)return leftTree.bstInsert(insKey, intValue);else leftTree = new TreeNode(insKey, intValue);} else if(insKey>key){if(rightTree!=null)return rightTree.bstInsert(insKey,intValue);else rightTree = new TreeNode(insKey, intValue); }else return}Insert BST
BST Insert Example•insert(32)18t.bstInsert(32)queryKey < keyleft!= nullt.left.bstInsert(32)queryKey > keyright!= null t.right.bstInsert(32) queryKey < keyleft!= null t.left.bstInsert(32)queryKey > key right == null right = new treeNode(32,intValue)Binary Tree bstSearch()32
BST Search Analysis•search(32)•Number of steps for if else conditions = constant (O(1))•Maximum number of Recursive levels = height of the BST (O(h))•For N nodes →O(h)= O(log N)?19
BST Search Analysis: Worst Case•search(90)•Number of steps for if else conditions = constant (O(1))•Maximum number of Recursive levels = height of the BST (O(h))•For N nodes →O(h)= O(log N)?•Worst Case →O(h)= O(N)•Shape of the BST20517127148490
Shape of BST•What determines the shape of a BST?21
Shape of BST•What determines the shape of a BST?•Order of Insertion•Number of ways to order insertions: N!•For searching,O(h)= O(log N) only if the BST can eliminate half the nodes in each recursive level→Balanced BST•Performance depends on shape of BST•Insert keys in a random order →balanced•Balanced BST if h=O(log N)=c*log(N)22
BST Search Analysis: Worst Case•search(90)•Number of steps for if else conditions = constant (O(1))•Maximum number of Recursive levels = height of the BST (O(h))•For N nodes →O(h)= O(log N)?•Worst Case →O(h)= O(N)•Balanced BST Case →O(h)= O(log N)•Like binary search for an ordered array23517127148490
BST Insert Analysis•insert(32)•Number of steps for if else conditions = constant (O(1))•Maximum number of Recursive levels = height of the BST (O(h))•For N nodes →O(h)= O(log N)?2432
BST Insert Analysis: Worst Case•insert(90)•Number of steps for if else conditions = constant (O(1))•Maximum number of Recursive levels = height of the BST (O(h))•For N nodes →O(h)= O(log N)?•Worst Case →O(h)= O(N)•Balanced BST Case →O(h)= O(log N)•Compared to an ordered array (O(N))25517127148490
Next Video →BST Successor Node and Deletion26End of SlidesThis Photoby Unknown Author is licensed under CC BY-SA-NC