Example #1
0
        private bool FirstCommonAncestorHelperCheckContains(
            MyBinarySearchTreeNode <int> root,
            MyBinarySearchTreeNode <int> node)
        {
            if (root == null)
            {
                return(false);
            }

            if (root == node)
            {
                return(true);
            }

            if (FirstCommonAncestorHelperCheckContains(root.Left, node))
            {
                return(true);
            }
            if (FirstCommonAncestorHelperCheckContains(root.Right, node))
            {
                return(true);
            }

            return(false);
        }
        private MyBinarySearchTreeNode DeleteWithGivenRoot(MyBinarySearchTreeNode root, T data)
        {
            if (root == null)
            {
                return(null);
            }
            if (root.Data.CompareTo(data) > 0)
            {
                (root as INodeAction).SetLeftNode(DeleteWithGivenRoot(root.LeftNode, data));
            }
            else if (root.Data.CompareTo(data) < 0)
            {
                (root as INodeAction).SetRightNode(DeleteWithGivenRoot(root.RightNode, data));
            }
            else
            {
                if (root.LeftNode == null)
                {
                    return(root.RightNode);
                }
                else if (root.RightNode == null)
                {
                    return(root.LeftNode);
                }

                var minNode = FindMinValueNodeWithGivenRoot(root.RightNode);
                (root as INodeAction).SetData(minNode.Data);
                (root as INodeAction).SetRightNode(DeleteWithGivenRoot(root.RightNode, minNode.Data));
            }

            return(root);
        }
Example #3
0
        private MyBinarySearchTreeNode <int> FirstCommonAncestorHelper(
            MyBinarySearchTreeNode <int> root,
            MyBinarySearchTreeNode <int> firstNode,
            MyBinarySearchTreeNode <int> secondNode)
        {
            var firstLeft  = FirstCommonAncestorHelperCheckContains(root.Left, firstNode);
            var firstRight = FirstCommonAncestorHelperCheckContains(root.Right, firstNode);

            var secondLeft  = FirstCommonAncestorHelperCheckContains(root.Left, secondNode);
            var secondRight = FirstCommonAncestorHelperCheckContains(root.Right, secondNode);

            if ((firstLeft && secondRight) || (firstRight && secondLeft))
            {
                return(root);
            }
            if (firstLeft && secondLeft)
            {
                var result = FirstCommonAncestorHelper(root.Left, firstNode, secondNode);
                return(result ?? root);
            }
            if (firstRight && secondRight)
            {
                var result = FirstCommonAncestorHelper(root.Right, firstNode, secondNode);
                return(result ?? root);
            }

            return(null);
        }
Example #4
0
        private MyBinarySearchTreeNode <int> SuccessorHelper(
            MyBinarySearchTreeNode <int> root,
            MyBinarySearchTreeNode <int> node,
            ref bool isNext)
        {
            if (root == null)
            {
                return(null);
            }

            var leftResult = SuccessorHelper(root.Left, node, ref isNext);

            if (leftResult != null)
            {
                return(leftResult);
            }

            if (isNext)
            {
                return(root);
            }

            isNext = isNext || root.Equals(node);

            var rightResult = SuccessorHelper(root.Right, node, ref isNext);

            if (rightResult != null)
            {
                return(rightResult);
            }

            return(null);
        }
Example #5
0
        private bool ValidateBSTHelper(MyBinarySearchTreeNode <int> root)
        {
            if (root == null)
            {
                return(true);
            }

            var leftResult = ValidateBSTHelper(root.Left);

            if (!leftResult)
            {
                return(false);
            }
            var rightResult = ValidateBSTHelper(root.Right);

            if (!rightResult)
            {
                return(false);
            }

            if (root.Left != null && root.Left.Data > root.Data)
            {
                return(false);
            }
            if (root.Right != null && root.Right.Data <= root.Data)
            {
                return(false);
            }

            return(true);
        }
Example #6
0
        public MyBinarySearchTreeNode <int> FirstCommonAncestor(MyBinarySearchTree <int> tree,
                                                                MyBinarySearchTreeNode <int> firstNode, MyBinarySearchTreeNode <int> secondNode)
        {
            if (tree == null || firstNode == null || secondNode == null)
            {
                throw new ArgumentNullException();
            }

            return(FirstCommonAncestorHelper(tree.Root, firstNode, secondNode));
        }
        private MyBinarySearchTreeNode FindMaxValueNodeWithGivenRoot(MyBinarySearchTreeNode root)
        {
            var current = root;

            while (current.RightNode != null)
            {
                current = current.RightNode;
            }

            return(current);
        }
Example #8
0
        public MyBinarySearchTreeNode <int> Successor(MyBinarySearchTree <int> tree,
                                                      MyBinarySearchTreeNode <int> node)
        {
            if (tree == null || node == null)
            {
                throw new ArgumentNullException();
            }

            bool isNext = false;

            return(SuccessorHelper(tree.Root, node, ref isNext));
        }
