static void Main(string[] args)
        {
            // Instantiate a new IntTree and populate it with Nodes.
            IntTree tree = new IntTree();

            tree.Add(tree.Root, 10);
            tree.Add(tree.Root, 5);
            tree.Add(tree.Root, 3);
            tree.Add(tree.Root, 4);
            tree.Add(tree.Root, 7);
            tree.Add(tree.Root, 15);

            // Convert the IntTree Nodes to an array of int values.
            int[] TreeArray = tree.PreOrder(tree.Root);

            // Output the int array to the console.
            foreach (int value in TreeArray)
            {
                Console.Write($"{value}, ");
            }
            Console.WriteLine();

            // Convert the IntTree to a FizzBuzz StringTree.
            StringTree st = FizzBuzzTree(tree);

            // Convert the StringTree Nodes to an array of string values.
            string[] FizzArray = st.PreOrder(st.Root);

            // Output the string array to the console.
            foreach (string value in FizzArray)
            {
                Console.Write($"{value}, ");
            }
            Console.WriteLine();
        }
Example #2
0
        // Parallel implementation
        // The parameter 'd' specifies current depth
        static int ParallelCount(IntTree t, int d)
        {
            int     value;
            IntTree l, r;

            // For small sub-tasks, use the non-parallel version
            if (d > 4)
            {
                return(Count(t));
            }

            if (t.TryGetNode(out l, out r))
            {
                // Calculate one sub-tree using 'Future'
                var cl = Task.Factory.StartNew(() => ParallelCount(l, d + 1));
                var cr = ParallelCount(r, d + 1);
                // Wait for both of the results and return
                return(cl.Result + cr);
            }
            else if (t.TryGetLeaf(out value))
            {
                // Test whether number is a prime
                return(IsPrime(value) ? 1 : 0);
            }

            // This is unreachable code
            throw new InvalidOperationException();
        }
        static StringTree FizzBuzzTree(IntTree tree)
        {
            StringTree NewTree = new StringTree();

            PreOrderFizzBuzz(tree.Root, NewTree);

            return(NewTree);
        }
Example #4
0
        public void Opdracht2d_TestInOrder1()
        {
            //Arrange
            IntTree tree = BuildTree1to10();
            //Act
            string result = tree.InOrder();

            //Assert
            Assert.AreEqual("1 2 3 4 5 6 7 8 9 10", result);
        }
Example #5
0
        public void Opdracht2c_TestCount2()
        {
            //Arrange
            IntTree tree = BuildTreeminus50to50();
            //Act
            int result = tree.Count();

            //Assert
            Assert.AreEqual(11, result);
        }
Example #6
0
        public void Opdracht2d_TestInOrder2()
        {
            //Arrange
            IntTree tree = BuildTreeminus50to50();
            //Act
            string result = tree.InOrder();

            //Assert
            Assert.AreEqual("-50 -40 -30 -20 -10 0 10 20 30 40 50", result);
        }
Example #7
0
        public void Opdracht2a_TestMin1()
        {
            //Arrange
            IntTree tree = BuildTree1to10();
            //Act
            int result = tree.Min();

            //Assert
            Assert.AreEqual(1, result);
        }
Example #8
0
        public void Opdracht2b_TestMax1()
        {
            //Arrange
            IntTree tree = BuildTree1to10();
            //Act
            int result = tree.Max();

            //Assert
            Assert.AreEqual(10, result);
        }
Example #9
0
        public void Opdracht2b_TestMax2()
        {
            //Arrange
            IntTree tree = BuildTreeminus50to50();
            //Act
            int result = tree.Max();

            //Assert
            Assert.AreEqual(50, result);
        }
Example #10
0
        public void Opdracht2c_TestCount1()
        {
            //Arrange
            IntTree tree = BuildTree1to10();
            //Act
            int result = tree.Count();

            //Assert
            Assert.AreEqual(10, result);
        }
