Download presentation
Presentation is loading. Please wait.
Published byJeffrey Hardy Modified over 9 years ago
1
BST Numbers, Memory, Removal & Tree Maps
2
BST Numbers n = number values contained h = height
3
BST Numbers n = number values contained h = height
4
BST Memory BST manages memory… – Destructor – Copy/Assignment
5
BST Delete Destructor deleteSubtree at root deleteSubtree(node) deleteSubtree on children delete node
6
BST Copy BSTNode* copySubtree(BSTNode* currentNode) If currentNode is nullptr return nullptr BSTNode* newNode = new BSTNode(currentNode->value) newNode->left = copySubtree(currentNode->left) newNode->right= copySubtree(currentNode->right) return newNode
7
BST Copy BSTNode* copySubtree(BSTNode* currentNode) If currentNode is nullptr return nullptr BSTNode* newNode = new BSTNode(currentNode->value) newNode->left = copySubtree(currentNode->left) newNode->right= copySubtree(currentNode->right) return newNode
8
BSTNode* copySubtree(BSTNode* currentNode) If currentNode is nullptr return nullptr BSTNode* newNode = new BSTNode(currentNode->value) newNode->left = copySubtree(currentNode->left) newNode->right= copySubtree(currentNode->right) return newNode BST Copy
9
BSTNode* copySubtree(BSTNode* currentNode) If currentNode is nullptr return nullptr BSTNode* newNode = new BSTNode(currentNode->value) newNode->left = copySubtree(currentNode->left) newNode->right= copySubtree(currentNode->right) return newNode BST Copy
10
BSTNode* copySubtree(BSTNode* currentNode) If currentNode is nullptr return nullptr BSTNode* newNode = new BSTNode(currentNode->value) newNode->left = copySubtree(currentNode->left) newNode->right= copySubtree(currentNode->right) return newNode BST Copy
11
BSTNode* copySubtree(BSTNode* currentNode) If currentNode is nullptr return nullptr BSTNode* newNode = new BSTNode(currentNode->value) newNode->left = copySubtree(currentNode->left) newNode->right= copySubtree(currentNode->right) return newNode BST Copy
12
BSTNode* copySubtree(BSTNode* currentNode) If currentNode is nullptr return nullptr BSTNode* newNode = new BSTNode(currentNode->value) newNode->left = copySubtree(currentNode->left) newNode->right= copySubtree(currentNode->right) return newNode BST Copy
13
BSTNode* copySubtree(BSTNode* currentNode) If currentNode is nullptr return nullptr BSTNode* newNode = new BSTNode(currentNode->value) newNode->left = copySubtree(currentNode->left) newNode->right= copySubtree(currentNode->right) return newNode BST Copy
14
BSTNode* copySubtree(BSTNode* currentNode) If currentNode is nullptr return nullptr BSTNode* newNode = new BSTNode(currentNode->value) newNode->left = copySubtree(currentNode->left) newNode->right= copySubtree(currentNode->right) return newNode BST Copy
15
BSTNode* copySubtree(BSTNode* currentNode) If currentNode is nullptr return nullptr BSTNode* newNode = new BSTNode(currentNode->value) newNode->left = copySubtree(currentNode->left) newNode->right= copySubtree(currentNode->right) return newNode BST Copy
16
BSTNode* copySubtree(BSTNode* currentNode) If currentNode is nullptr return nullptr BSTNode* newNode = new BSTNode(currentNode->value) newNode->left = copySubtree(currentNode->left) newNode->right= copySubtree(currentNode->right) return newNode BST Copy
17
BST Memory Duplicate Copy Constror Assignment Operator
18
BST Removal Case 1: No children
19
BST Removal Case 1: No children Prune from parent and delete
20
BST Removal Case 2: One child
21
BST Removal Case 2: One child Hook parent to the one child
22
BST Removal Case 3: Two Children
23
BST Removal Case 3: Two Children Identify replacement (smallest on right)
24
BST Removal Case 3: Two Children Move value to node being replaced
25
Case 3: Two Children Remove smallest on right BST Removal
26
Recursive Remove: How It Works
27
SmallestValue( startNode ) – Return value of leftmost child of startNode Helpers
28
RemoveSmallestNode( startNode ) – Return pointer to subtree with leftmost node removed Helpers
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.