Trees/Removal
From charlesreid1
Removing single node from unsorted tree
Removal operation for trees:
- One way to call the remove() method is with no parameters
- This replaces a node with its child
- Raises an exception if a node has two (or more) children
Removing single node from sorted tree
But it also gets more complicated.
- In a sorted binary tree, need to rearrange the tree - traverse the tree to find the preceding node, and replace the node being removed with the preceding node.
- This requires traversing potentially the entire height of the tree, and so costs O(log N).
Removing subtree from unsorted tree
Removing a subtree:
- Removal of an entire subtree should be possible.
- Pass in a position in the tree, the method will remove the node at that position, as well as any child nodes.
- This is done by first setting the left and right children of the target position to null, then finding the parent of the target and setting its child equal to null.
- In a tree these are the only possible nodes that could point to this node.
Removing subtree from sorted tree
Removing a subtree from a sorted tree:
- When removing a subtree in a sorted tree, you're removing a set of contiguous values.
- No need to balance tree, because no possible child nodes needing reordering.
Flags
| Trees Part of Computer Science Notes
Series on Data Structures Abstract data type: Trees/ADT Concrete implementations: Trees/LinkedTree · Trees/ArrayTree · SimpleTree
Tree Traversal Preorder traversal: Trees/Preorder Postorder traversal: Trees/Postorder In-Order traversal: Binary Trees/Inorder Breadth-First Search: BFS Breadth-First Traversal: BFT Depth-First Search: DFS Depth-First Traversal: DFT OOP Principles for Traversal: Tree Traversal/OOP · Tree Traversal/Traversal Method Template Tree operations: Trees/Operations Performance · Trees/Removal
Tree Applications Finding Minimum in Log N Time: Tree/LogN Min Search
Abstract data type: Binary Trees/ADT Concrete implementations: Binary Trees/LinkedBinTree · Binary Trees/ArrayBinTree Binary Trees/Cheat Sheet · Binary Trees/OOP · Binary Trees/Implementation Notes
|