Esempio n. 1
0
        private void PreOrderIteractive(TreeNodeInt node)
        {
            Stack <TreeNodeInt> stack = new Stack <TreeNodeInt>();

            stack.Push(node);

            while (stack.Count > 0)
            {
                node = stack.Pop();

                //process node


                //add right child
                if (node.right != null)
                {
                    stack.Push(node.right);
                }

                //add left child
                if (node.left != null)
                {
                    stack.Push(node.left);
                }
            }
        }
Esempio n. 2
0
        //http://leetcode.com/2010/04/binary-search-tree-in-order-traversal.html
        private void InOrderIteractive(TreeNodeInt node, List <int> list)
        {
            if (node == null)
            {
                return;
            }

            Stack <TreeNodeInt> stack = new Stack <TreeNodeInt>();

            while (stack.Count > 0 || node != null)
            {
                //while there's a left child, keep going left
                if (node != null)
                {
                    stack.Push(node);
                    node = node.left;
                }
                //if there's no more left, process current node and then right side
                else
                {
                    node = stack.Pop();
                    list.Add(node.data);
                    node = node.right;
                }
            }
        }
Esempio n. 3
0
        public static bool UnbalancedBinary(TreeNodeInt node)
        {
            if (node == null)
            {
                return true;
            } else
            {
                bool result = true;

                if (node.left != null) result = result && (node.left.data <= node.data);
                if (node.right != null) result = result && (node.right.data > node.data);

                return result && UnbalancedBinary(node.left) && UnbalancedBinary(node.right);
            }
        }
Esempio n. 4
0
        public static void BuildTree()
        {
            TreeNodeInt n20 = new TreeNodeInt(20); TreeNodeInt n10 = new TreeNodeInt(10); TreeNodeInt n25 = new TreeNodeInt(25); TreeNodeInt n30 = new TreeNodeInt(30);
            TreeNodeInt n5 = new TreeNodeInt(5);

            n20.left = n10;
            n20.right = n30;
            //n10.right = n25;
            //n30.left = n5;

            int max, min;
            //Console.WriteLine("CheckBalanced = " + CheckBalanced(n20, out max, out min));
            //Console.WriteLine("UnbalancedBinary = " + UnbalancedBinary(n20));
            Console.WriteLine("CheckBalancedBook = " + CheckBalancedBook(n20, int.MinValue, int.MaxValue));
        }
Esempio n. 5
0
        public static void BuildTree()
        {
            TreeNodeInt n20 = new TreeNodeInt(20); TreeNodeInt n10 = new TreeNodeInt(10); TreeNodeInt n25 = new TreeNodeInt(25); TreeNodeInt n30 = new TreeNodeInt(30);
            TreeNodeInt n5 = new TreeNodeInt(5);

            n20.left  = n10;
            n20.right = n30;
            //n10.right = n25;
            //n30.left = n5;

            int max, min;

            //Console.WriteLine("CheckBalanced = " + CheckBalanced(n20, out max, out min));
            //Console.WriteLine("UnbalancedBinary = " + UnbalancedBinary(n20));
            Console.WriteLine("CheckBalancedBook = " + CheckBalancedBook(n20, int.MinValue, int.MaxValue));
        }
Esempio n. 6
0
        private void InOrderRecursive(TreeNodeInt node, List <int> list)
        {
            if (node == null)
            {
                return;
            }

            //process left child
            InOrderRecursive(node.left, list);

            //process self
            list.Add(node.data);

            //process right child
            InOrderRecursive(node.right, list);
        }