Example #9
0
        private void GetMarkedPreOrderTraversal(MyBinarySearchTreeNode <int> root,
                                                MyStringBuilder result)
        {
            if (root == null)
            {
                result.Append("*");
                return;
            }

            result.Append(root.Data.ToString());
            GetMarkedPreOrderTraversal(root.Left, result);
            GetMarkedPreOrderTraversal(root.Right, result);
        }
 private MyBinarySearchTreeNode FindNode(MyBinarySearchTreeNode currentRoot, T data)
 {
     if (currentRoot == null || currentRoot.Data.Equals(data))
     {
         return(currentRoot);
     }
     else if (currentRoot.Data.CompareTo(data) < 0)
     {
         return(FindNode(currentRoot.RightNode, data));
     }
     else
     {
         return(FindNode(currentRoot.LeftNode, data));
     }
 }
Example #11
0
        private int CheckBalancedHelper(
            MyBinarySearchTreeNode <int> root)
        {
            if (root == null)
            {
                return(0);
            }
            var left  = CheckBalancedHelper(root.Left);
            var right = CheckBalancedHelper(root.Right);

            if (Math.Abs(left - right) > 1 || left == -1 || right == -1)
            {
                return(Int32.MinValue);
            }

            return(Math.Max(left + 1, right + 1));
        }
Example #12
0
        private List <List <int> > BSTSequencesHelper(MyBinarySearchTreeNode <int> root)
        {
            if (root == null)
            {
                return(new List <List <int> >());
            }

            var leftItems  = BSTSequencesHelper(root.Left);
            var rightItems = BSTSequencesHelper(root.Right);

            var bigger  = rightItems.Count > leftItems.Count ? rightItems : leftItems;
            var smaller = rightItems.Count <= leftItems.Count ? rightItems : leftItems;

            List <List <int> > permutations = new List <List <int> >();

            foreach (var item in bigger)
            {
                foreach (var value in smaller)
                {
                    var result  = new List <List <int> >();
                    var current = new HashSet <int>();
                    FindOrderedPermutations(current, item, value, 0, 0, result);
                    permutations.AddRange(result);
                }
                if (smaller.Count == 0)
                {
                    permutations.Add(item);
                }
            }


            foreach (var item in permutations)
            {
                item.Insert(0, root.Data);
            }

            if (permutations.Count == 0)
            {
                permutations.Add(new List <int>()
                {
                    root.Data
                });
            }

            return(permutations);
        }
Example #13
0
        private void ListOfDepthHelper(
            MyBinarySearchTreeNode <int> root, int depth,
            List <MyDoublyLinkedList <int> > lists)
        {
            if (root == null)
            {
                return;
            }

            if (lists.Count == depth)
            {
                lists.Add(new MyDoublyLinkedList <int>());
            }

            lists[depth].AddLast(new MyDoublyLinkedListNode <int>(root.Data));

            ListOfDepthHelper(root.Left, depth + 1, lists);
            ListOfDepthHelper(root.Right, depth + 1, lists);
        }
        public MyBinarySearchTreeNode Insert(T data)
        {
            var node = new MyBinarySearchTreeNode(data);

            if (Root == null)
            {
                return(Root = node);
            }

            var currentRoot = Root;

            while (currentRoot != null)
            {
                if (currentRoot.Data.CompareTo(data) < 0)
                {
                    if (currentRoot.RightNode == null)
                    {
                        break;
                    }

                    currentRoot = currentRoot.RightNode;
                }
                else
                {
                    if (currentRoot.LeftNode == null)
                    {
                        break;
                    }

                    currentRoot = currentRoot.LeftNode;
                }
            }

            if (currentRoot.Data.CompareTo(data) < 0)
            {
                return((currentRoot as INodeAction).SetRightNode(node));
            }
            else
            {
                return((currentRoot as INodeAction).SetLeftNode(node));
            }
        }
Example #15
0
        private MyBinarySearchTreeNode <int> GetNthNode(MyBinarySearchTreeNode <int> root,
                                                        ref int index, int endIndex)
        {
            if (index == endIndex)
            {
                return(root);
            }
            index++;

            MyBinarySearchTreeNode <int> leftRes = null;

            if (root.Left != null)
            {
                leftRes = GetNthNode(root.Left, ref index, endIndex);
            }
            if (leftRes != null)
            {
                return(leftRes);
            }

            return(root.Right != null?GetNthNode(root.Right, ref index, endIndex) : null);
        }
Example #16
0
        private void PathsWithSumHelper(MyBinarySearchTreeNode <int> root,
                                        Dictionary <MyBinarySearchTreeNode <int>, List <int> > map,
                                        MyBinarySearchTreeNode <int> parent)
        {
            if (root == null)
            {
                return;
            }

            var currentValues = new List <int>()
            {
                root.Data
            };

            if (parent != null)
            {
                var parentValues = map[parent];
                currentValues.AddRange(parentValues.Select(x => x + root.Data));
            }
            map[root] = currentValues;

            PathsWithSumHelper(root.Left, map, root);
            PathsWithSumHelper(root.Right, map, root);
        }
 MyBinarySearchTreeNode INodeAction.SetRightNode(MyBinarySearchTreeNode node)
 {
     return(rightNode = node);
 }
 public MyBinarySearchTree(T rootData)
 {
     Root = new MyBinarySearchTreeNode(rootData);
 }
 MyBinarySearchTreeNode INodeAction.SetLeftNode(MyBinarySearchTreeNode node)
 {
     return(leftNode = node);
 }
 public void Delete(MyBinarySearchTreeNode node)
 {
     Delete(node.Data);
 }