Beispiel #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;
        }
Beispiel #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);
         }
     }
 }
Beispiel #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;
        }
Beispiel #4
0
 public void Clear()
 {
     rootNode = new Link();
 }
Beispiel #5
0
 public Node(int val, Link left, Link right)
 {
     Value = val;
     Left = left;
     Right = right;
 }
Beispiel #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);
            }
        }
Beispiel #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;
        }
Beispiel #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);
                }
            }
        }
Beispiel #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]);
                }
            }
        }
Beispiel #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);
        }
Beispiel #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;
 }
Beispiel #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;
 }