Esempio n. 1
0
        static void Main(string[] args)
        {

            int numberOfNodes = 7;
            int[] values = new int[numberOfNodes];
            Random r = new Random();
            r.Next(numberOfNodes);

            for (int i = 0; i < numberOfNodes; i++)
                values[i] = r.Next(numberOfNodes);

            BinaryTree b = new BinaryTree(values);

            int searchFor = 99; // r.Next(numberOfNodes);

            Console.WriteLine(string.Format("BFS searching for {0} result: {1}", searchFor, BreadthFirstSearch(b, searchFor)));

            //Console.WriteLine(string.Format("DFS searching for {0} result: {1}", searchFor, DFS(b, searchFor)));

            Console.Write("PreOrder: ");
            DFSPreOrder(b);
            Console.WriteLine("\n");

            Console.Write("InOrder: ");
            DFSInOrder(b);
            Console.WriteLine("\n");

            Console.Write("PostOrder: ");
            DFSPostOrder(b);
            Console.WriteLine("\n");


            if (Debugger.IsAttached)
                Console.ReadLine();
        }
Esempio n. 2
0
        static void DFSPostOrder(BinaryTree node)
        {
            if (node == null)
                return;

            DFSPreOrder(node.Left);
            DFSPreOrder(node.Right);
            Console.Write(string.Format(" {0} ", node.Value));
        }
Esempio n. 3
0
        static bool DFS(BinaryTree node, int searchFor)
        {

            if (node == null)
                return false;

            if (node.Value == searchFor)
                return true;

            return DFS(node.Left, searchFor)
                || DFS(node.Right, searchFor);

        }
Esempio n. 4
0
        static bool DepthFirstSearchIterative(BinaryTree node, int searchFor)
        {
            Stack<BinaryTree> nodeStack = new Stack<BinaryTree>();
            nodeStack.Push(node);

            while (nodeStack.Count > 0)
            {
                BinaryTree current = nodeStack.Pop();

                if (current.Value == searchFor)
                    return true;

                if (current.Right != null)
                    nodeStack.Push(current.Right);

                if (current.Left != null)
                    nodeStack.Push(current.Left);
            }

            return false;
        }
Esempio n. 5
0
 void Load(BinaryTree tree, int[] values, int index)
 {
     Value = values[index];
     if (index * 2 + 1 < values.Length)
     {
         Left = new BinaryTree(values, index * 2 + 1);
     }
     if (index * 2 + 2 < values.Length)
     {
         Right = new BinaryTree(values, index * 2 + 2);
     }
 }
Esempio n. 6
0
        static bool BreadthFirstSearch(BinaryTree root, int searchFor)
        {
            Queue<BinaryTree> queue = new Queue<BinaryTree>();

            queue.Enqueue(root);

            int count = 0;
            while (queue.Count > 0)
            {
                BinaryTree current = queue.Dequeue();

                if (current == null)
                    continue;

                Console.WriteLine(string.Format("{3}: {0} <-- {1} --> {2}",
                    current.Left == null ? "" : current.Left.Value.ToString(),
                    current.Value,
                    current.Right == null ? "" : current.Right.Value.ToString(),
                    count++
                    ));

                queue.Enqueue(current.Left);
                queue.Enqueue(current.Right);

                if (current.Value == searchFor)
                    return true;
            }
            return false;
        }
Esempio n. 7
0
        public int CompareTo(Object obj)
        {
            BinaryTree <T> tree = (BinaryTree <T>)obj;

            return(this.Root.CompareTo(tree.Root));
        }