1. How to check tree is balanced?
2. Number of internal nodes, external nodes, and height.
Todos:
Binary search tree property:
"Let x be a node in a binary search tree. If y is a node in the left subtree of x, then y:key <= x:key. If y is a node in the right subtree of x, then y:key >= x:key."
Operation related to Binary Search Tree:
Solving Real Tree Problems
- Inorder-Tree-Walk (recursive)
- Preorder-Tree-Walk
- put the print x.key before the recursive calls.
- Postorder-Tree-Walk
- put the print x.key after the recursive calls.
- Search(recursive)
- Search(iterative)
- Minimum
- Maximum
- Successor
- Two case observed,
- If successor exist and x's right sub-tree is not NUL, we find the minimum node of the right sub-tree.
- If successor exist and x don't have a right sub-tree, it's successor must be in the ancestors, which is above the x in the tree. Its successor in this case can be found as the lowest (first when we track back from bottom up) ancestor who has the left children is also x's ancestor. (for instance, the node 13's successor is 15 in the following tree)
- Predecessor
- Similar to the successor, two cases:
- If the left sub-tree exist, find the maximum of the left sub-tree, which is the predecessor of the node.
- If the left sub-tree doesn't exist, the predecessor is the first ancestor that has a right child which is also its ancestor.
- Insertion
- Two point to notice:
- y is defined to keep a copy of the x's parent pointer, it also called trailing pointer. Because by the time the NUL is found for z, x already become NUL, only its previous(save at y) can be used to update with z.
- x is defined as a pointer to find the proper position in the tree. View it as a pinter that used to visit the tree node to find something.
- Deletion
- Deletion is non-trivial, there are three cases:
- z has only one child, just remove the node, this will not impact the property of Binary search tree.
- z has two children and its successor y is z's right children, replace z by y.
- z has two children and its successor y is not z's right children. In this case, we still need to replace z by y, but it involves more operations. We need re-arrange the node in the z's right sub-tree in order to maintain the binary search tree property. I can be proved that z's successor y doesn't have a left sub-tree. So we do the following: (1) move y out of the right sub-tree, transplant its right sub-tree to y, notice this doesn't violate the tree properties. (2) replace z by y, update y's right sub-tree with z's right sub-tree. update y's left sub-tree with z's left sub-tree to complete the "delete operation". Notice this second step still keep the tree as a valid binary search tree. (If you don't understand, see CLRS page 297 Figure 12.4)
- Transplant
- this is function is to replace the sub-tree rooted at node u with the sub-tree rooted at node v, node u's parent becomes node v's parent. Pay attention to update v's parent with v.p = u.p if v is not NUL. if it is, we don't need to do anything, replace u with a NUL will do the all.
Solving Real Tree Problems
- Write a iterative method to do in-order traversal of a Binary Tree. Use two solution, solution 1 can use stack. In solution 2, stack is not allowed.
Thoughts:
- Using loops to do the iterative, stack is used as a temp storage.
- First node to visit is left most node in the tree. It don't have any left nodes.
- End the loop when the largest node has been visited (in-order), the largest nodes in the right most.
- Algorithm: Visit the left most node first, two possibilities exist: (1) it have no children, (2) it have right children. If it have no children, visit it --> visit parents --> go to its parent's right children.
- BE CAUTIONS about the if condition correctness.
The non-stack solution can rely on pointer that can visit its parents and its grandparents after visit the right children. This is straight forward to think but hardest to implement.
The full solution to this problem can find here: http://leetcode.com/2010/04/binary-search-tree-in-order-traversal.html - untitled











No comments:
Post a Comment