Esempio n. 1
0
        public BinaryTreeExample()
        {
            var compareChoice = 12; //(11 By Id | 12  By Marks)

            BinaryTree <Student> binaryTree = new BinaryTree <Student>()
            {
                new Student {
                    StudentId = 1001, Marks = 75, CompareChoice = compareChoice
                },
                new Student {
                    StudentId = 1002, Marks = 10, CompareChoice = compareChoice
                },
                new Student {
                    StudentId = 1003, Marks = 20, CompareChoice = compareChoice
                },
            };

            binaryTree.Add(new Student {
                StudentId = 1004, Marks = 10, CompareChoice = compareChoice
            });
            binaryTree.Add(new Student {
                StudentId = 1005, Marks = 50, CompareChoice = compareChoice
            });
            binaryTree.Add(new Student {
                StudentId = 1006, Marks = 65, CompareChoice = compareChoice
            });
            binaryTree.Add(new Student {
                StudentId = 1007, Marks = 40, CompareChoice = compareChoice
            });
            binaryTree.Add(new Student {
                StudentId = 1008, Marks = 50, CompareChoice = compareChoice
            });
            binaryTree.Add(new Student {
                StudentId = 1009, Marks = 5, CompareChoice = compareChoice
            });
            binaryTree.Add(new Student {
                StudentId = 1010, Marks = 15, CompareChoice = compareChoice
            });

            Console.WriteLine(" Binary Tree Created ! ");

            var choice = -1;

            Console.WriteLine("-------- Tree Structures --------");
            Console.WriteLine(" 1. Display");
            Console.WriteLine(" 2. Display BFT ");
            Console.WriteLine(" 3. Display DFT ");
            Console.WriteLine(" 4. Add ");
            Console.WriteLine(" 5. Find ");
            Console.WriteLine(" 6. Edit ");
            Console.WriteLine(" 7. Remove ");
            Console.WriteLine(" 8. Convert To Threaded BST ");
            Console.WriteLine(" 10. Exit");
            Console.WriteLine("---------------------------------");

            do
            {
                choice = int.Parse(Console.ReadLine());

                switch (choice)
                {
                case 1:
                    Console.Write("Inorder : ");
                    foreach (var item in binaryTree)
                    {
                        Console.Write("{0} ", item.Marks);
                    }
                    Console.WriteLine("");
                    Console.WriteLine("---------------------------------");
                    break;

                case 2:
                    if (!binaryTree.IsEmpty)
                    {
                        var treeTraversalObject = new TreeTraversal <Student>();
                        var bfsList             = treeTraversalObject.GetLeverOrder(binaryTree.Root);

                        foreach (var item in bfsList)
                        {
                            if (item.Count == 0)
                            {
                                Console.WriteLine("");
                            }
                            else
                            {
                                Console.Write("{0} ", item.Value.Marks);
                            }
                        }
                        //Console.Write(" [{0}-{1}-{2}] ", item.Value.StudentId, item.Value.Marks, item.Count);
                    }
                    else
                    {
                        Console.WriteLine("Tree is Empty!!");
                    }
                    Console.WriteLine("---------------------------------");
                    break;

                case 3:
                    if (!binaryTree.IsEmpty)
                    {
                        var treeTraversalObject = new TreeTraversal <Student>();
                        var dfsList             = treeTraversalObject.DepthFirstTraversal(binaryTree.Root);

                        foreach (var item in dfsList)
                        {
                            Console.WriteLine(" {0}-{1}-{2} ", item.Value.StudentId, item.Value.Marks, item.Count);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Tree is Empty!!");
                    }
                    Console.WriteLine("---------------------------------");
                    break;

                case 4:
                    Console.Write("Enter Id : ");
                    var Id = int.Parse(Console.ReadLine());
                    Console.WriteLine("");
                    Console.Write("Enter Marks : ");
                    var Marks = int.Parse(Console.ReadLine());
                    binaryTree.Add(new Student {
                        StudentId = Id, Marks = Marks, CompareChoice = compareChoice
                    });
                    Console.WriteLine("Record Added!");
                    Console.WriteLine("---------------------------------");
                    break;

                case 5:
                    Console.Write("Enter Id : ");
                    var itemToSearch = int.Parse(Console.ReadLine());
                    var data         = binaryTree.Find(new Student {
                        StudentId = itemToSearch
                    });
                    if (data != null)
                    {
                        Console.WriteLine("Id: {0} - Marks: {1} ", data.Value.StudentId, data.Value.Marks);
                    }
                    else
                    {
                        Console.WriteLine("No Record Found");
                    }
                    Console.WriteLine("---------------------------------");
                    break;

                case 6:
                    Console.Write("Enter Id : ");
                    var searchId = int.Parse(Console.ReadLine());
                    Console.WriteLine("");
                    Console.Write("Enter New Marks : ");
                    var editedMarks = int.Parse(Console.ReadLine());
                    binaryTree.Edit(new Student {
                        StudentId = searchId, Marks = editedMarks
                    });
                    Console.WriteLine("Record Edited!");
                    Console.WriteLine("---------------------------------");
                    break;


                case 7:
                    Console.Write("Enter Node Value : ");
                    var itemToDelete = int.Parse(Console.ReadLine());
                    var check        = false;
                    Console.WriteLine("");
                    if (compareChoice == 11)
                    {
                        check = binaryTree.Remove(new Student {
                            StudentId = itemToDelete
                        });
                    }
                    else if (compareChoice == 12)
                    {
                        check = binaryTree.Remove(new Student {
                            Marks = itemToDelete
                        });
                    }

                    if (check)
                    {
                        Console.WriteLine("Record Deleted!");
                    }
                    else
                    {
                        Console.WriteLine("Failed to Delete the record!");
                    }
                    Console.WriteLine("---------------------------------");
                    break;

                case 8:
                    binaryTree.ConvertToThreadedBST(binaryTree.Root, null);
                    var tBst = binaryTree.GetThreadedInorderList();
                    Console.Write("Inorder : ");
                    foreach (var item in tBst)
                    {
                        Console.Write("{0} ", item.Value.Marks);
                    }

                    Console.WriteLine("---------------------------------");
                    break;
                }
            } while (choice != 10);
        }