Exemple #1
0
        public ChildType GetMyBranchType(TreeNode node)
        {
            TreeNode parent = Parent(node);
            if (parent == null)
                return ChildType.NONE;

            if (parent.Left == node)
                return ChildType.LEFT_CHILD;
            else
                return ChildType.RIGHT_CHILD;
        }
Exemple #2
0
        public void Insert(int value)
        {
            if (root == null)
            {
                root = new TreeNode(value);
                return;
            }

            TreeNode newNode = new TreeNode(value);

            TreeNode currNode = root;
            TreeNode nodeInsertAt = null;
            while (currNode != null)
            {
                nodeInsertAt = currNode;
                if (newNode.Value <= currNode.Value) currNode = currNode.Left;
                else currNode = currNode.Right;
            }
            if (newNode.Value <= nodeInsertAt.Value)
                nodeInsertAt.Left = newNode;
            else
                nodeInsertAt.Right = newNode;
        }
Exemple #3
0
 public void Clear()
 {
     this.root = null;
 }
Exemple #4
0
        private TreeNode MinValNode(TreeNode root)
        {
            if (root == null)
                return null;

            if (root.Left == null)
                return root;

            TreeNode curr = root;
            while (curr.Left != null)
                curr = curr.Left;
            return curr;
        }
Exemple #5
0
        private string PreOrderPrintHelper(TreeNode root)
        {
            if (root == null)
                return "";

            string msg = "";
            msg = msg + " " + root.Value;
            msg = msg + PreOrderPrintHelper(root.Left);
            msg = msg + PreOrderPrintHelper(root.Right);
            return msg;
        }
Exemple #6
0
        //Path counting
        private int HeightHelper2(TreeNode currNode)
        {
            if (currNode == null) return 0;
            int leftHeight = 0;
            if (currNode.Left != null)
                leftHeight = 1 + HeightHelper2(currNode.Left);
            int rightHeight = 0;
            if (currNode.Right != null)
                rightHeight = 1 + HeightHelper2(currNode.Right);

            return leftHeight > rightHeight ? leftHeight : rightHeight;
        }
Exemple #7
0
        private TreeNode MaxValNode(TreeNode root)
        {
            if (root == null)
                return null;

            if (root.Right == null)
                return root;

            TreeNode curr = root;
            while (curr.Right != null)
                curr = curr.Right;
            return curr;
        }
Exemple #8
0
 //Nodes counting
 private int HeightHelper(TreeNode currNode)
 {
     if (currNode == null)
         return 0;
     else
         return 1 + compareMax(HeightHelper(currNode.Left), HeightHelper(currNode.Right));
 }
Exemple #9
0
 public BSTClass(int rootValue)
 {
     root = new TreeNode(rootValue);
 }
Exemple #10
0
        private void DeleteHelper(TreeNode ToDelete)
        {
            TreeNode toSplice;
            if (ToDelete.Left == null || ToDelete.Right == null)
                toSplice = ToDelete;
            else
                toSplice = SuccessorNode(ToDelete);

            TreeNode childNode;
            if (toSplice.Left != null)
                childNode = toSplice.Left;
            else
                childNode = toSplice.Right;

            TreeNode toSpliceParent = Parent(toSplice);
            if (toSpliceParent == null)
                root = childNode;
            else
            {
                ChildType ToSpliceBranch = GetMyBranchType(toSplice);
                if (ToSpliceBranch == ChildType.LEFT_CHILD)
                    toSpliceParent.Left = childNode;
                else
                    toSpliceParent.Right = childNode;
            }

            if (toSplice != ToDelete)
                ToDelete.Value = toSplice.Value;
        }
Exemple #11
0
        public TreeNode SuccessorNode(TreeNode node)
        {
            if (node == null)
                return null;

            if (node.Right != null)
                return MinValNode(node.Right);
            else
            {
                TreeNode parent = Parent(node);
                while (parent != null && GetMyBranchType(node) == ChildType.RIGHT_CHILD)
                {
                    node = parent;
                    parent = Parent(parent);
                }
                return parent;
            }
        }
Exemple #12
0
        public TreeNode Parent(TreeNode node)
        {
            if (node == null || node == root)
                return null;

            TreeNode parent = root;
            TreeNode curr;
            if (node.Value <= parent.Value)
                curr = parent.Left;
            else
                curr = parent.Right;

            while (curr != null)
            {
                if (node.Value == curr.Value)
                    return parent;
                else if (node.Value <= curr.Value)
                {
                    parent = curr;
                    curr = curr.Left;
                }
                else
                {
                    parent = curr;
                    curr = curr.Right;
                }
            }
            return null;
        }