BST Numbers, Memory, Removal & Tree Maps. BST Numbers n = number values contained h = height.
-
Upload
jeffrey-hardy -
Category
Documents
-
view
227 -
download
1
description
Transcript of BST Numbers, Memory, Removal & Tree Maps. BST Numbers n = number values contained h = height.
BST Numbers, Memory, Removal & Tree Maps
BST Numbers
Ideal binary tree:• Leaf nodes dominate :
max number leaves =
• Height directly related to n:n = 2(h+1) – 1
• Height = Height related to log of tree size
n = number values containedh = height
BST Numbers
Ideal binary tree:• Leaf nodes dominate :
max number leaves =
• Height directly related to n:n = 2(h+1) – 1
• Height = Height related to log of tree sizeAlgorithm that descends one path doing constant work at each node is O(logn)
n = number values containedh = height
BST Memory
• BST manages memory…– Destructor– Copy/Assignment
BST DeleteDestructor
deleteSubtree at root
deleteSubtree(node)deleteSubtree on childrendelete node
BST CopyBSTNode* copySubtree(BSTNode* currentNode)
If currentNode is nullptrreturn nullptr
BSTNode* newNode = new BSTNode(currentNode->value)newNode->left = copySubtree(currentNode->left) newNode->right= copySubtree(currentNode->right)return newNode
BST CopyBSTNode* copySubtree(BSTNode* currentNode)
If currentNode is nullptrreturn nullptr
BSTNode* newNode = new BSTNode(currentNode->value)newNode->left = copySubtree(currentNode->left) newNode->right= copySubtree(currentNode->right)return newNode
BSTNode* copySubtree(BSTNode* currentNode)If currentNode is nullptr
return nullptrBSTNode* newNode = new BSTNode(currentNode->value)newNode->left = copySubtree(currentNode->left) newNode->right= copySubtree(currentNode->right)return newNode
BST Copy
BSTNode* copySubtree(BSTNode* currentNode)If currentNode is nullptr
return nullptrBSTNode* newNode = new BSTNode(currentNode->value)newNode->left = copySubtree(currentNode->left) newNode->right= copySubtree(currentNode->right)return newNode
BST Copy
BSTNode* copySubtree(BSTNode* currentNode)If currentNode is nullptr
return nullptrBSTNode* newNode = new BSTNode(currentNode->value)newNode->left = copySubtree(currentNode->left) newNode->right= copySubtree(currentNode->right)return newNode
BST Copy
BSTNode* copySubtree(BSTNode* currentNode)If currentNode is nullptr
return nullptrBSTNode* newNode = new BSTNode(currentNode->value)newNode->left = copySubtree(currentNode->left) newNode->right= copySubtree(currentNode->right)return newNode
BST Copy
BSTNode* copySubtree(BSTNode* currentNode)If currentNode is nullptr
return nullptrBSTNode* newNode = new BSTNode(currentNode->value)newNode->left = copySubtree(currentNode->left) newNode->right= copySubtree(currentNode->right)return newNode
BST Copy
BSTNode* copySubtree(BSTNode* currentNode)If currentNode is nullptr
return nullptrBSTNode* newNode = new BSTNode(currentNode->value)newNode->left = copySubtree(currentNode->left) newNode->right= copySubtree(currentNode->right)return newNode
BST Copy
BSTNode* copySubtree(BSTNode* currentNode)If currentNode is nullptr
return nullptrBSTNode* newNode = new BSTNode(currentNode->value)newNode->left = copySubtree(currentNode->left) newNode->right= copySubtree(currentNode->right)return newNode
BST Copy
BSTNode* copySubtree(BSTNode* currentNode)If currentNode is nullptr
return nullptrBSTNode* newNode = new BSTNode(currentNode->value)newNode->left = copySubtree(currentNode->left) newNode->right= copySubtree(currentNode->right)return newNode
BST Copy
BSTNode* copySubtree(BSTNode* currentNode)If currentNode is nullptr
return nullptrBSTNode* newNode = new BSTNode(currentNode->value)newNode->left = copySubtree(currentNode->left) newNode->right= copySubtree(currentNode->right)return newNode
BST Copy
BST Memory
• Duplicate
• Copy Constror Assignment Operator
BST Removal
• Case 1: No children
BST Removal
• Case 1: No children
• Prune from parent and delete
BST Removal
• Case 2: One child
BST Removal
• Case 2: One child
• Hook parent to the one child
BST Removal
• Case 3: Two Children
BST Removal
• Case 3: Two Children
• Identify replacement (smallest on right)
BST Removal
• Case 3: Two Children
• Move value to node being replaced
• Case 3: Two Children
• Remove smallest on right
BST Removal
• Recursive Remove:
How It Works
• SmallestValue( startNode )– Return value of leftmost
child of startNode
Helpers
• RemoveSmallestNode( startNode )– Return pointer to
subtree with leftmostnode removed
Helpers