Example #11
0
        private void QuickInitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            QuickCreate quickCreate = new QuickCreate();

            if (quickCreate.ShowDialog() == DialogResult.OK)
            {
                tree      = new IntTree(quickCreate.N, quickCreate.K, 1, 10);
                draw.Tree = tree;
                Refresh();
            }
        }
Example #12
0
        // Count the number of primes in the tree
        static int Count(IntTree t)
        {
            int val;
              IntTree l, r;
              if (t.TryGetNode(out l, out r))
            // For a node, recursively process sub-trees
            return Count(l) + Count(r);
              else if (t.TryGetLeaf(out val))
            // For a leaf, test whether it contains a prime
            return IsPrime(val) ? 1 : 0;

              // This is unreachable code
              throw new InvalidOperationException();
        }
Example #13
0
        private static long IntGet <V>(IntTree <V> tree, int key, int times)
        {
            V   ignored   = default(V);
            var treeWatch = Stopwatch.StartNew();

            for (int i = 0; i < times; i++)
            {
                ignored = tree.GetValueOrDefault(key);
            }

            treeWatch.Stop();
            GC.KeepAlive(ignored);
            GC.Collect();
            return(treeWatch.ElapsedMilliseconds);
        }
Example #14
0
 private void OpenToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if (openFileDialog.ShowDialog() == DialogResult.OK)
     {
         try
         {
             tree      = new IntTree(new List <string>(File.ReadLines(openFileDialog.FileName)));
             draw.Tree = tree;
             this.Refresh();
         }
         catch (Exception)
         {
             MessageBox.Show("ошибка чтения", "Error");
         }
     }
 }
Example #15
0
        private IntTree BuildTree1to10()
        {
            IntTree tree = new IntTree(5);

            tree.Add(6);
            tree.Add(9);
            tree.Add(1);
            tree.Add(3);
            tree.Add(4);
            tree.Add(8);
            tree.Add(2);
            tree.Add(10);
            tree.Add(5);
            tree.Add(7);
            return(tree);
        }
Example #16
0
        private IntTree BuildTreeminus50to50()
        {
            IntTree tree = new IntTree(0);

            tree.Add(-30);
            tree.Add(40);
            tree.Add(-40);
            tree.Add(10);
            tree.Add(40);
            tree.Add(-50);
            tree.Add(-10);
            tree.Add(20);
            tree.Add(50);
            tree.Add(-20);
            tree.Add(30);
            return(tree);
        }
Example #17
0
        // Count the number of primes in the tree
        static int Count(IntTree t)
        {
            int     val;
            IntTree l, r;

            if (t.TryGetNode(out l, out r))
            {
                // For a node, recursively process sub-trees
                return(Count(l) + Count(r));
            }
            else if (t.TryGetLeaf(out val))
            {
                // For a leaf, test whether it contains a prime
                return(IsPrime(val) ? 1 : 0);
            }

            // This is unreachable code
            throw new InvalidOperationException();
        }
Example #18
0
        public void DepthFirstPathsRootRight()
        {
            IntTree root = new IntTree()
            {
                Item = 0
            };
            IntTree left = new IntTree()
            {
                Item = 4
            };
            IntTree right = new IntTree()
            {
                Item = 1
            };

            left.Add(new IntTree()
            {
                Item = 6
            });
            left.Add(new IntTree()
            {
                Item = 5
            });
            right.Add(new IntTree()
            {
                Item = 3
            });
            right.Add(new IntTree()
            {
                Item = 2
            });
            root.Add(left);
            root.Add(right);

            List <string> values = new List <string>();

            root.DepthFirstPaths((p, t) => values.Add($"{p.Item}-{t.Item}"), true, false);

            Assert.Equal(new string[] { "0-1", "1-2", "1-3", "0-4", "4-5", "4-6" }, values);
        }
