Esempio n. 1
0
 public void addValue(double newValue)
 {
     if(newValue > value)
       {
     //check if the right value is null otherwise add it at this Node
     if(rightNode == null)
     {
       rightNode = new BinaryNode(newValue,this, avl);
     }
     else
     {
       rightNode.addValue(newValue);
     }
       }
       else
       {
     //check if the left Node is null otherwise add it at this Node
     if(leftNode == null)
     {
       leftNode = new BinaryNode(newValue, this, avl);
     }
     else
     {
       leftNode.addValue(newValue);
     }
       }
       calculateBalance();
       //now handle the avl desire
       if(avl)
       {
     AVlHandling();
       }
 }
Esempio n. 2
0
 //if I'm the parent of the tree
 public BinaryNode(double value, bool avl)
 {
     this.value = value;
       this.parent = null;
       leftNode = null;
       rightNode = null;
       this.avl = avl;
       balance = 0;
 }
 public bool SaveFileAtPath(BinaryNode saveValue)
 {
     try
       {
     XMLSerializer serializer = new XMLSerializer(typeof(BinaryNode));
     FileStream fs = new FileStream(Path, FileMode.Create);
     serializer.Serialize(fs,saveValue);
     fs.Close();
     return true;
       }
       catch
       {
     return false;
       }
 }
Esempio n. 4
0
 public double[] GetInOrder(BinaryNode current)
 {
     if(current == null)
       {
     //use the root as initial value
     current = root;
       }
       //L-W-R
       double[] result = new double[0];
       double[] backup;
       if(leftNode != null)
       {
     result = GetInOrder(leftNode);
       }
       //add my own value
       backup = result;
       result = new double[backup.Length + 1];
       for(int i = 0; i < backup.Length; i++)
       {
     result[i] = backup[i];
       }
       result[backup.Length] = value;
       //add the right value
       if(rightNode != null)
       {
     double[] rightResult = GetInOrder(rightNode);
     backup = result;
     result = new double[backup.Length + rightResult.Length];
     for(int i = 0; i < backup.Length; i++)
     {
       result[i] = backup[i];
     }
     for(int i = 0; i < rightResult.Length; i++)
     {
       result[i + backup.Length] = rightResult[i];
     }
       }
       //return the result
       return result;
 }
Esempio n. 5
0
 public BinaryTree(BinaryNode root)
 {
     this.root = root;
 }
Esempio n. 6
0
 public BinaryTree(bool avl, double startValue)
 {
     root = new BinaryNode(startValue, avl);
 }
Esempio n. 7
0
 public double[] GetPreOrder(BinaryNode current)
 {
     if(current == null)
       {
     //use the root as initial value
     current = root;
       }
       //W-L-R
       //add my own value
       double[] result = {value};
       double[] backup;
       if(leftNode != null)
       {
     double[] leftResult = GetPreOrder(leftNode);
     backup = result;
     result = new double[backup.Length + leftResult.Length];
     for(int i = 0; i < backup.Length; i++)
     {
       result[i] = backup[i];
     }
     for(int i = 0; i < leftResult.Length; i++)
     {
       result[i + backup.Length] = leftResult[i];
     }
       }
       if(rightNode != null)
       {
     double[] rightResult = GetPreOrder(rightNode);
     backup = result;
     result = new double[backup.Length + rightResult.Length];
     for(int i = 0; i < backup.Length; i++)
     {
       result[i] = backup[i];
     }
     for(int i = 0; i < rightResult.Length; i++)
     {
       result[i + backup.Length] = rightResult[i];
     }
       }
       //now return the result
       return result;
 }
Esempio n. 8
0
 public double[] GetPostOrder(BinaryNode current)
 {
     //check for the first call of the method
       if(current == null)
       {
     current = root;
       }
       //L-R-W
       double[] result = new double[0];
       if(leftNode != null)
       {
     double[] leftResult = GetPostOrder(leftNode);
     result = leftResult;
       }
       if(rightNode != null)
       {
     double[] righResult = GetPostOrder(rightNode);
     double[] backup = result;
     result = new double[backup.Length + rightResult.Length];
     for(int i = 0; i < backup.Length; i++)
     {
       result[i] = backup[i];
     }
     for(int i = 0; i < rightResult.Length; i++)
     {
       result[i + backup.Length] = rightResult[i];
     }
       }
       //now add my own value
       double[] backup = result;
       result = new double[backup.Length + 1];
       for(int i = 0; i < backup.Length; i++)
       {
     result[i] = backup[i];
       }
       result[backup.Length] = value;
       //return the result
       return result;
 }
Esempio n. 9
0
 //set Methods
 public void SetRightNode(BinaryNode newNode)
 {
     rightNode = newNode;
 }
Esempio n. 10
0
 public void SetParent(BinaryNode newParent)
 {
     parent = newParent;
 }
Esempio n. 11
0
 public void SetLeftNode(BinaryNode newNode)
 {
     leftNode = newNode;
 }
Esempio n. 12
0
 public void RightRotation()
 {
     //get the right Node of the current Left Node to prevent for loosing data
       BinaryNode rightOfLeft = leftNode.GetRightNode();
       //setup the leftNode to be the parent
       leftNode.setParent(parent);
       leftNode.SetRightNode(this);
       //setup the parent
       //check if I'm the left or the right node of the parent
       if(parent.getValue > value)
       {
     //I'm a left node
     parent.SetLeftNode(leftNode);
       }
       else
       {
     //I'm a right node
     parent.SetRightNode(leftNode);
       }
       //setup myself
       parent = leftNode;
       leftNode = null;
       //now look if the rightOfLeft has a value
       if(rightOfLeft != null)
       {
     parent.addValue(rightOfLeft.getValue());
       }
 }
Esempio n. 13
0
 public void LeftRotation()
 {
     //get the left Node of the current Right node to prevent loosing data
       BinaryNode leftOfRight = rightNode.GetLeftNode();
       //setup the right node
       rightNode.setParent(parent);
       rightNode.SetLeftNode(this);
       //setup parent
       if(parent.getValue() > value)
       {
     //I'm a left node
     parent.SetLeftNode(rightNode);
       }
       else
       {
     //I'm a right Node
     parent.SetRightNode(rightNode);
       }
       //setup myself
       parent = leftNode;
       leftNode = null;
       //now look if the leftOfRight has a value
       if(leftOfRight != null)
       {
     parent.addValue(leftOfRight.getValue());
       }
 }