示例#1
0
文件: BT_Node.cs 项目: rhsattev/ALG2
        /**
         * Add a number in the subtree of this node
         */
        public BT_Node Insert(int _value)
        {
            BT_Node newNode = new BT_Node(this, _value);

            if (_value < this.Value)
            {
                // Smaller value, insert it into the left subtree
                if (this.Left == null)
                    this.Left = newNode;
                else
                    this.Left.Insert(_value);
            }
            else
            {
                // Larger value, insert it in the right subtree
                if (this.Right == null)
                    this.Right = newNode;
                else
                    this.Right.Insert(_value);
            }
            return newNode;
        }
示例#2
0
 private BT_Node Search(BT_Node n, int _value)
 {
     if (this.Root.Value == _value)
         return Root;
     else
         return n.Value <= _value ? StepRight(n, _value) : StepLeft(n, _value);
 }
示例#3
0
 /**
  * Double rotate binary tree node: first right child
  * with its left child; then node k1 with new right child.
  * For AVL trees, this is a double rotation for case 3.
  * Update heights, then return new root.
  */
 private BT_Node RotateRightRight(BT_Node _node)
 {
     _node.Right = RotateLeft(_node.Right);
     return RotateRight(_node);
 }
示例#4
0
        /**
         * Rotate binary tree node with right child.
         * For AVL trees, this is a single rotation for case 4.
         * Update heights, then return new root.
         */
        private BT_Node RotateRight(BT_Node _node)
        {
            BT_Node temp = _node.Right;
            _node.Right = temp.Left;
            temp.Left = _node;

            return temp;
        }
示例#5
0
        private bool Remove(int _value)
        {
            if (Root == null)
            {
                return false;
            }
            else
            {
                if (Root.Value == _value)
                {
                    BT_Node node = new BT_Node(null, 0);
                    node.Left = root;
                    bool result = root.Remove(_value, node);
                    root = node.Left;

                    return result;
                }
                else
                {
                    return root.Remove(_value, null);
                }
            }
        }
示例#6
0
        private int CountRecusive(BT_Node node)
        {
            int elements = 0;

            if (node != null)
                elements = 1 + CountRecusive(node.Left) + CountRecusive(node.Right);

            return elements;
        }
示例#7
0
 /**
  * Inserts the value into the binary search tree
  */
 public void Insert(int _value)
 {
     if (Root == null)
     {
         Root = new BT_Node(null, _value);
     }
     else
     {
         BT_Node nodeComairer = Root;
         BT_Node newNode = new BT_Node(null, _value);
         while (newNode != null)
         {
             // Go Left
             if (newNode.Value < nodeComairer.Value)
             {
                 if (nodeComairer.Left == null)
                 {
                     newNode.Parent = nodeComairer;
                     nodeComairer.Left = newNode;
                     InsertBalance(newNode.Parent);
                     return;
                 }
                 else
                 {
                     nodeComairer = nodeComairer.Left;
                 }
             }
             // Go Right
             else if (newNode.Value > nodeComairer.Value)
             {
                 if (nodeComairer.Right == null)
                 {
                     newNode.Parent = nodeComairer;
                     nodeComairer.Right = newNode;
                     InsertBalance(newNode.Parent);
                     return;
                 }
                 else
                 {
                     nodeComairer = nodeComairer.Right;
                 }
             }
             else
             {
                 InsertBalance(Root.Insert(_value).Parent);
             }
         }
     }
 }
示例#8
0
        /**
         * Print all values that lie between min and max in order (inclusive)
         */
        public void PrintInRange(BT_Node _node, int _min, int _max)
        {
            if (_node == null)
                return;

            if ( _node.Value > _min)
                PrintInRange(_node.Left, _min, _max);

            if (_node.Value >= _min && _node.Value <= _max)
                Console.Write(_node.Value + ", ");

            if (_node.Value < _max)
                PrintInRange(_node.Right, _min, _max);
        }
示例#9
0
        public void Rebalance(BT_Node _node)
        {
            // substrect by one for the parm node is counted as well
            int depthLeft = (this.Depth(Root.Left) - 1);
            int depthRight = (this.Depth(Root.Right) - 1);

            int balance = depthLeft - depthRight;

            if ((depthLeft - depthRight) < 1)
            {
                return;
            }
            // go left
            else if ((depthLeft - depthRight) <= 2)
            {
                if ((depthLeft - depthRight) == 1)
                    RotateRight(_node);
                else
                    RotateLeftLeft(_node);

                return;
            }
            // go Right
            else if ((depthRight - depthLeft) <= 2)
            {
                if ((depthRight - depthLeft) == 1)
                    RotateLeft(_node);
                else
                    RotateRightRight(_node);

                return;
            }
        }
示例#10
0
 public int minValue(BT_Node node)
 {
     if (node.Left == null)
         return node.Value;
     else
         return minValue(node.Left);
 }
示例#11
0
        /**
         * Print all the values in the tree in order
         */
        public void Print(BT_Node node)
        {
            if (node == null)
                return;

            Print(node.Left);

            Console.Write(node.Value + " ");

            Print(node.Right);
        }
示例#12
0
 /**
  * Returns the smallest value in the tree (or -1 if tree is empty)
  */
 public BT_Node Min(BT_Node node)
 {
     if (node != null)
     {
         if (node.Left != null && node.Left.Value <= node.Value)
             return Min(node.Left);
         else
             return node;
     }
     else
     {
         node = new BT_Node();
         node.Value = -1;
         return node;
     }
 }