Example #19
0
        public void DepthFirstPathsLeafRight()
        {
            IntTree root = new IntTree()
            {
                Item = 6
            };
            IntTree left = new IntTree()
            {
                Item = 5
            };
            IntTree right = new IntTree()
            {
                Item = 2
            };

            left.Add(new IntTree()
            {
                Item = 4
            });
            left.Add(new IntTree()
            {
                Item = 3
            });
            right.Add(new IntTree()
            {
                Item = 1
            });
            right.Add(new IntTree()
            {
                Item = 0
            });
            root.Add(left);
            root.Add(right);

            List <string> values = new List <string>();

            root.DepthFirstPaths((p, t) => values.Add($"{p.Item}-{t.Item}"), false, false);

            Assert.Equal(new string[] { "2-0", "2-1", "6-2", "5-3", "5-4", "6-5" }, values);
        }
Example #20
0
        public void BreathFirstLevelsLeafRight()
        {
            IntTree root = new IntTree()
            {
                Item = 0
            };
            IntTree left = new IntTree()
            {
                Item = 1
            };
            IntTree right = new IntTree()
            {
                Item = 2
            };

            left.Add(new IntTree()
            {
                Item = 3
            });
            left.Add(new IntTree()
            {
                Item = 4
            });
            right.Add(new IntTree()
            {
                Item = 5
            });
            right.Add(new IntTree()
            {
                Item = 6
            });
            root.Add(left);
            root.Add(right);

            List <string> values = new List <string>();

            root.BreathFirstLevels(t => values.Add(string.Join("-", t.Select(p => p.Item))), false, false);

            Assert.Equal(new string[] { "6-5-4-3", "2-1", "0" }, values);
        }
Example #21
0
        public void DepthFirstRootRight()
        {
            IntTree root = new IntTree()
            {
                Item = 0
            };
            IntTree left = new IntTree()
            {
                Item = 4
            };
            IntTree right = new IntTree()
            {
                Item = 1
            };

            left.Add(new IntTree()
            {
                Item = 6
            });
            left.Add(new IntTree()
            {
                Item = 5
            });
            right.Add(new IntTree()
            {
                Item = 3
            });
            right.Add(new IntTree()
            {
                Item = 2
            });
            root.Add(left);
            root.Add(right);

            List <int> values = new List <int>();

            root.DepthFirst(t => values.Add(t.Item), true, false);

            Assert.Equal(new int[] { 0, 1, 2, 3, 4, 5, 6 }, values);
        }
Example #22
0
        public void BreathFirstLeafLeft()
        {
            IntTree root = new IntTree()
            {
                Item = 6
            };
            IntTree left = new IntTree()
            {
                Item = 4
            };
            IntTree right = new IntTree()
            {
                Item = 5
            };

            left.Add(new IntTree()
            {
                Item = 0
            });
            left.Add(new IntTree()
            {
                Item = 1
            });
            right.Add(new IntTree()
            {
                Item = 2
            });
            right.Add(new IntTree()
            {
                Item = 3
            });
            root.Add(left);
            root.Add(right);

            List <int> values = new List <int>();

            root.BreathFirst(t => values.Add(t.Item), false);

            Assert.Equal(new int[] { 0, 1, 2, 3, 4, 5, 6 }, values);
        }
Example #23
0
        public void BreathFirstLevelsRootLeft()
        {
            IntTree root = new IntTree()
            {
                Item = 0
            };
            IntTree left = new IntTree()
            {
                Item = 1
            };
            IntTree right = new IntTree()
            {
                Item = 2
            };

            left.Add(new IntTree()
            {
                Item = 3
            });
            left.Add(new IntTree()
            {
                Item = 4
            });
            right.Add(new IntTree()
            {
                Item = 5
            });
            right.Add(new IntTree()
            {
                Item = 6
            });
            root.Add(left);
            root.Add(right);

            List <string> values = new List <string>();

            root.BreathFirstLevels(t => values.Add(string.Join("-", t.Select(p => p.Item))));

            Assert.Equal(new string[] { "0", "1-2", "3-4-5-6" }, values);
        }
