Advertisement
kimo12

Untitled

Mar 23rd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.89 KB | None | 0 0
  1. package eg.edu.alexu.csd.filestructure.avl;
  2.  
  3. public class Node<T extends Comparable<T>> implements INode<T> {
  4.     private T value;
  5.     private Node<T> rightChild = null;
  6.     private Node<T> leftChild = null;
  7.     private Node<T> parent = null;
  8.  
  9.     public Node(T value, Node parent) {
  10.         this.value = value;
  11.         this.parent = parent;
  12.     }
  13.  
  14.     public int getBalanceFactor() {
  15.         return this.getLeftHeight() - this.getRightHeight();
  16.     }
  17.  
  18.     public int getLeftHeight() {
  19.  
  20.         if (this.hasLeftChild()) {
  21.             int leftH = ((Node) this.getLeftChild()).getLeftHeight();
  22.             int rightH = ((Node) this.getLeftChild()).getRightHeight();
  23.             if (rightH > leftH) {
  24.                 return rightH + 1;
  25.             } else
  26.                 return leftH + 1;
  27.         }
  28.  
  29.         return 0;
  30.     }
  31.  
  32.     public int getRightHeight() {
  33.  
  34.         if (this.hasRightChild()) {
  35.             int leftH = ((Node) this.getRightChild()).getLeftHeight();
  36.             int rightH = ((Node) this.getRightChild()).getRightHeight();
  37.             if (rightH > leftH) {
  38.                 return rightH + 1;
  39.             } else
  40.                 return leftH + 1;
  41.         }
  42.         return 0;
  43.     }
  44.  
  45.     public boolean hasLeftChild() {
  46.         if (this.leftChild == null)
  47.             return false;
  48.         return true;
  49.     }
  50.  
  51.     public boolean hasParent() {
  52.         if (this.parent == null)
  53.             return false;
  54.         return true;
  55.     }
  56.  
  57.     public boolean hasRightChild() {
  58.         if (this.rightChild == null)
  59.             return false;
  60.         return true;
  61.     }
  62.  
  63.     public void setLeftChild(Node leftChild) {
  64.         this.leftChild = leftChild;
  65.     }
  66.  
  67.     public void setRightChild(Node rightChild) {
  68.         this.rightChild = rightChild;
  69.     }
  70.  
  71.     public void setParent(Node parent) {
  72.         this.parent = parent;
  73.     }
  74.  
  75.     @Override
  76.     public INode<T> getLeftChild() {
  77.         return this.leftChild;
  78.     }
  79.  
  80.     @Override
  81.     public INode<T> getRightChild() {
  82.         return this.rightChild;
  83.     }
  84.  
  85.     public INode<T> getParent() {
  86.         return this.parent;
  87.     }
  88.  
  89.     @Override
  90.     public T getValue() {
  91.         return this.value;
  92.     }
  93.  
  94.     @Override
  95.     public void setValue(T value) {
  96.         this.value = value;
  97.  
  98.     }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement