Example #1
0
        private PointTree RandomTree(int size)
        {
            // Temporary object.
            PointTree root;

            // The number of elements in the left and in the right branches.
            int nl, nr;

            // If size=0 then it's the end of the tree.
            if (size == 0)
            {
                root = null;
                return(root);
            }

            // Getting the sizes of branches.
            nl = size / 2;
            nr = size - nl - 1;

            // Creating a random value for the root.
            int d = rnd.Next(20, 51);

            root = new PointTree(d);

            // Building the branches.
            root.Left  = RandomTree(nl);
            root.Right = RandomTree(nr);

            return(root);
        }
Example #2
0
 // Constructor with a parameter.
 public Tree(PointTree Root)
 {
     this.Root        = Root;
     this.Size        = 0;
     this.Height      = 0;
     this.TiersCounts = null;
 }
Example #3
0
        // Method to print the tree.
        public void ShowTree(PointTree root, int space)
        {
            if (root != null)
            {
                ShowTree(root.Left, space + 3);

                for (int i = 0; i < space; i++)
                {
                    Console.Write(" ");
                }

                Console.WriteLine(root.Value);

                ShowTree(root.Right, space + 3);
            }
        }
Example #4
0
        private void GetCounts(PointTree root, int currentTier, ref int[] counts)
        {
            // The end of the branch.
            if (root == null)
            {
                return;
            }
            else
            {
                // Increasing the number of elements on the current tier.
                counts[currentTier]++;

                // Calculating the next tier.
                GetCounts(root.Left, currentTier + 1, ref counts);
                GetCounts(root.Right, currentTier + 1, ref counts);
            }
        }
Example #5
0
 // Constructor without parameters.
 public PointTree()
 {
     this.Value = 0;
     this.Left  = null;
     this.Right = null;
 }
Example #6
0
 // Constructor with parameters.
 public PointTree(int value)
 {
     this.Value = value;
     this.Left  = null;
     this.Right = null;
 }