array to binary tree

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • perdoname
    New Member
    • Feb 2008
    • 7

    #1

    array to binary tree

    Hello!

    If i want to place the elements of an unsorted array of integers and add the elements to a binary tree then do i have to make use of heapsort??

    and another thing how can i print all the values that are in the tree between two given integers ??

    Thanks in advance
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    If you use the binary tree's add method, the numbers should be sorted as part of the adding process. If you want an optimal binary search tree, you could use a heapsort - but then, why not just use a heap?

    EDIT: Just saw your second question. It should be fairly easy to write a non-optimized version of this printing function by checking every number. Now what could you do to cut back on how many you check?

    For instance, if you check node A and it is less than the lowest value you want to print, you don't print it. Do you need to check its left child? Its right child? How about for node B, which is greater than the highest value you want to print? Do you want to check its left child? Its right child?

    Comment

    • perdoname
      New Member
      • Feb 2008
      • 7

      #3
      Thanks for the answer
      but on that code which implements a heapsort how can the the part which will return the values between the two integers can be implemented ?

      thanks in advance

      Code:
      public class HeapSort {
        public static void heapSort(int list[]) {
      
          for (int i = 1; i < list.length; i++) {
            makeHeap(list, i);
          }
      
      
          for (int last = list.length - 1; last > 0; ) {
      
            int temp = list[last];
            list[last] = list[0];
            list[0] = temp;
            rebuildHeap(list, --last);
          }
        }
      
      
        private static void makeHeap(int[] list, int k) {
          int currentIndex = k;
      
          while (currentIndex > 0 &&
              list[currentIndex] > list[(currentIndex - 1) / 2]) {
            // Swap list[currentIndex] with list[(currentIndex - 1) / 2]
            int temp = list[currentIndex];
            list[currentIndex] = list[(currentIndex - 1) / 2];
            list[(currentIndex - 1) / 2] = temp;
      
            currentIndex = (currentIndex - 1) / 2;
          }
        }
      
        private static void rebuildHeap(int[] list, int last) {
          int currentIndex = 0;
          boolean isHeap = false;
      
          while (!isHeap) {
            int leftChildIndex = 2 * currentIndex + 1;
            int rightChildIndex = 2 * currentIndex + 2;
            int maxIndex = currentIndex;
      
            if (leftChildIndex <= last &&
              list[maxIndex] < list[leftChildIndex]) {
              maxIndex = leftChildIndex;
            }
      
            if (rightChildIndex <= last &&
              list[maxIndex] < list[rightChildIndex]) {
              maxIndex = rightChildIndex;
            }
      
            if (maxIndex != currentIndex) {
      
              int temp = list[currentIndex];
              list[currentIndex] = list[maxIndex];
              list[maxIndex] = temp;
              currentIndex = maxIndex;
            } else {
              isHeap = true;
            }
          }
        }
        
        public static void main(String[] args) {
      
          int[] list = {2, 3, 1, 4, 42, 4, 59, 32, 39, 44, 13, 22, 29, 14, 33, 17, 30, 9, 68};
          heapSort(list);
          for (int i = 0; i < list.length; i++) {
            System.out.print(list[i] + " ");
          }
        }
      }

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Well, that's up for you to figure out, isn't it? I already gave you a large hint in my last post - you just need to figure out how to make it work with heaps.

        Comment

        • perdoname
          New Member
          • Feb 2008
          • 7

          #5
          so i do actually just have to replace the variables with the taken two numbers ? and print in that way the array?
          :S

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            Originally posted by perdoname
            so i do actually just have to replace the variables with the taken two numbers ? and print in that way the array?
            :S
            Which variables? Which taken two numbers? Where are you printing?

            All I see in your code is the code to make a heap.

            Comment

            • perdoname
              New Member
              • Feb 2008
              • 7

              #7
              Its okay i found the solution , i think so
              but i have a problem with the 2nd part which will return the numbers between the two inputs:
              During the compile it returns "illegal start of expression" at the " public E findNums(int min, int max, root.left(Node) ;" ???

              Do you know why is that happening ???

              Thanks in advance!

              Code:
              public E findNums(int min, int max, Node root)
                  {
                      if (root == nul)
                      {
                          return ;
                      }
                      if (root.data >= min)
                      {
                          public E findNums(int min, int max, root.left(Node);
                          if (root.data <= max)
                          {
                              System.out.println(root.data);
                          }
                      }
                      if (root.data <= max)
                      {
                          public E findNums(int min, int max, root.rightNode);
                      }
                  }

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                There is an extra '(' in your statement. In addition, you are not calling your function correctly: you don't need to say public E findNums.

                That's a very basic point of calling functions - are you sure you know how to do this?

                Comment

                • perdoname
                  New Member
                  • Feb 2008
                  • 7

                  #9
                  Its okay now thanks !

                  Comment

                  Working...