Esempio n. 7
0
        //http://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion-and-without-stack/
        private void InOrderIteractiveNoStack(TreeNodeInt current, List <int> list)
        {
            if (current == null)
            {
                return;
            }

            TreeNodeInt predecessor = null;

            while (current != null)
            {
                //if there's no left child, process node and move right
                if (current.left == null)
                {
                    list.Add(current.data);
                    current = current.right;
                }
                else
                {
                    //there's a left node

                    //find the inorder predecessor of current
                    predecessor = current.left;
                    while (predecessor.right != null && predecessor.right != current)
                    {
                        predecessor = predecessor.right;
                    }

                    // Make current as right child of its inorder predecessor
                    if (predecessor.right == null)
                    {
                        //we can now move to the left side of the tree because prodecessor.right hold a referent to current
                        //and at some point we'll process predecessor and the next after that is current
                        predecessor.right = current;
                        current           = current.left;
                    }
                    else
                    {
                        //Revert the changes made in if part to restore the original
                        //tree i.e., fix the right child of predecssor
                        predecessor.right = null;
                        list.Add(current.data);
                        current = current.right;
                    }
                }
            }
        }
        /// <summary>
        /// Adds an element to the tree.
        /// </summary>
        public void Add(int x)
        {
            //empty tree
            if (root == null)
            {
                root = new TreeNodeInt(x);
                return;
            }

            bool found = false;
            TreeNodeInt current = root;

            while (!found)
            {
                if (x <= current.data)
                {
                    //found empty left spot
                    if (current.left == null)
                    {
                        current.left = new TreeNodeInt(x);
                        found = true;
                    }
                    else
                    {
                        //go left on the tree
                        current = current.left;
                    }
                }
                else
                {
                    //found empty left spot
                    if (current.right == null)
                    {
                        current.right = new TreeNodeInt(x);
                        found = true;
                    }
                    else
                    {
                        //go left on the tree
                        current = current.right;
                    }
                }
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Adds an element to the tree.
        /// </summary>
        public void Add(int x)
        {
            //empty tree
            if (root == null)
            {
                root = new TreeNodeInt(x);
                return;
            }

            bool        found   = false;
            TreeNodeInt current = root;

            while (!found)
            {
                if (x <= current.data)
                {
                    //found empty left spot
                    if (current.left == null)
                    {
                        current.left = new TreeNodeInt(x);
                        found        = true;
                    }
                    else
                    {
                        //go left on the tree
                        current = current.left;
                    }
                }
                else
                {
                    //found empty left spot
                    if (current.right == null)
                    {
                        current.right = new TreeNodeInt(x);
                        found         = true;
                    }
                    else
                    {
                        //go left on the tree
                        current = current.right;
                    }
                }
            }
        }
Esempio n. 10
0
        public static bool CheckBalancedBook(TreeNodeInt node, int min, int max)
        {
            if (node == null)
            {
                return(true);
            }

            if (node.data <= min || node.data > max)
            {
                return(false);
            }

            if (!CheckBalancedBook(node.left, min, node.data) || !CheckBalancedBook(node.right, node.data, max))
            {
                return(false);
            }

            return(true);
        }
Esempio n. 11
0
        public static bool CheckBalancedBook(TreeNodeInt node, int min, int max)
        {
            if (node == null)
            {
                return true;
            }

            if (node.data <= min || node.data > max)
            {
                return false;
            }

            if (!CheckBalancedBook(node.left, min, node.data) || !CheckBalancedBook(node.right, node.data, max))
            {
                return false;
            }

            return true;
        }
Esempio n. 12
0
        public static bool UnbalancedBinary(TreeNodeInt node)
        {
            if (node == null)
            {
                return(true);
            }
            else
            {
                bool result = true;

                if (node.left != null)
                {
                    result = result && (node.left.data <= node.data);
                }
                if (node.right != null)
                {
                    result = result && (node.right.data > node.data);
                }

                return(result && UnbalancedBinary(node.left) && UnbalancedBinary(node.right));
            }
        }
Esempio n. 13
0
        public static bool CheckBalanced(TreeNodeInt node, out int max, out int min)
        {
            max = node.data;
            min = node.data;
            int  maxLeft = 0, maxRight = 0;
            int  minLeft = 0, minRight = 0;
            bool result = true;

            if (node.left != null)
            {
                result = result && CheckBalanced(node.left, out maxLeft, out minLeft) && (maxLeft <= node.data);
                max    = Math.Max(max, maxLeft);
                min    = Math.Min(min, minLeft);
            }

            if (node.right != null)
            {
                result = result && CheckBalanced(node.right, out maxRight, out minRight) && (minRight > node.data);
                max    = Math.Max(max, maxRight);
                min    = Math.Min(min, minRight);
            }

            return(result);
        }
Esempio n. 14
0
        public static bool CheckBalanced(TreeNodeInt node, out int max, out int min)
        {
            max = node.data;
            min = node.data;
            int maxLeft = 0, maxRight = 0;
            int minLeft = 0, minRight = 0;
            bool result = true;

            if (node.left != null)
            {
                result = result && CheckBalanced(node.left, out maxLeft, out minLeft) && (maxLeft <= node.data);
                max = Math.Max(max, maxLeft);
                min = Math.Min(min, minLeft);
            }

            if (node.right != null)
            {
                result = result && CheckBalanced(node.right, out maxRight, out minRight) && (minRight > node.data);
                max = Math.Max(max, maxRight);
                min = Math.Min(min, minRight);
            }

            return result;
        }
        //http://leetcode.com/2010/04/binary-search-tree-in-order-traversal.html
        private void InOrderIteractive(TreeNodeInt node, List<int> list)
        {
            if (node == null)
            {
                return;
            }

            Stack<TreeNodeInt> stack = new Stack<TreeNodeInt>();

            while (stack.Count > 0 || node != null)
            {
                //while there's a left child, keep going left
                if (node != null)
                {
                    stack.Push(node);
                    node = node.left;
                }
                //if there's no more left, process current node and then right side
                else
                {
                    node = stack.Pop();
                    list.Add(node.data);
                    node = node.right;
                }
            }
        }
        private void PreOrderIteractive(TreeNodeInt node)
        {
            Stack<TreeNodeInt> stack = new Stack<TreeNodeInt>();
            stack.Push(node);

            while (stack.Count > 0)
            {
                node = stack.Pop();

                //process node

                //add right child
                if (node.right != null) stack.Push(node.right);

                //add left child
                if (node.left != null) stack.Push(node.left);
            }
        }
        private void InOrderRecursive(TreeNodeInt node, List<int> list)
        {
            if (node == null)
            {
                return;
            }

            //process left child
            InOrderRecursive(node.left, list);

            //process self
            list.Add(node.data);

            //process right child
            InOrderRecursive(node.right, list);
        }
        //http://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion-and-without-stack/
        private void InOrderIteractiveNoStack(TreeNodeInt current, List<int> list)
        {
            if (current == null)
            {
                return;
            }

            TreeNodeInt predecessor = null;

            while (current != null)
            {
                //if there's no left child, process node and move right
                if (current.left == null)
                {
                    list.Add(current.data);
                    current = current.right;
                }
                else
                {
                    //there's a left node

                    //find the inorder predecessor of current
                    predecessor = current.left;
                    while (predecessor.right != null && predecessor.right != current)
                        predecessor = predecessor.right;

                    // Make current as right child of its inorder predecessor
                    if (predecessor.right == null)
                    {
                        //we can now move to the left side of the tree because prodecessor.right hold a referent to current
                        //and at some point we'll process predecessor and the next after that is current
                        predecessor.right = current;
                        current = current.left;
                    }
                    else
                    {
                        //Revert the changes made in if part to restore the original
                        //tree i.e., fix the right child of predecssor
                        predecessor.right = null;
                        list.Add(current.data);
                        current = current.right;

                    }
                }
            }
        }