Ejemplo n.º 1
0
 public AVLNode(int nodeValue, AVLNode ParentNode)
 {
     //the preferred constructor will take in an integer value
     //and also take in a ParentNode
     this.value = nodeValue;
     this.parentNode = ParentNode;
     this.leftChild = null;
     this.rightChild = null;
 }
Ejemplo n.º 2
0
 public AVLNode(int nodeValue)
 {
     //the simple constructor will take in an integer value
     //and sets ourself as the parent node
     this.value = nodeValue;
     this.parentNode = this;
     this.leftChild = null;
     this.rightChild = null;
     this.myLabel = null;
 }
Ejemplo n.º 3
0
 public void addValue(int newValue)
 {
     //create a new node with the new value
     AVLNode newNode = new AVLNode(newValue);
     if (this.rootNode == null) {
         //if we don't currently have a root node, make it the new node
         this.rootNode = newNode;
     } else {
         //if we do have a root node, pass the new node down to it for processing
         this.rootNode.addChild(newNode);
     }
 }
Ejemplo n.º 4
0
 public void addChild(AVLNode newChild)
 {
     //This function will put a new child node into the tree
     if (newChild.value <= this.value) {
         //check to see of we have an empty slot in the left child position
         if (this.leftChild == null) {
             //if so, fill it
             newChild.parentNode = this;
             this.leftChild = newChild;
         } else {
             //if not, then pass the new node on to it
             this.leftChild.addChild(newChild);
         }
     } else {
         if (this.rightChild == null) {
             newChild.parentNode = this;
             this.rightChild = newChild;
         } else {
             this.rightChild.addChild(newChild);
         }
     }
 }
Ejemplo n.º 5
0
 public void balance()
 {
     //balance the tree and get the new rootNode, if it changes
     this.rootNode = this.rootNode.balance();
 }
Ejemplo n.º 6
0
 public AVLTree()
 {
     //the default constructor does some simple initialization
     this.rootNode = null;
 }
Ejemplo n.º 7
0
        public void swap(AVLNode replacementNode)
        {
            //this function is the brunt of the balancing movement
            //this function should only be called on a parent node
            //to swap with one of its children
            if (this.parentNode != this) {
                replacementNode.ParentNode = this.parentNode;
            } else {
                //if we are at the top, make the other node its own parent
                replacementNode.ParentNode = replacementNode;
            }

            if (this.leftChild == replacementNode) {
                //swap the position of this node with the supplanting node
                if (this.parentNode != this) {
                    if (replacementNode.Value > this.parentNode.Value) {
                        this.parentNode.RightChild = replacementNode;
                    } else {
                        this.parentNode.LeftChild = replacementNode;
                    }
                }
                this.leftChild = replacementNode.RightChild;           //adopt its appropriate child
                replacementNode.RightChild = replacementNode;
                if (this.leftChild != null) {this.leftChild.ParentNode = this;} //notify the child of its new parent

            } else if (this.rightChild == replacementNode) {
                if (this.parentNode != this) {
                    if (replacementNode.Value < this.parentNode.Value) {
                        this.parentNode.LeftChild = replacementNode;
                } else {
                    this.parentNode.RightChild = replacementNode;
                }
                }
                this.rightChild = replacementNode.LeftChild;
                replacementNode.LeftChild = this;
                if (this.rightChild != null) { this.rightChild.ParentNode = this; }
            }
            //finally make our old child into our parentNode. (weird!)
            this.parentNode = replacementNode;
        }