Пример #1
0
        public void delete_leaf_node_on_first_level()
        {
            Tree tree = new Tree(10);

            tree.Insert(5);
            tree.Insert(15);

            tree.Delete(5);

            Assert.Null(tree.Root.Left);
            Assert.NotNull(tree.Root.Right);
        }
Пример #2
0
        public void create_two_right_nodes_from_root()
        {
            Tree tree = new Tree(10);

            tree.Insert(15);
            tree.Insert(33);

            Assert.NotNull(tree.Root.Right);
            Assert.Null(tree.Root.Left);
            Assert.Equal(33, tree.Root.Right.Right.Data);
            Assert.Null(tree.Root.Right.Left);
        }
Пример #3
0
        public void delete_root_with_two_children()
        {
            Tree tree = new Tree(10);

            tree.Insert(5);
            tree.Insert(15);

            tree.Delete(10);

            Assert.Equal(15, tree.Root.Data);
            Assert.Null(tree.Root.Right);
            Assert.NotNull(tree.Root.Left);
        }
Пример #4
0
        public void delete_node_with_one_child()
        {
            Tree tree = new Tree(10);

            tree.Insert(5);
            tree.Insert(15);
            tree.Insert(1);

            tree.Delete(5);

            Assert.NotNull(tree.Root.Left);
            Assert.NotNull(tree.Root.Right);
            Assert.Equal(1, tree.Root.Left.Data);
        }
Пример #5
0
 // вставка
 public void Insert(T value)
 {
     if (this.value.Equals(default(T)))
     {
         this.value = value;
     }
     else
     {
         if (this.value.CompareTo(value) == 1)
         {
             if (left == null)
             {
                 this.left = new Tree <T>();
             }
             left.Insert(value);
         }
         else
         {
             if (right == null)
             {
                 this.right = new Tree <T>();
             }
             right.Insert(value);
         }
     }
 }
Пример #6
0
 static void Main(string[] args)
 {
     for (int j = 0; j < 15; j++)
     {
         Tree <int> tree = new Tree <int>();
         // var j = 15;
         int lastInsertElement = new int();
         using (StreamReader sr = new StreamReader((j + 1) + ".txt"))
         {
             while (!sr.EndOfStream)
             {
                 string[] line = sr.ReadLine().Split(new char[] { ' ' });
                 for (int i = 0; i < line.Length; i++)
                 {
                     if (line[i] == "")
                     {
                         break;
                     }
                     int element = Convert.ToInt32(line[i]);
                     tree.Insert(element);
                     lastInsertElement = element;
                 }
             }
         }
         Stopwatch sw = new Stopwatch();
         sw.Start();
         lastInsertElement = 7;
         bool isFind = tree.Search(lastInsertElement);
         sw.Stop();
         Console.WriteLine(sw.ElapsedTicks);
     }
     Console.ReadKey();
 }
Пример #7
0
        public void create_left_node_from_root()
        {
            Tree tree = new Tree(10);

            tree.Insert(5);

            Assert.NotNull(tree.Root.Left);
            Assert.Null(tree.Root.Right);
            Assert.Equal(5, tree.Root.Left.Data);
        }
Пример #8
0
        public void traverse_tree_in_postorder_manner()
        {
            List <int> result = new List <int>();
            Tree       tree   = new Tree(10);

            tree.Insert(5);
            tree.Insert(15);
            tree.Insert(1);
            tree.Insert(6);

            tree.Postorder(x =>
            {
                result.Add(x);
            });

            Assert.Equal(5, result.Count);
            Assert.Equal(1, result[0]);
            Assert.Equal(6, result[1]);
            Assert.Equal(5, result[2]);
            Assert.Equal(15, result[3]);
            Assert.Equal(10, result[4]);
        }
Пример #9
0
        private static void AddData(Tree tree, int numberOfTestNodes, TestData.TestDataOrder testDataOrder)
        {
            TestData testData = new TestData(numberOfTestNodes, testDataOrder);

            Console.WriteLine("Order of test data is : {0}", testDataOrder.ToString());
            Console.Write("  Adding {0} nodes with values from 0 to {1}. ", testData.Keys.Length, testData.Keys.Length - 1);
            Stopwatch stopwatch = Stopwatch.StartNew();

            foreach (int key in testData.Keys)
            {
                tree.Insert(key);
            }
            stopwatch.Stop();
            Console.WriteLine("Time elapsed (ms) : {0}", stopwatch.ElapsedMilliseconds);
        }
Пример #10
0
        public void delete_nodes_which_have_two_children_nodes()
        {
            Tree tree = new Tree(100);

            tree.Insert(50);
            tree.Insert(150);
            tree.Insert(25);
            tree.Insert(75);
            tree.Insert(125);
            tree.Insert(60);
            tree.Insert(80);
            tree.Insert(200);

            tree.Delete(50);
            tree.Delete(150);

            Assert.Equal(200, tree.Root.Right.Data);
            Assert.Equal(60, tree.Root.Left.Data);
        }
Пример #11
0
        public void check_if_all_nodes_are_properly_distributed()
        {
            Tree tree = new Tree(100);

            tree.Insert(50);
            tree.Insert(150);
            tree.Insert(25);
            tree.Insert(75);
            tree.Insert(125);
            tree.Insert(175);

            Assert.Equal(100, tree.Root.Data);
            Assert.Equal(50, tree.Root.Left.Data);
            Assert.Equal(25, tree.Root.Left.Left.Data);
            Assert.Equal(75, tree.Root.Left.Right.Data);
            Assert.Equal(150, tree.Root.Right.Data);
            Assert.Equal(125, tree.Root.Right.Left.Data);
            Assert.Equal(175, tree.Root.Right.Right.Data);
        }
Пример #12
0
        static void Main(string[] args)
        {
            Tree theTree = new Tree();

            theTree.Insert(20);
            theTree.Insert(25);
            theTree.Insert(45);
            theTree.Insert(15);
            theTree.Insert(67);
            theTree.Insert(43);
            theTree.Insert(80);
            theTree.Insert(33);
            theTree.Insert(67);
            theTree.Insert(99);
            theTree.Insert(91);
            Console.WriteLine("Inorder Traversal : ");
            theTree.Inorder(theTree.ReturnRoot());
            Console.WriteLine(" ");
            Console.WriteLine();
            Console.WriteLine("Preorder Traversal : ");
            theTree.Preorder(theTree.ReturnRoot());
            Console.WriteLine(" ");
            Console.WriteLine();
            Console.WriteLine("Postorder Traversal : ");
            theTree.Postorder(theTree.ReturnRoot());
            Console.WriteLine(" ");
            Console.ReadLine();
        }
Пример #13
0
        static void Main(string[] args)
        {
            Tree tree = new Tree();

            tree.AddNode(3);

            tree.Insert(tree.root, tree.AddNode(2));
            tree.Insert(tree.root, tree.AddNode(1));
            tree.Insert(tree.root, tree.AddNode(4));
            tree.Insert(tree.root, tree.AddNode(10));
            tree.Insert(tree.root, tree.AddNode(6));

            tree.Print(tree.root);
        }