Ejemplo n.º 1
0
        public void PostorderNoRecursion(BinTreeNode tree, List <long> orderList)
        {
            Stack <BinTreeNode>   stack   = new Stack <BinTreeNode>();
            HashSet <BinTreeNode> visited = new HashSet <BinTreeNode>();

            stack.Push(tree);
            while (stack.Count > 0)
            {
                BinTreeNode crt = stack.Peek();
                if (!visited.Contains(crt))
                {
                    visited.Add(crt);
                    if (null != crt.Right)
                    {
                        stack.Push(crt.Right);
                    }
                    if (null != crt.Left)
                    {
                        stack.Push(crt.Left);
                    }
                }
                else
                {
                    orderList.Add(crt.Info);
                    stack.Pop();
                }
            }
        }
Ejemplo n.º 2
0
        public long[] BreadthFirst(BinTreeNode tree)
        {
            Queue <BinTreeNode> queue   = new Queue <BinTreeNode>();
            Queue <long>        results = new Queue <long>();


            if (null != tree)
            {
                queue.Enqueue(tree);

                while (queue.Count > 0)
                {
                    BinTreeNode node = queue.Dequeue();
                    if (null != node.Left)
                    {
                        queue.Enqueue(node.Left);
                    }
                    if (null != node.Right)
                    {
                        queue.Enqueue(node.Right);
                    }
                    results.Enqueue(node.Info);
                }
            }

            return(results.ToArray());
        }
Ejemplo n.º 3
0
        public long[] DepthFirst(BinTreeNode tree)
        {
            Stack <BinTreeNode> stack   = new Stack <BinTreeNode>();
            Queue <long>        results = new Queue <long>();

            if (null != tree)
            {
                stack.Push(tree);

                while (stack.Count > 0)
                {
                    BinTreeNode node = stack.Pop();
                    if (null != node.Right)
                    {
                        stack.Push(node.Right);
                    }
                    // push left last to search it first
                    if (null != node.Left)
                    {
                        stack.Push(node.Left);
                    }
                    results.Enqueue(node.Info);
                }
            }

            return(results.ToArray());
        }
Ejemplo n.º 4
0
        /// <summary>
        /// sum all values on each level, for a BST tree
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public long[] LevelSum(BinTreeNode node)
        {
            List <long> sums = new List <long>();

            int level = 0;

            LevelSum(node, level, sums);

            return(sums.ToArray());
        }
Ejemplo n.º 5
0
        public long [] Inorder(BinTreeNode tree)
        {
            List <long> results = new List <long>();

            if (null != tree)
            {
                results.AddRange(Inorder(tree.Left));
                results.Add(tree.Info);
                results.AddRange(Inorder(tree.Right));
            }

            return(results.ToArray());
        }
Ejemplo n.º 6
0
        public bool IsBST(BinTreeNode tree, out long min, out long max)
        {
            min = max = long.MinValue;

            if (null == tree ||
                (null != tree.Left && tree.Left.Info > tree.Info) ||
                (null != tree.Right && tree.Right.Info < tree.Info)
                )
            {
                return(false);
            }

            min = max = tree.Info;

            if (null != tree.Left)
            {
                long minLeft, maxLeft;
                if (!IsBST(tree.Left, out minLeft, out maxLeft))
                {
                    return(false);
                }

                if (maxLeft > tree.Info)
                {
                    return(false);
                }

                min = minLeft;
            }

            if (null != tree.Right)
            {
                long minRight, maxRight;
                if (!IsBST(tree.Right, out minRight, out maxRight))
                {
                    return(false);
                }

                if (minRight < tree.Info)
                {
                    return(false);
                }

                max = maxRight;
            }

            return(true);
        }
Ejemplo n.º 7
0
        private void LevelSum(BinTreeNode node, int level, List <long> sums)
        {
            if (null == node)
            {
                return;
            }

            // add to sums[level]
            if (sums.Count <= level)
            {
                sums.Add(node.Info);
            }
            else
            {
                sums[level] += node.Info;
            }

            LevelSum(node.Left, level + 1, sums);
            LevelSum(node.Right, level + 1, sums);
        }
Ejemplo n.º 8
0
        public void PreorderNoRecursion(BinTreeNode tree, List <long> orderList)
        {
            Stack <BinTreeNode> stack = new Stack <BinTreeNode>();

            stack.Push(tree);

            while (stack.Count > 0)
            {
                BinTreeNode crt = stack.Pop();
                orderList.Add(crt.Info);
                if (null != crt.Right)
                {
                    stack.Push(crt.Right);
                }
                if (null != crt.Left)
                {
                    stack.Push(crt.Left);
                }
            }
        }
Ejemplo n.º 9
0
        public BinTreeNode GetBSTFromPreorder(long[] elements)
        {
            if (elements.Length == 0)
            {
                return(null);
            }
            BinTreeNode root = new BinTreeNode(elements[0]);

            //List<long> leftElements = new List<long>();
            //List<long> rightElements = new List<long>();

            int idx = 1;
            int leftMaxIdx = 0, rightMaxIdx = 0;;

            while (idx < elements.Length && elements[idx] < root.Info)
            {
                idx++;
            }
            leftMaxIdx = idx - 1;


            while (idx < elements.Length && elements[idx] > root.Info)
            {
                idx++;
            }
            rightMaxIdx = idx - 1;

            if (idx != elements.Length)
            {
                throw new System.InvalidOperationException("no matching BST for the given preorder!");
            }

            long[] leftElements  = elements.Skip(1).Take(leftMaxIdx).ToArray();
            long[] rightElements = elements.Skip(1 + leftMaxIdx).Take(rightMaxIdx - leftMaxIdx).ToArray();

            root.Left  = GetBSTFromPreorder(leftElements);
            root.Right = GetBSTFromPreorder(rightElements);


            return(root);
        }
Ejemplo n.º 10
0
        // min value for null childrens
        public BinTreeNode(long info, long left, long right)
        {
            this.Info = info;
            if (long.MinValue != left)
            {
                this.Left = new BinTreeNode(left);
            }
            else
            {
                Left = null;
            }

            if (long.MinValue != right)
            {
                this.Right = new BinTreeNode(right);
            }
            else
            {
                this.Left = null;
            }
        }
Ejemplo n.º 11
0
 // building a tree
 public BinTreeNode(long info)
 {
     this.Info = info;
     Left      = null;
     Right     = null;
 }