Ejemplo n.º 1
0
        private void EnqueueRecur(int index, Node node)
        {
            //merge the node with the current order-0 node to create order-1 tree
            if (!string.IsNullOrEmpty(node.elem))
            {
                var newTree = heap[index];
                newTree.children.Add(node);

                EnqueueRecur(index + 1, newTree);
            }
        }
Ejemplo n.º 2
0
        public void Enqueue(string word)
        {
            Node node = new Node(word);

            if (heap.Count == 0)
                heap.Add(node);
            else
            {
                EnqueueRecur(0, node);

                //set the root to blank string
                var root = heap[0];
                root.elem = "";
            }
        }
Ejemplo n.º 3
0
        public static int sameTree(Node a, Node b)
        {
            if (a != null && b != null)
            {
                if (a.Key != b.Key)
                    return 0;
            }

            var leftSame = 1;
            var rightSame = 1;

            if (a.Left != null && b.Left != null)
            {
                leftSame = sameTree(a.Left, b.Left);

                if (leftSame == 0)
                    return 0;
            }
            else
            {
                if (!(a.Left == null && b.Left == null))
                    return 0;
            }

            if (a.Right != null && b.Right != null)
            {
                rightSame = sameTree(a.Right, b.Right);

                if (rightSame == 0)
                    return 0;
            }
            else
            {
                if (!(a.Left == null && b.Left == null))
                    return 0;
            }

            return 1;
        }
Ejemplo n.º 4
0
 public void Insert(int data)
 {
     head = Insert(head, data);
 }
Ejemplo n.º 5
0
 private int Size(Node node)
 {
     if (node == null)
         return 0;
     else
         return Size(node.Left) + Size(node.Right) + 1;
 }
Ejemplo n.º 6
0
        private string PrintTree(Node node)
        {
            if (node == null)
                return "";

            return PrintTree(node.Left) + node.Key + " " + PrintTree(node.Right);
        }
Ejemplo n.º 7
0
        private string PrintPostOrder(Node node)
        {
            if (node == null)
                return "";

            return PrintPostOrder(node.Left) + PrintPostOrder(node.Right) + node.Key;
        }
Ejemplo n.º 8
0
        private void printPathsRecur(Node node, int[] path, int pathLength)
        {
            if(node.Left == null && node.Right == null) {
                path[pathLength] = node.Key;

                var pathString = path.Take(pathLength + 1);
                return;
            }

            if (node != null)
            {
                path[pathLength] = node.Key;
                pathLength++;

                if (node.Left != null)
                    printPathsRecur(node.Left, path, pathLength);

                if (node.Right != null)
                    printPathsRecur(node.Right, path, pathLength);
            }
        }
Ejemplo n.º 9
0
        private int hasPathSum(Node node, int sum)
        {
            if (node.Left == null && node.Right == null)
                return sum - node.Key == 0 ? 1 : 0;

            var leftPathSum = 0;
            var rightPathSum = 0;

            if (node.Left != null)
                leftPathSum = hasPathSum(node.Left, sum - node.Key);

            if (node.Right != null)
                rightPathSum = hasPathSum(node.Right, sum - node.Key);

            if (leftPathSum == 1 || rightPathSum == 1)
                return 1;
            else
                return 0;
        }
Ejemplo n.º 10
0
        private void Mirror(Node node)
        {
            if (node != null)
            {
                Node temp;

                temp = node.Left;
                node.Left = node.Right;
                node.Right = temp;

                if (node.Left != null)
                    Mirror(node.Left);

                if (node.Right != null)
                    Mirror(node.Right);
            }
        }
Ejemplo n.º 11
0
        private int MinValue(Node node)
        {
            if (node.Left == null)
                return node.Key;

            return MinValue(node.Left);
        }
Ejemplo n.º 12
0
        private int MaxDepth(Node node)
        {
            var leftDepth = 0;
            var rightDepth = 0;

            if (node == null)
                return 0;
            else
            {
                //get max depth of left tree
                leftDepth = MaxDepth(node.Left) + 1;

                //get max depth of left tree
                rightDepth = MaxDepth(node.Right) + 1;
            }

            return leftDepth > rightDepth ? leftDepth : rightDepth;
        }
Ejemplo n.º 13
0
        private bool Lookup(Node head, int value)
        {
            if (head == null)
                return false;
            else
            {
                //compare value to head value
                if (value == head.Key)
                    return true;

                //compare lookup value to current node key value
                if (value <= head.Key)
                    return Lookup(head.Left, value);
                else
                    return Lookup(head.Right, value);
            }
        }
Ejemplo n.º 14
0
        private Node Insert(Node node, int value)
        {
            //if head is empy, just insert the node
            if (node == null)
                return new Node(value);
            else
            {
                //head is not empty

                //compare current key value to new node's key value
                if (value <= node.Key)

                    //new node key is less than current node key
                    node.Left = Insert(node.Left, value);
                else
                    //new node key is greater than current node key
                    node.Right = Insert(node.Right, value);
            }

            return node;
        }
Ejemplo n.º 15
0
 public BST()
 {
     head = null;
 }
Ejemplo n.º 16
0
 private void printPaths(Node node)
 {
     if (node != null)
     {
         int[] path = new int[10];
         printPathsRecur(node, path, 0);
     }
 }
Ejemplo n.º 17
0
        private void doubleTree(Node node)
        {
            //create new node
            Node duplicate = new Node(node.Key);
            duplicate.Left = node.Left;
            duplicate.Right = node.Right;

            node.Left = duplicate;

            if (duplicate.Left != null)
                doubleTree(duplicate.Left);

            if (duplicate.Right != null)
                doubleTree(duplicate.Right);
        }