示例#13
0
 /**
  * Returns the largest value in the tree (or -1 if tree is empty)
  */
 public BT_Node Max(BT_Node node)
 {
     if (node != null)
     {
         if (node.Right != null && node.Right.Value > node.Value)
             return Max(node.Right);
         else
             return node;
     }
     else
     {
         node = new BT_Node();
         node.Value = -1;
         return node;
     }
 }
示例#14
0
        public void InsertBalance(BT_Node newNode)
        {
            // substrect by one for the parm node is counted as well
            int depthLeft = (this.Depth(Root.Left));
            int depthRight = (this.Depth(Root.Right));

            int balance = depthLeft - depthRight;

            while (newNode != null)
            {
                if (balance > -2 && balance < 2)
                {
                    return;
                }
                // go left
                else if (balance >= 2)
                {
                    if (balance == 2)
                        RotateRight(newNode);
                    else
                        RotateRightRight(newNode);
                }
                // go Right
                else if (balance <= -2)
                {
                    if (balance == -2)
                        RotateLeft(newNode);
                    else
                        RotateLeftLeft(newNode);
                }

                if (newNode != null)
                    //balance += (newNode.Parent.Left == newNode ? 1 : -1);
                    balance -= 1;

                newNode = newNode.Parent;
            }
        }
示例#15
0
 private BT_Node StepRight(BT_Node n, int _value)
 {
     if (n.Value == _value)
         return n;
     else if (n.Left != null && n.Value <= _value)
         return StepLeft(n.Left, _value);
     else if (n.Right != null && n.Value > _value)
         return StepRight(n.Right, _value);
     else
         return null;
 }
示例#16
0
 public void remove(int x)
 {
     root = remove(x, root);
 }
示例#17
0
文件: BT_Node.cs 项目: rhsattev/ALG2
 public BT_Node(BT_Node _parrent, int _value)
 {
     this.Value = _value;
     this.Parent = _parrent;
 }
示例#18
0
        public BT_Node remove(int x, BT_Node _node)
        {
            return null;
            //if (_node == null)
            //{
            //	return null;
            //}
            //Console.WriteLine("Remove starts... " + _node.Value + " and " + x);

            //if (x.CompareTo(_node.Value) < 0)
            //{
            //	_node.Left = remove(x, _node.Left);
            //	int l = _node.Left != null ? Depth(_node.Left) : 0;

            //	if ((_node.Right != null) && (_node.Right.height - l >= 2))
            //	{
            //		int rightHeight = _node.Right.Right != null ? _node.Right.Right.height : 0;
            //		int leftHeight = _node.Right.Left != null ? _node.Right.Left.height : 0;

            //		if (rightHeight >= leftHeight)
            //			_node = RotateLeft(_node);
            //		else
            //			_node = RotateRightRight(_node);
            //	}
            //}
            //else if (x.CompareTo(_node.Value) > 0)
            //{
            //	_node.Right = remove(x, _node.Right);
            //	int r = _node.Right != null ? _node.Right.height : 0;
            //	if ((_node.Left != null) && (_node.Left.height - r >= 2))
            //	{
            //		int leftHeight = _node.Left.Left != null ? _node.Left.Left.height : 0;
            //		int rightHeight = _node.Left.Right != null ? _node.Left.Right.height : 0;
            //		if (leftHeight >= rightHeight)
            //			_node = RotateRight(_node);
            //		else
            //			_node = RotateLeftLeft(_node);
            //	}
            //}
            ///*
            //   Here, we have ended up when we are node which shall be removed.
            //   Check if there is a left-hand node, if so pick out the largest element out, and move down to the root.
            // */
            //else if (_node.Left != null)
            //{
            //	_node.Value = Max(_node.Left).Value;
            //	remove(_node.Value, _node.Left);

            //	if ((_node.Right != null) && (_node.Right.height - _node.Left.height >= 2))
            //	{
            //		int rightHeight = _node.Right.right != null ? _node.Right.right.height : 0;
            //		int leftHeight = _node.Right.Left != null ? _node.Right.Left.height : 0;

            //		if (rightHeight >= leftHeight)
            //			_node = RotateLeft(_node);
            //		else
            //			_node = RotateRightRight(_node);
            //	}
            //}

            //else
            //	_node = (_node.Left != null) ? _node.Left : _node.Right;

            //if (_node != null)
            //{
            //	int leftHeight = _node.Left != null ? _node.Left.height : 0;
            //	int rightHeight = _node.Right != null ? _node.Right.height : 0;
            //	_node.height = Math.Max(leftHeight, rightHeight) + 1;
            //}
            //return _node;
        }
示例#19
0
文件: BT_Node.cs 项目: rhsattev/ALG2
        public bool Remove(int _value, BT_Node parent)
        {
            if (_value < this.value)
            {
                if (left != null)
                    return left.Remove(_value, this);
                else
                    return false;
            }
            else if (_value > this.value)
            {
                if (right != null)
                    return right.Remove(_value, this);
                else
                    return false;
            }
            else
            {
                if (left != null && right != null)
                {
                    this.value = right.minValue();
                    right.Remove(this.value, this);
                }
                else if (parent.left == this)
                {
                    parent.left = (left != null) ? left : right;
                }
                else if (parent.right == this)
                {
                    parent.right = (left != null) ? left : right;
                }

                return true;

            }
        }
示例#20
0
 /**
  * Returns how many levels deep the deepest level in the tree is
  * (the empty tree is 0 levels deep, the tree with only one root node is 1 deep)
  * @return
  */
 public int Depth(BT_Node node)
 {
     if (node == null)
     {
         return 0;
     }
     else
     {
         // compute the depth of each subtree
         int lDepth = Depth(node.Left);
         int rDepth = Depth(node.Right);
         // use the larger one
         if (lDepth > rDepth)
             return (lDepth + 1);
         else
             return (rDepth + 1);
     }
 }