5.2CS2040DETreesv2pdf

.pdf
School
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
Background image
Previous Video: Binary Search Trees2
Background image
BST Operations: Search Minimum3All in left sub-tree < key < all in right sub-tree
Background image
BST Operations: Search Minimum4All in left sub-tree < key < all in right sub-treeDefine base Case: node (t) does not have a left child return the node (t)as minimumRecursively call the search minimum method
Background image
BST Operations: Search Minimum5All 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
Background image
BST Operations: Search Maximum6All in left sub-tree < key < all in right sub-tree
Background image
BST Operations: Search Maximum7All in left sub-tree < key < all in right sub-treeDefine base Case: node (t) does not have a right child return the node (t)as maximumRecursively call the search maximum method
Background image
BST Operations: Search Maximum8All 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
Background image
BST Operations: Search9All in left sub-tree < key < all in right sub-tree
Background image
BST Operations: Search10Designate ‘root’node as “t” nodeIf the queryKey < cur.key, search for the value in the left sub-treeIf the queryKey > cur.key, search for the value in the right sub-tree If the queryKey == cur.key, return the current nodeRepeat the above steps till we find the queryKey or reach the bottom of the tree
Background image
BST Operations: Search11public TreeNode bstSearch(int queryKey){if(queryKey<key){if(leftTree!=null)return leftTree.bstSearch(queryKey);else return null;} else if(queryKey>key){if(rightTree!=null)return rightTree.bstSearch(queryKey);else return null;}else return this}Search BST
Background image
BST Search Examplesearch(74)12t.bstSearch(74)queryKey > keyright!= nullt.right.bstSearch(74)queryKey > keyright!= null t.right.bstSearch(74) queryKey < keyleft!= null t.left.bstSearch(74)queryKey == key return this nodeBinary Tree bstSearch()
Background image
BST Search Examplesearch(32)13t.bstSearch(32)queryKey < keyleft!= nullt.left.bstSearch(32)queryKey > keyright!= null t.right.bstSearch(32) queryKey < keyleft!= null t.left.bstSearch(32)queryKey > key right == null return nullBinary Tree bstSearch()
Background image
BST Operations: Insert14All in left sub-tree < key < all in right sub-tree
Background image
BST Operations: Insert15Designate ‘root’node as “t” nodeIf the insKey < cur.key, insert the value in the left sub-treeIf the insKey > cur.key, insert the value in the right sub-tree If the insKey == cur.key, return as key is already in the treeRepeat the above steps till we insert the new node
Background image
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
Background image
BST Insert Exampleinsert(74)17t.bstInsert(74)queryKey > keyright!= nullt.right.bstInsert(74)queryKey > keyright!= null t.right.bstInsert(74) queryKey < keyleft!= null t.left.bstInsert(74)queryKey == key returnBinary Tree bstSearch()
Background image
BST Insert Exampleinsert(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
Background image
BST Search Analysissearch(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
Background image
BST Search Analysis: Worst Casesearch(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
Background image
Shape of BSTWhat determines the shape of a BST?21
Background image
Shape of BSTWhat determines the shape of a BST?Order of InsertionNumber 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 levelBalanced BSTPerformance depends on shape of BSTInsert keys in a random order balancedBalanced BST if h=O(log N)=c*log(N)22
Background image
BST Search Analysis: Worst Casesearch(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
Background image
BST Insert Analysisinsert(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
Background image
BST Insert Analysis: Worst Caseinsert(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
Background image
Next Video BST Successor Node and Deletion26End of SlidesThis Photoby Unknown Author is licensed under CC BY-SA-NC
Background image