public static void Init() { //Node node1111 = new Node(1111); //Node node111 = new Node(111); //Node node11 = new Node(11); //Node node12 = new Node(12); //Node node1 = new Node(1); //Node root = new Node(0); //Node node2 = new Node(2); //Node node21 = new Node(21); //Node node211 = new Node(211); //root.Childs.Add(node1); //root.Childs.Add(node2); //node1.Childs.Add(node11); //node1.Childs.Add(node12); //node11.Childs.Add(node111); //node111.Childs.Add(node1111); //node2.Childs.Add(node21); //node21.Childs.Add(node211); //List<Node> path = root.Depth; //foreach (Node n in path) // Console.Write(String.Format("{0} - ", n.ToString())); //Console.WriteLine(); //Node node2111 = new Node(2111); //node2111.Childs.Add(new Node(21111)); //node211.Childs.Add(node2111); //path = root.Depth; //foreach (Node n in path) // Console.Write(String.Format("{0} - ", n.ToString())); //Console.WriteLine(); // 15 // 5 25 //2 10 22 29 // 7 12 21 23 27 30 // 6 8 28 Console.WriteLine("Print after insert Node"); Node rootRD = new Node(15); rootRD.Insert(5); rootRD.Insert(10); rootRD.Insert(25); rootRD.Insert(22); rootRD.Insert(29); rootRD.Insert(2); rootRD.Insert(7); rootRD.Insert(12); rootRD.Insert(6); rootRD.Insert(8); rootRD.Insert(21); rootRD.Insert(23); rootRD.Insert(27); rootRD.Insert(30); rootRD.Insert(28); rootRD.PrintTree(); Console.WriteLine("Is node with value 13 present? " + Node.IsPresent(rootRD, 13).ToString()); Console.WriteLine("Leave nodes are :"); rootRD.PrintLeaves(); Console.WriteLine("Outer nodes are :"); rootRD.PrintOuterNodes(); }
public static void Init() { // A // / \ // / \ // B --> C // / \ /\ // b1->D-> E-> F // / \ // G -------->H Node A = new Node('A'); Node B = new Node('B'); Node B1 = new Node('1'); Node C = new Node('C'); Node D = new Node('D'); Node E = new Node('E'); Node F = new Node('F'); Node G = new Node('G'); Node H = new Node('H'); A.Left = B; A.Right = C; B.Left = B1; B.Right = D; C.Left = E; C.Right = F; //PreOrder Traversal - Works for complete binary trees Console.WriteLine("Print populated Tree using PreOrder Traversal:"); A.ConnectUsingPreOrder(A); A.PrintTree(); //Add leaf nodes. Now tree is not a complete binary tree. D.Left = G; F.Right = H; //Using Dictionary Console.WriteLine(""); Console.WriteLine("Print populated Tree using using Dictionary:"); A.ConnectUsingDictionary(); A.PrintTree(); //Using Queue Console.WriteLine(""); Console.WriteLine("Print populated Tree using Queue:"); A.connectUsingQueue(); A.PrintTree(); ///* Constructed binary tree is // 10 // / \ // 8 2 // / \ // 3 90 //*/ //Node root = new Node(10); //root.Left = new Node(8); //root.Right = new Node(2); //root.Left.Left = new Node(3); //root.Right.Right = new Node(90); //// Populates Next pointer in all nodes //root.connectUsingQueue(); //// Let us check the values of nextRight pointers //Console.WriteLine(""); //Console.WriteLine("Following are populated nextRight pointers in \n" + "the tree (-1 is printed if there is no nextRight)"); //Console.WriteLine("Next of " + root.Data + " is " + ((root.Next != null) ? root.Next.Data : -1)); //Console.WriteLine("Next of " + root.Left.Data + " is " + ((root.Left.Next != null) ? root.Left.Next.Data : -1)); //Console.WriteLine("Next of " + root.Right.Data + " is " + ((root.Right.Next != null) ? root.Right.Next.Data : -1)); //Console.WriteLine("Next of " + root.Left.Left.Data + " is " + ((root.Left.Left.Next != null) ? root.Left.Left.Next.Data : -1)); //Console.WriteLine("Next of " + root.Right.Right.Data + " is " + ((root.Right.Right.Next != null) ? root.Right.Right.Next.Data : -1)); }