Advertisement
kimo12

Untitled

Mar 23rd, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.00 KB | None | 0 0
  1. package eg.edu.alexu.csd.filestructure.avl;
  2.  
  3. public class AVLTree<T extends Comparable<T>> implements IAVLTree<T> {
  4.     private Node<T> root;
  5.     private boolean haveRoot;
  6.  
  7.     public AVLTree() {
  8.         haveRoot = false;
  9.         root = new Node<T>(null, null);
  10.     }
  11.  
  12.     @Override
  13.     public void insert(T key) {
  14.  
  15.         Node<T> cursor = null; // y
  16.         Node<T> current = root; // x
  17.         Node<T> newNode = new Node<T>(key, null); // z
  18.         while (current != null && current.getValue() != null) {
  19.             cursor = current;
  20.             try {
  21.                 if (newNode.getValue().compareTo(current.getValue()) < 0) {
  22.                     current = (Node<T>) current.getLeftChild();
  23.                 } else if (newNode.getValue().compareTo(current.getValue()) > 0) {
  24.                     current = (Node<T>) current.getRightChild();
  25.                 }
  26.             } catch (NullPointerException ex) {
  27.                 break;
  28.             }
  29.         }
  30.  
  31.         newNode.setParent(cursor);
  32.         if (cursor == null) {
  33.             root = newNode;
  34.         } else if (newNode.getValue().compareTo(cursor.getValue()) < 0) {
  35.             cursor.setLeftChild(newNode);
  36.             newNode.setParent(cursor);
  37.             reBalance((Node<T>) cursor.getLeftChild());
  38.         } else if (newNode.getValue().compareTo(cursor.getValue()) > 0) {
  39.             cursor.setRightChild(newNode);
  40.             newNode.setParent(cursor);
  41.             reBalance((Node<T>) cursor.getRightChild());
  42.         }
  43.  
  44.     }
  45.  
  46.     public void reBalance(Node<T> t) {
  47.         Node<T> parent = null; // up
  48.         Node<T> check = t;
  49.         Node<T> child = t;
  50.         while (check.hasParent()) {
  51.             // parent = (Node<T>) check.getParent();
  52.             child = check;
  53.             check = (Node<T>) check.getParent();
  54.             int balanceFactor = check.getBalanceFactor();
  55.             if (balanceFactor == 2) {
  56.                 if (check.getParent() != null)
  57.                     parent = (Node<T>) check.getParent();
  58.                 if (child.getBalanceFactor() == 1) {
  59.                     leftLeft(check, child, parent);
  60.                     break;
  61.                 } else if (child.getBalanceFactor() == -1) {
  62.                     leftRight(check, child, parent);
  63.                     break;
  64.                 }
  65.             } else if (balanceFactor == -2) {
  66.  
  67.                 if (check.getParent() != null)
  68.                     parent = (Node<T>) check.getParent();
  69.                 if (child.getBalanceFactor() == 1) {
  70.                     rightLeft(check, child, parent);
  71.                     break;
  72.                 } else if (child.getBalanceFactor() == -1) {
  73.                     rightRight(check, child, parent);
  74.                     break;
  75.  
  76.                 }
  77.             }
  78.         }
  79.     }
  80.  
  81.     private void rightRight(Node<T> check, Node<T> child, Node<T> parent) {
  82.         boolean right = false, noParent = false;
  83.         check.setRightChild(null);
  84.         child.setParent(null);
  85.         if (parent == null)
  86.             noParent = true;
  87.         else if (parent.getValue().compareTo(check.getValue()) < 0)
  88.             right = true;
  89.  
  90.         if (!noParent) {
  91.             if (right)
  92.                 parent.setRightChild(child);
  93.             else
  94.                 parent.setLeftChild(child);
  95.             child.setParent(parent);
  96.         }
  97.         if (child.hasLeftChild())
  98.             check.setRightChild((Node<T>) child.getLeftChild());
  99.         child.setLeftChild(check);
  100.         check.setParent(child);
  101.         if (check == root)
  102.             root = child;
  103.     }
  104.  
  105.     private void rightLeft(Node<T> check, Node<T> child, Node<T> parent) {
  106.         Node<T> n = (Node<T>) child.getLeftChild();
  107.         boolean right = false, noParent = false;
  108.         check.setRightChild(null);
  109.         child.setLeftChild(null);
  110.         if (parent == null)
  111.             noParent = true;
  112.         else if (parent.getValue().compareTo(check.getValue()) < 0)
  113.             right = true;
  114.  
  115.         if (!noParent) {
  116.             if (right)
  117.                 parent.setRightChild(n);
  118.             else
  119.                 parent.setLeftChild(n);
  120.             n.setParent(parent);
  121.         }
  122.  
  123.         if (n.hasRightChild()) {
  124.             ((Node<T>) n.getRightChild()).setParent(child);
  125.             child.setLeftChild((Node<T>) n.getRightChild());
  126.         }
  127.         n.setRightChild(child);
  128.         child.setParent(n);
  129.         if (n.hasLeftChild()) {
  130.             ((Node<T>) n.getLeftChild()).setParent(check);
  131.             check.setRightChild((Node<T>) n.getLeftChild());
  132.         }
  133.         n.setLeftChild(check);
  134.         check.setParent(n);
  135.         if (check == root)
  136.             root = n;
  137.  
  138.     }
  139.  
  140.     private void leftRight(Node<T> check, Node<T> child, Node<T> parent) {
  141.         Node<T> n = (Node<T>) child.getRightChild();
  142.         boolean right = false, noParent = false;
  143.         check.setLeftChild(null);
  144.         child.setRightChild(null);
  145.         if (parent == null)
  146.             noParent = true;
  147.         else if (parent.getValue().compareTo(check.getValue()) < 0)
  148.             right = true;
  149.  
  150.         if (!noParent) {
  151.             if (right)
  152.                 parent.setRightChild(n);
  153.             else
  154.                 parent.setLeftChild(n);
  155.             n.setParent(parent);
  156.         }
  157.  
  158.         if (n.hasRightChild()) {
  159.             ((Node<T>) n.getRightChild()).setParent(check);
  160.             check.setLeftChild((Node<T>) n.getRightChild());
  161.         }
  162.         n.setRightChild(check);
  163.         check.setParent(n);
  164.         if (n.hasLeftChild()) {
  165.             ((Node<T>) n.getLeftChild()).setParent(child);
  166.             child.setRightChild((Node<T>) n.getLeftChild());
  167.         }
  168.         n.setLeftChild(child);
  169.         child.setParent(n);
  170.         if (check == root)
  171.             root = n;
  172.  
  173.     }
  174.  
  175.     private void leftLeft(Node<T> check, Node<T> child, Node<T> parent) {
  176.         boolean right = false, noParent = false;
  177.         check.setLeftChild(null);
  178.         child.setParent(null);
  179.         if (parent == null)
  180.             noParent = true;
  181.         else if (parent.getValue().compareTo(check.getValue()) < 0)
  182.             right = true;
  183.  
  184.         if (!noParent) {
  185.             if (right)
  186.                 parent.setRightChild(child);
  187.             else
  188.                 parent.setLeftChild(child);
  189.             child.setParent(parent);
  190.         }
  191.         if (child.hasRightChild())
  192.             check.setLeftChild((Node<T>) child.getRightChild());
  193.         child.setRightChild(check);
  194.         check.setParent(child);
  195.         if (check == root)
  196.             root = child;
  197.     }
  198.  
  199.     public Node<T> getNodes() {
  200.         return this.root;
  201.     }
  202.  
  203.     public void inOrder(Node root) {
  204.  
  205.         if (root != null) {
  206.             inOrder((Node<T>) root.getLeftChild());
  207.             System.out.println(root.getValue());
  208.             inOrder((Node) root.getRightChild());
  209.         }
  210.     }
  211.  
  212.     private Node<T> minimum(Node<T> node) {
  213.         if (!node.hasLeftChild())
  214.             return node;
  215.         return minimum((Node<T>) node.getLeftChild());
  216.  
  217.     }
  218.  
  219.     // private Node<T> getMySuccessor(Node<T> node) {
  220.     // if (node.hasRightChild()) {
  221.     // return this.minimum((Node<T>) node.getRightChild());
  222.     // } else {
  223.     // Node<T> carry = (Node<T>) node.getParent();
  224.     // while (carry != null && ((Node<T>) node).equals((Node<T>)
  225.     // carry.getRightChild())) {
  226.     // node = carry;
  227.     // carry = (Node<T>) node.getParent();
  228.     // }
  229.     // return carry;
  230.     // }
  231.     //
  232.     // }
  233.  
  234.     private void deleteRebalance(Node<T> t) {
  235.         Node<T> parent = null; // up
  236.         Node<T> check = t;
  237.         Node<T> child = t;
  238.         while (check.hasParent()) {
  239.             check = (Node<T>) check.getParent();
  240.             int balanceFactor = check.getBalanceFactor();
  241.             if (balanceFactor == 2) {
  242.                 if (check.getParent() != null)
  243.                     parent = (Node<T>) check.getParent();
  244.                 if (check.hasLeftChild()) {
  245.                     int leftChildBF = ((Node) check.getLeftChild()).getBalanceFactor();
  246.                     if (leftChildBF == 1) {
  247.                         child = (Node<T>) check.getLeftChild();
  248.                         leftLeft(check, child, parent);
  249.                     }
  250.                 } else if (check.hasRightChild()) {
  251.                     int rightChildBF = ((Node) check.getLeftChild()).getBalanceFactor();
  252.                     if (rightChildBF == -1) {
  253.                         child = (Node<T>) check.getRightChild();
  254.                         leftRight(check, child, parent);
  255.                     }
  256.                 }
  257.             } else if (balanceFactor == -2) {
  258.                 if (check.getParent() != null)
  259.                     parent = (Node<T>) check.getParent();
  260.                 if (check.hasLeftChild()) {
  261.                     int leftChildBF = ((Node) check.getLeftChild()).getBalanceFactor();
  262.                     if (leftChildBF == -1) {
  263.                         child = (Node<T>) check.getRightChild();
  264.                         rightRight(check, child, parent);
  265.                     }
  266.                 } else if (check.hasRightChild()) {
  267.                     int rightChildBF = ((Node) check.getLeftChild()).getBalanceFactor();
  268.                     if (rightChildBF == 1) {
  269.                         child = (Node<T>) check.getLeftChild();
  270.                         rightLeft(check, child, parent);
  271.                     }
  272.                 }
  273.             }
  274.         }
  275.     }
  276.  
  277.     private void transplant(Node<T> toBeRemoved, Node<T> toBePlanted) {
  278.  
  279.         Node<T> toBeRemovedParent = (Node<T>) toBeRemoved.getParent();
  280.         if (!toBeRemoved.hasParent()) {
  281.             root = toBePlanted;
  282.         } else if (toBeRemoved.equals(toBeRemovedParent.getLeftChild())) {
  283.             toBeRemovedParent.setLeftChild(toBePlanted);
  284.         } else {
  285.             toBeRemovedParent.setRightChild(toBePlanted);
  286.         }
  287.         toBeRemoved.setParent(null);
  288.         if (toBePlanted != null) {
  289.             Node<T> toBePlantedParent = (Node<T>) toBePlanted.getParent();
  290.  
  291.             // change
  292.             if (!toBeRemoved.equals(toBePlantedParent)) {
  293.                 if (toBePlantedParent.hasLeftChild() && toBePlantedParent.getLeftChild().equals(toBePlanted)) {
  294.                     toBePlantedParent.setLeftChild((Node) toBePlanted.getRightChild());
  295.                     if (toBePlanted.hasRightChild())
  296.                         ((Node) toBePlanted.getRightChild()).setParent(toBePlantedParent);
  297.                 } else if (toBePlantedParent.hasRightChild() && toBePlantedParent.getRightChild().equals(toBePlanted)) {
  298.                     toBePlantedParent.setRightChild(null);
  299.                 }
  300.  
  301.                 toBePlanted.setParent(toBeRemovedParent);
  302.  
  303.                 if (toBeRemoved.hasLeftChild()) {
  304.                     Node<T> leftOfToBeRemoved = ((Node<T>) toBeRemoved.getLeftChild());
  305.                     toBePlanted.setLeftChild(leftOfToBeRemoved);
  306.                     leftOfToBeRemoved.setParent(toBePlanted);
  307.                     toBeRemoved.setLeftChild(null);
  308.  
  309.                 }
  310.  
  311.                 if (toBeRemoved.hasRightChild()) {
  312.                     Node<T> rightOfToBeRemoved = ((Node<T>) toBeRemoved.getRightChild());
  313.                     toBePlanted.setRightChild(rightOfToBeRemoved);
  314.                     rightOfToBeRemoved.setParent(toBePlanted);
  315.                     toBeRemoved.setRightChild(null);
  316.  
  317.                 }
  318.             } else {
  319.  
  320.                 toBePlanted.setParent(toBeRemovedParent);
  321.  
  322.                 if (toBeRemoved.getRightChild().equals(toBePlanted)) {
  323.                     toBePlanted.setLeftChild((Node) toBeRemoved.getLeftChild());
  324.                     ((Node) toBePlanted.getLeftChild()).setParent(toBePlanted);
  325.                 } else if (toBeRemoved.getLeftChild().equals(toBePlanted)) {
  326.                     toBePlanted.setRightChild((Node) toBeRemoved.getRightChild());
  327.                     ((Node) toBePlanted.getRightChild()).setParent(toBePlanted);
  328.                 }
  329.             }
  330.            
  331.             deleteRebalance(toBePlanted);
  332.  
  333.         }
  334.  
  335.     }
  336.  
  337.     @Override
  338.     public boolean delete(T key) {
  339.         if (!search(key))
  340.             return false;
  341.  
  342.         Node<T> toBeDeleted = searchReturnsNode(key);
  343.         if (!toBeDeleted.hasLeftChild()) {
  344.             this.transplant(toBeDeleted, (Node<T>) toBeDeleted.getRightChild());
  345.         } else if (!toBeDeleted.hasRightChild()) {
  346.             this.transplant(toBeDeleted, (Node<T>) toBeDeleted.getLeftChild());
  347.         } else {
  348.             Node<T> node = minimum((Node<T>) toBeDeleted.getRightChild());
  349.             this.transplant(toBeDeleted, node);
  350.         }
  351.  
  352.         return true;
  353.     }
  354.  
  355.     private Node<T> searchReturnsNode(T key, Node<T> node) {
  356.         if (key.compareTo(node.getValue()) == 0)
  357.             return node;
  358.         if (key.compareTo(node.getValue()) < 0)
  359.             return searchReturnsNode(key, (Node<T>) node.getLeftChild());
  360.         else
  361.             return searchReturnsNode(key, (Node<T>) node.getRightChild());
  362.  
  363.     }
  364.  
  365.     public Node<T> searchReturnsNode(T key) {
  366.         return this.searchReturnsNode(key, root);
  367.     }
  368.  
  369.     private boolean search(T key, Node<T> node) {
  370.         if (node != null) {
  371.             if (key.compareTo(node.getValue()) == 0)
  372.                 return true;
  373.             if (key.compareTo(node.getValue()) < 0)
  374.                 return search(key, (Node<T>) node.getLeftChild());
  375.             else
  376.                 return search(key, (Node<T>) node.getRightChild());
  377.         }
  378.         return false;
  379.  
  380.     }
  381.  
  382.     @Override
  383.     public boolean search(T key) {
  384.         return this.search(key, root);
  385.     }
  386.  
  387.     @Override
  388.     public int height() {
  389.         if (root.getValue() == null) {
  390.             return -1;
  391.         } else {
  392.             return Math.max(root.getLeftHeight(), root.getLeftHeight());
  393.         }
  394.     }
  395.  
  396.     @Override
  397.     public INode<T> getTree() {
  398.         return root;
  399.     }
  400.  
  401. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement