コード例 #1
0
        static void Main(string[] args)
        {
            int[]            nRanNumbers = new int[] { 3, 9, 7, 6, 8, 1, 2, 4, 5 };
            BinaryTree <int> tree        = new BinaryTree <int>();

            foreach (int x in nRanNumbers)
            {
                tree.Add(x);
                Console.WriteLine(x);
            }


            Console.ReadLine();
        }
コード例 #2
0
        private static void TestBinaryTree()
        {
            BinaryTree <int> tree = new BinaryTree <int>();

            tree.Add(8);
            tree.Add(4);
            tree.Add(2);
            tree.Add(3);
            tree.Add(10);
            tree.Add(6);
            tree.Add(7);

            Console.WriteLine("Tree contains 6?: {0}", tree.Contains(6));
            Console.WriteLine("Tree contains 16?: {0}", tree.Contains(16));
            Console.WriteLine("Tree contains 10?: {0}", tree.Contains(10));
        }
コード例 #3
0
        static void Main(string[] args)
        {
            #region BinaryTree
            BinaryTree binaryTree = new BinaryTree();

            binaryTree.Add(1);
            binaryTree.Add(2);
            binaryTree.Add(7);
            binaryTree.Add(3);
            binaryTree.Add(10);
            binaryTree.Add(5);
            binaryTree.Add(8);

            Node node  = binaryTree.Find(5);
            int  depth = binaryTree.GetTreeDepth();

            Console.WriteLine("PreOrder Traversal:");
            binaryTree.TraversePreOrder(binaryTree.Root);
            Console.WriteLine();

            Console.WriteLine("InOrder Traversal:");
            binaryTree.TraverseInOrder(binaryTree.Root);
            Console.WriteLine();

            Console.WriteLine("PostOrder Traversal:");
            binaryTree.TraversePostOrder(binaryTree.Root);
            Console.WriteLine();

            binaryTree.Remove(7);
            binaryTree.Remove(8);

            Console.WriteLine("PreOrder Traversal After Removing Operation:");
            binaryTree.TraversePreOrder(binaryTree.Root);
            Console.WriteLine();

            Console.ReadLine();
            #endregion
        }
コード例 #4
0
        static void Main(string[] args)
        {
            SingleLinkedList linkedList = new SingleLinkedList();

            linkedList.InsertFront(linkedList, 2);

            // We add one and the head will have data of 2 in it;
            // the next property will be null as its the end

            linkedList.InsertFront(linkedList, 3);

            // if we add another this becomes the head
            // and next becomes the data node with 2 in it
            // and its next is null

            var node = linkedList.FindNode(linkedList, 3);

            linkedList.InsertAfter(node, 4);


            var myNode = linkedList.FindNode(linkedList, 2);

            linkedList.InsertAfter(myNode, 7);

            linkedList.DeleteNodebyKey(linkedList, 2);


            linkedList.ReverseLinkedList(linkedList);


            BinaryTree binaryTree = new BinaryTree();


            // this gets added to the root because there are no other nodes
            // if data is less than it goes on the left.
            // if its more then it goes on the right..
            // so as we are searching - if the value of the data < this node then go left
            // if value of data > this node then go right
            // and progress down the tree to find a space for the data
            // Each pass cuts data in half and improves performance..
            binaryTree.Add(1);
            binaryTree.Add(2);
            binaryTree.Add(7);
            binaryTree.Add(3);
            binaryTree.Add(10);
            binaryTree.Add(5);
            binaryTree.Add(8);

            // what happens if we already have the value

            binaryTree.Add(1);

            AvlTree tree = new AvlTree();

            tree.Add(5);
            tree.Add(3);
            tree.Add(7);
            tree.Add(2);
            tree.Delete(7);
            tree.DisplayTree();


            // LIFO so
            Stack <FootballTeam> stackOfFootballTeams = new Stack <FootballTeam>();

            stackOfFootballTeams.Push(new FootballTeam()
            {
                Name = "Manchester United", ShirtColor = "Red", Stadium = "Old Trafford"
            });
            stackOfFootballTeams.Push(new FootballTeam()
            {
                Name = "Liverpool", ShirtColor = "Red", Stadium = "Anfield"
            });

            var count = stackOfFootballTeams.Count;

            // so first out will be liverpool
            var item = stackOfFootballTeams.Pop();

            // This is a queue so FIFO

            Queue <FootballTeam> queueOfFootballTeams = new Queue <FootballTeam>();

            queueOfFootballTeams.Enqueue(new FootballTeam()
            {
                Name = "Manchester United", ShirtColor = "Red", Stadium = "Old Trafford"
            });
            queueOfFootballTeams.Enqueue(new FootballTeam()
            {
                Name = "Liverpool", ShirtColor = "Red", Stadium = "Anfield"
            });

            // This will be man united.
            var footballTeam = queueOfFootballTeams.Dequeue();

            Console.ReadLine();
        }
コード例 #5
0
ファイル: BinaryTree.cs プロジェクト: nikxsh/dotnet
        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);
        }