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