Example #24
0
        static void Main(string[] args)
        {
            IntTree <int> newIntTree = new IntTree <int>();

            newIntTree.AddLeaf(50);
            newIntTree.AddLeaf(30);
            newIntTree.AddLeaf(15);
            newIntTree.AddLeaf(40);
            newIntTree.AddLeaf(35);
            newIntTree.AddLeaf(31);
            newIntTree.AddLeaf(37);
            newIntTree.AddLeaf(47);
            newIntTree.AddLeaf(49);
            newIntTree.AddLeaf(46);
            newIntTree.AddLeaf(48);
            newIntTree.AddLeaf(80);
            newIntTree.AddLeaf(70);
            newIntTree.AddLeaf(95);
            newIntTree.AddLeaf(85);
            newIntTree.AddLeaf(100);

            Console.ReadLine();
        }
Example #25
0
 public override bool TryGetNode(out IntTree left, out IntTree right)
 {
     left = this.left; right = this.right;
     return(true);
 }
Example #26
0
 public IntTreeNode(IntTree left, IntTree right)
 {
     this.left = left; this.right = right;
 }
Example #27
0
 public virtual bool TryGetNode(out IntTree left, out IntTree right)
 {
     left  = null;
     right = null;
     return(false);
 }
Example #28
0
 private void ClearToolStripMenuItem_Click(object sender, EventArgs e)
 {
     tree      = new IntTree();
     draw.Tree = tree;
     Refresh();
 }
Example #29
0
 public TreeDraw(int width, int height, IntTree tree)
 {
     this.width  = width;
     this.height = height;
     this.Tree   = tree;
 }
Example #30
0
        // Parallel implementation
        // The parameter 'd' specifies current depth
        static int ParallelCount(IntTree t, int d)
        {
            int value;
              IntTree l, r;
              // For small sub-tasks, use the non-parallel version
              if (d > 4) return Count(t);

              if (t.TryGetNode(out l, out r)) {
            // Calculate one sub-tree using 'Future'
            var cl = Task.Factory.StartNew(() => ParallelCount(l, d + 1));
            var cr = ParallelCount(r, d + 1);
            // Wait for both of the results and return
            return cl.Result + cr;
              }
              else if (t.TryGetLeaf(out value))
            // Test whether number is a prime
            return IsPrime(value) ? 1 : 0;

              // This is unreachable code
              throw new InvalidOperationException();
        }
Example #31
0
 public virtual bool TryGetNode(out IntTree left, out IntTree right)
 {
     left = null;
     right = null;
     return false;
 }
Example #32
0
 public IntTreeNode(IntTree left, IntTree right)
 {
     this.left = left; this.right = right;
 }
Example #33
0
 public override bool TryGetNode(out IntTree left, out IntTree right)
 {
     left = this.left; right = this.right;
     return true;
 }
Example #34
0
 public TreeForm()
 {
     InitializeComponent();
     tree = new IntTree();
 }
