Ejemplo n.º 1
0
 public void InOrderTraversal(BTNode node)
 {
     if (node == null)
         return;
     InOrderTraversal(node.left);
     Console.Out.Write(node.value);
     InOrderTraversal(node.right);
 }
Ejemplo n.º 2
0
        public BTNode ConstructBalancedRecurs(int[] values, int start, int end)
        {
            if (start > end)
                return null;

            int mid = end - start / 2;
            BTNode node = new BTNode(null, null, values[mid]);

            node.left = ConstructBalancedRecurs(values, start, mid - 1);
            node.right = ConstructBalancedRecurs(values, mid + 1, end);
            return node;
        }
Ejemplo n.º 3
0
        public int CheckLevel(BTNode node, int value)
        {
            if (node == null)
                return 0;

            if (value == (int)node.value)
                return 1;

            int left = CheckLevel(node.left, value);
            int right = CheckLevel(node.right, value);

            return Math.Max(left+1, right+1);
        }
Ejemplo n.º 4
0
        public bool IsBst(BTNode node)
        {
            if (node == null)
                return true;

            if (node.left != null && (int)node.left.value > (int)node.value)
                return false;

            if (node.right != null && (int)node.right.value < (int)node.value)
                return false;

            if (!IsBST(node.left) || !IsBST(node.right))
                return false;

            return true;
        }
Ejemplo n.º 5
0
        public void BFS(BTNode node)
        {
            if (node == null)
                return;

            Queue<BTNode> queue = new Queue<BTNode>();
            queue.Enqueue(node);
            while (queue.Count != 0)
            {
                BTNode current = queue.Dequeue();

                Console.Out.Write(current.value);
                if (node.left != null) queue.Enqueue(current.left);
                if (node.right != null) queue.Enqueue(current.right);
            }
        }
Ejemplo n.º 6
0
        public void DFS(BTNode node)
        {
            if (node == null)
                return;

            Stack<BTNode> stack = new Stack<BTNode>();
            stack.Push(node);

            while(stack.Count != 0)
            {
                BTNode current = stack.Pop();

                Console.Out.Write(node.value);
                if (node.left != null) stack.Push(current.left);
                if (node.right != null) stack.Push(current.right);
            }
        }
Ejemplo n.º 7
0
        public BTNode Delete(BTNode node, int value)
        {
            //Find the value
            if (value < (int)node.value)
                Delete(node.left, value);
            if (value > (int)node.value)
                Delete(node.right, value);

            if ((int)node.value == value)
            {
                //No children
                if (node.left == null && node.right == null)
                    node = null;

                //1 child
                else if (node.left == null)
                {
                    BTNode temp = node.right;
                    node = temp;
                    temp = null;
                }
                else if (node.right == null)
                {
                    BTNode temp = node.left;
                    node = temp;
                    temp = null;
                }

                //2 Children
                else
                {
                    BTNode temp = FindMin(node);
                    node = temp;
                    node.right = Delete(node.right, (int)temp.value);
                }
            }

            return node;
        }
Ejemplo n.º 8
0
        public BTNode Search(BTNode node, int value)
        {
            if ((int)node.value == value)
            {
                return node;
            }

            if (value < (int)node.value)
                Search(node.left, value);
            if (value > (int)node.value)
                Search(node.right, value);

            return null;
        }
Ejemplo n.º 9
0
        public BTNode InsertRec(BTNode node, int value)
        {
            if (node == null)
            {
                node = new BTNode(null, null, value);
                return node;
            }

            if (value < (int)node.value)
                node.left = InsertRec(node.left, value);
            if (value > (int)node.value)
                node.right = InsertRec(node.right, value);

            return node;
        }
Ejemplo n.º 10
0
        public BTNode FindMin(BTNode node)
        {
            if (node.left == null)
                return node;
            else
                FindMin(node.left);

            return node;
        }
Ejemplo n.º 11
0
 public BinaryTree(BTNode root)
 {
     Root = root;
 }
Ejemplo n.º 12
0
        //Check if BST... How about inorder traversal and checking if its sorted???
        public bool IsBST(BTNode node)
        {
            if (node == null)
                return true;

            if (IsSubTreeLess(node.left, (int)node.value) &&
                IsSubTreeGreater(node.right, (int)node.value) &&
                IsBST(node.left) &&
                IsBST(node.right))
                return true;
            else
                return false;
        }
Ejemplo n.º 13
0
 public BTNode(BTNode left, BTNode right, int value)
 {
     this.left = left;
     this.right = right;
     this.value = value;
 }
Ejemplo n.º 14
0
 public BST(BTNode root)
     : base(root)
 {
 }
Ejemplo n.º 15
0
        public BTNode FindMax(BTNode node)
        {
            if (node.right == null)
                return node;
            else
                FindMin(node.right);

            return node;
        }
Ejemplo n.º 16
0
        public int FindHeight(BTNode node)
        {
            if (node == null)
                //+1 will cause a leaf node will return 1, so need this to balance out
                return -1;

            return Math.Max(FindHeight(node.left), FindHeight(node.right)) + 1;
        }
Ejemplo n.º 17
0
        private bool IsSubTreeLess(BTNode node, int value)
        {
            if (node == null)
                return true;

            if (value > (int)node.value &&
                IsSubTreeLess(node.left, value) && IsSubTreeLess(node.right, value))
                return true;
            else
                return false;
        }