Esempio n. 1
0
        public int CalcSize(Link node, int size)
        {
            size++;

            if (node.Left != null)
            {
                CalcSize(node.Left, size);
            }
            if (node.Right != null)
            {
                CalcSize(node.Right, size);
            }

            return size;
        }
Esempio n. 2
0
 public void Add(int val)
 {
     if (rootNode == null)
     {
         rootNode = new Link();
     }
     else
     {
         Link newNode = Search(rootNode, val);
         if (val < newNode.Value)
         {
             newNode.Left = new Link(val, null, null);
         }
         else
             if (val > newNode.Value)
         {
             newNode.Right = new Link(val, null, null);
         }
     }
 }
Esempio n. 3
0
        public int FindHeight(Link node, int count)
        {
            if (node == null)
                return 0;

            int heightLeft = FindHeight(node.Left, count);
            int heightRight = FindHeight(node.Right, count);

            if (heightLeft > heightRight)
                count = heightLeft + 1;
            else
                count = heightRight + 1;

            return count;
        }
Esempio n. 4
0
 public void Clear()
 {
     rootNode = new Link();
 }
Esempio n. 5
0
 public Node(int val, Link left, Link right)
 {
     Value = val;
     Left = left;
     Right = right;
 }
Esempio n. 6
0
        public void SortElements(Link node, List<int> arr)
        {
            if (node.Left != null)
            {
                SortElements(node.Left, arr);
                arr.Add(node.Value);
            }
            else
            {
                arr.Add(node.Value);

            }

            if (node.Right != null)
            {
                SortElements(node.Right, arr);
            }
        }
Esempio n. 7
0
        public Link Search(Link node, int val)
        {
            if (rootNode == null)
            {
                node = new Link(val, null, null);
                rootNode = node;
            }

            if (node.Value != val)
            {
                if (val < node.Value && node.Left != null)
                {
                    node = Search(node.Left, val);
                }
                else
                {
                    if (val > node.Value && node.Right != null)
                        node = Search(node.Right, val);
                }
            }
            return node;
        }
Esempio n. 8
0
        public void Reverse(Link node)
        {
            if (node != null)
            {
                Link tempNode = node.Left;
                node.Left = node.Right;
                node.Right = tempNode;

                if (node.Left != null)
                {
                    Reverse(node.Left);
                }
                if (node.Right != null)
                {
                    Reverse(node.Right);
                }
            }
        }
Esempio n. 9
0
        public void Init(int[] arr)
        {
            if (arr.Length > 0)
            {
                rootNode = new Link(arr[0], null, null);

                for (int i = 1; i < arr.Length; i++)
                {
                    Add(arr[i]);
                }
            }
        }
Esempio n. 10
0
        public int FindWidth(Link node, int level)
        {
            if (node == null)
                return 0;

            if (level == 1)
                return 1;

            return FindWidth(node.Left, level - 1) + FindWidth(node.Right, level - 1);
        }
Esempio n. 11
0
 public Link FindParent(Link node, Link currentNode)
 {
     if (currentNode.Left != null && node != currentNode.Left && node.Value < currentNode.Value)
     {
         currentNode = FindParent(node, currentNode.Left);
     }
     if (currentNode.Right != null && node != currentNode.Right && node.Value > currentNode.Value)
     {
         currentNode = FindParent(node, currentNode.Right);
     }
     return currentNode;
 }
Esempio n. 12
0
 public int FindLeaves(Link node, int count)
 {
     if (node.Left == null && node.Right == null)
     {
         count++;
     }
     if (node.Left != null)
     {
         FindLeaves(node.Left, count);
     }
     if (node.Right != null)
     {
         FindLeaves(node.Right, count);
     }
     return count;
 }