Example #35
0
        static void Main(string[] args)
        {
            Console.WriteLine("Лаб. 12. Решетняк");

            //Task12.1
            //===================================================================================================================================================
            Console.WriteLine("\nTask12.1\n");
            Console.WriteLine("Удалить из списка первый элемент с четным информационным полем. \n");
            int counter = 1;
            LinkedList <Person> list = new LinkedList <Person>();

            list.Add(new Worker("Fedor", "Solyanov", 22, "Fire"));
            list.Add(new Administer("Fire", "Loading", 29, "Legolas"));
            list.Add(new Engineer("Leonid", "Pirogov", 28, "Fire", "Lego"));
            list.Add(new Worker("People", "Stronger", 34, "Fire"));
            Administer ADM = new Administer("Fire", "Anakin", 30, "Lego");

            list.Add(ADM);
            foreach (Person WRK in list)
            {
                Console.WriteLine(counter + " " + WRK.Show());
                counter++;
            }
            Console.WriteLine();
            Console.WriteLine("Удаление первого чётного элемента");
            Console.WriteLine("Удаленный элемент - " + list.FindEven(2).Data.Show() + "\n");
            list.Delete(list.FindEven(2).Data);
            // list.Delete(ADM);
            counter = 1;
            foreach (Person item in list)
            {
                Console.WriteLine(counter + " " + item.Show());
                counter++;
            }
            Console.WriteLine();
            list.Clear();
            if (list.Count == 0)
            {
                Console.WriteLine("Однонаправленный список удалён");
            }
            Console.WriteLine("Нажмите, чтобы продолжить");
            Console.ReadLine();

            //Task12.2
            //===================================================================================================================================================
            Console.WriteLine("Task12.2\n");
            Console.WriteLine("Добавить в список элементы с номерами 1, 3, 5 и т. д.\n");
            int counterDva = 1;
            DoubleList <Person> doubleList = new DoubleList <Person>();

            doubleList.Add(new Worker("AMD", "loser", 12, "Fire"));
            doubleList.Add(new Administer("fire", "lol", 24, "Loading"));
            Worker workWalk = new Worker("Anime", "LoL", 22, "TEST");

            doubleList.Add(workWalk);

            foreach (Person item in doubleList)
            {
                Console.WriteLine(counterDva + " " + item.Show());
                counterDva++;
            }
            counterDva = 1;

            Console.WriteLine();

            Console.WriteLine("Добавить в список элементы с номерами 1, 3, 5 и т. д.");
            foreach (Person item in doubleList.FindUnEven())
            {
                Console.WriteLine(counterDva + " " + item.Show());
                counterDva++;
            }
            doubleList.Clear();
            if (doubleList.count == 0)
            {
                Console.WriteLine("Двунаправленный список удалён");
            }
            Console.WriteLine("Нажмите, чтобы продолжить");
            Console.ReadLine();

            //Task12.2
            //===================================================================================================================================================

            Console.WriteLine("\nTask 12.3\n");

            Console.WriteLine("Оригинальное дерево:");
            Console.WriteLine("                                    5                             ");
            Console.WriteLine("                             /             \\                  ");
            Console.WriteLine("                         3                    8                         ");
            Console.WriteLine("                    /       \\              /  \\                  ");
            Console.WriteLine("                   1         4          7         9                   ");
            Console.WriteLine("                    \\                   /                ");
            Console.WriteLine("                     2                 6                ");


            Tree <Worker> intTree = new Tree <Worker>();

            intTree.Add(new Worker("Da", "Net", 5, "Fire"));
            intTree.Add(new Worker("Da", "Net", 3, "Fire"));
            intTree.Add(new Worker("Da", "Net", 8, "Fire"));
            intTree.Add(new Worker("Da", "Net", 7, "Fire"));
            intTree.Add(new Worker("Da", "Net", 4, "Fire"));
            intTree.Add(new Worker("Da", "Net", 1, "Fire"));
            intTree.Add(new Worker("Da", "Net", 2, "Fire"));
            intTree.Add(new Worker("Da", "Net", 6, "Fire"));
            intTree.Add(new Worker("Da", "Net", 9, "Fire"));

            Console.WriteLine("Префиксный");
            foreach (Worker IntTree in intTree.Preorder())
            {
                Console.WriteLine(IntTree.Show() + " / ");
            }
            Console.WriteLine("\nПостфиксный\n");
            foreach (Worker IntTree in intTree.Postorder())
            {
                Console.WriteLine(IntTree.Show() + " / ");
            }
            Console.WriteLine("\nИнфиксный\n");
            foreach (Worker IntTree in intTree.Inorder())
            {
                Console.WriteLine(IntTree.Show() + " / ");
            }
            Console.WriteLine("Длина дерева: " + Convert.ToString(intTree.MaxHeight(intTree.Root)));
            Console.WriteLine("Общее количество элементов: " + Convert.ToString(intTree.Count()));
            intTree.Clear();
            if (intTree.Count() == 0)
            {
                Console.WriteLine("Дерево удалено");
            }
            Console.ReadLine();
        }