Esempio n. 1
0
        private BinaryTreeNode <T> FindParentNode(BinaryTreeNode <T> node)
        {
            var current               = Root;
            var comparison            = node.CompareTo(current);
            BinaryTreeNode <T> parent = null;

            while (comparison != 0)
            {
                if (comparison > 0) // value is bigger than current node search right subtree
                {
                    parent  = current;
                    current = current.RightChild;
                }
                else if (comparison < 0) // value is smaller than current node search left subtree
                {
                    parent  = current;
                    current = current.LeftChild;
                }

                if (current is null)
                {
                    return(parent);
                }
                else
                {
                    comparison = node.CompareTo(current);
                }
            }

            return(parent);
        }
Esempio n. 2
0
        private void SetLeftChild(BinaryTreeNode <T> target)
        {
            if (target is null)
            {
                RemoveLeftChild();
                return;
            }

            if (target.CompareTo(this) > 0)
            {
                throw new Exception("Value of left child cannot be greater than value of node");
            }

            var existingLeftChild = Neighbours.Cast <BinaryTreeNode <T> >().FirstOrDefault(n => n.IsLeftChild);

            if (existingLeftChild != null)
            {
                RemoveLeftChild();
            }

            target.IsLeftChild  = true;
            target.IsRightChild = false;
            Neighbours.Add(target);
            _leftChild = target;
        }