Binary Search Tree - 2

Binary Search Tree - 2

Traversing the Tree

  • Traversing a tree means visiting each node in a specified order.
  • There are three simple ways to traverse a tree.
    • preorder
    • inorder
    • postorder

Inorder Traversal :

  • The method needs to do only three things :

    1. Call itself to traverse the node’s left subtree.
    2. Visit the node.
    3. Call itself to traverse the node’s right subtree.
  • Implement inorder traversal by recursion.

  1. //pseudo code
  2. public static void main(String args[]) {
  3. inOrder(root);
  4. }
  5. private void inOrder(Node localRoot) {
  6. if(localRoot != null) {
  7. inOrder(localRoot.leftChild);
  8. System.out.print(localRoot.iData + “);
  9. inOrder(localRoot.rightChild);
  10. }
  11. }
Example :

Preorder Traversal :

  • The method needs to do only three things :
    1. Visit the node.
    2. Call itself to traverse the node’s left subtree.
    3. Call itself to traverse the node’s right subtree.

Postorder Traversal :

  • The method needs to do only three things :
    1. Call itself to traverse the node’s left subtree.
    2. Call itself to traverse the node’s right subtree.
    3. Visit the node.

Inorder V.S. Preorder V.S. Postorder Traversal :

Example : algebraic expression :

Finding Maximum and Minimum Values in Binary Search Tree :

  • For the minimum, go to the left child of the root; then go to the left child of that child, and so on, until you come to a node that has no left child. This node is the minimum.

  • For the maximum value in the tree, follow the same procedure, but go from right child to right child until you find a node with no right child. This node is the maximum.

    1. //pseudo code
    2. // returns node with minimum key value
    3. public Node minimum() {
    4. Node current, last;
    5. current = root; // start at root
    6. // until the bottom,
    7. while(current != null) {
    8. last = current; // remember node
    9. current = current.leftChild; // go to left child
    10. }
    11. return last;
    12. }

Deleting a Node :

  • Deleting a node is the most complicated common operation required for binary search trees.
  • There are three cases to consider:
    1. The node to be deleted is a leaf (has no children).
    2. The node to be deleted has one child.
    3. The node to be deleted has two children.

Case 1: The Node to Be Deleted Has No Children :

  • To delete a leaf node, you simply change the appropriate child field in the node’s parent to point to null, instead of to the node.
  • The node will still exist, but it will no longer be part of the tree.
  1. //pseudo code
  2. // delete node with given key
  3. public boolean delete(int key) {
  4. // (assumes non-empty list)
  5. Node current = root;
  6. Node parent = root;
  7. boolean isLeftChild = true;
  8. // search for node
  9. while(current.iData != key) {
  10. parent = current;
  11. // go left?
  12. if(key < current.iData) {
  13. isLeftChild = true;
  14. current = current.leftChild;
  15. } else {
  16. // or go right?
  17. isLeftChild = false;
  18. current = current.rightChild;
  19. }
  20. // end of the line,
  21. if(current == null) {
  22. // didn’t find it
  23. return false;
  24. }
  25. //delete node
  26. // if no children, simply delete it
  27. if(current.leftChild == null && current.rightChild == null) {
  28. // if root,
  29. if(current == root) {
  30. root = null; // tree is empty
  31. } else if(isLeftChild) {
  32. // disconnect from parent
  33. parent.leftChild = null;
  34. } else {
  35. parent.rightChild = null;
  36. }
  37. }
  38. }
  39. }

Case 2: The Node to Be Deleted Has One Child :

  • The node has only two connections: to its parent and to its only child.
  • You want to “snip” the node out of this sequence by connecting its parent directly to its child.
  • This process involves changing the appropriate reference in the parent (leftChild or rightChild) to point to the deleted node’s child.
  1. //pseudo code
  2. //delete node
  3. // if no right child, replace with left subtree
  4. else if(current.rightChild==null) {
  5. if(current == root) {
  6. root = current.leftChild;
  7. } else if(isLeftChild) {
  8. // left child of parent
  9. parent.leftChild = current.leftChild;
  10. } else {
  11. // right child of parent
  12. parent.rightChild = current.leftChild;
  13. }
  14. }
  15. // if no left child, replace with right subtree
  16. else if(current.leftChild==null) {
  17. if(current == root) {
  18. root = current.rightChild;
  19. } else if(isLeftChild) {
  20. // left child of parent
  21. parent.leftChild = current.rightChild;
  22. } else {
  23. // right child of parent
  24. parent.rightChild = current.rightChild;
  25. }
  26. }

Case 3: The Node to Be Deleted Has Two Children :

  • If the deleted node has two children, you can’t just replace it with one of these children, at least if the child has its own children.
  • For each node, the node with the next-highest key is called its inorder successor, or simply its successor.
  • To delete a node with two children, replace the node with its inorder successor.

Finding the Successor :

  • What we’re really looking for is the smallest of the set of nodes that are larger than the original node.
  • When you go to the original node’s right child, all the nodes in the resulting subtree are greater than the original node because this is how a binary search tree is defined.
  • Now we want the smallest value in this subtree. As we learned, you can find the minimum value in a subtree by following the path down all the left children.

Trees Represented as Arrays :

  • With this scheme, a node’s children and parent can be found by applying some simple arithmetic to the node’s index number in the array.
  • If a node’s index number is index :
    • this node’s left child is : 2*index + 1
    • its right child is : 2*index + 2
    • its parent is : (index-1) / 2

留言