Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: heap ordering using binary tree

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default heap ordering using binary tree

    I have an assignment asking me to create a binary heap using binary tree instead of array. I have no idea about how to do this as what I got from internet/lecture is implement through ARRAY.




    private static class Node {
            public double value;
            public Node left, right;
     
            public Node(double value, Node left, Node right) {
                this.value = value;
                this.left = left;
                this.right = right;
            }


    Code above is given, and I required to create an insert method. So, can you give me some general idea on how to do this?

    thanks


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: heap ordering using binary tree

    Typically, nodes in a list have left and/or right values. nodes in a tree have children...see Binary Trees for an example of one type of tree: a binary tree

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: heap ordering using binary tree

     public void insert(double x){
            size++;
            root = insert(root, x);
          }
     
      private static Node insert(Node newnode, double x){
          if(newnode == null){
              newnode = new Node(x, null, null);
          }
     
              if(newnode.value < x) {
                 if((newnode.left != null) && (newnode.right != null)){
                  int n = (int)(10.0 * Math.random()) + 1;
                  if(n <= 5)
                      newnode.left = insert(newnode.left, x);
     
                  else
                      newnode.right = insert(newnode.right,x);
     
              }
     else{
     
                  if(newnode.left == null  || newnode.right == null){
                      if(newnode.left == null)
                          newnode.left = new Node(x,null,null);
                     if(newnode.right == null)
                      newnode.right = new Node(x,null,null);
                  }
                  }
                  }
     
             if(newnode.value > x){
                  double temp;
                  temp = newnode.value;
                  newnode.value = x;
                  insert(newnode, temp);
                  }

    the above code is the insert method i tried to create. but it does not work. can you tell me why?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: heap ordering using binary tree

    Quote Originally Posted by student View Post
    the above code is the insert method i tried to create. but it does not work. can you tell me why?
    Please define 'does not work'.

  5. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: heap ordering using binary tree

    when i try to compile this code, it just simply cannot print out the right numbers. the first number was correct, but not the others.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: heap ordering using binary tree

    Quote Originally Posted by student View Post
    when i try to compile this code, it just simply cannot print out the right numbers. the first number was correct, but not the others.
    This isn't the entire code listing, so we do not know what you've added to the tree/list, and how you are retrieving those values. Please provide a short example that clearly demonstrates the problem.

Similar Threads

  1. need help with binary tree
    By vash0047 in forum Java Theory & Questions
    Replies: 5
    Last Post: July 12th, 2010, 08:23 AM
  2. Data Structures(Binary Search Tree to AVL Tree)ASAP
    By jfAdik in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 5th, 2010, 03:58 AM
  3. Problem with binary tree
    By Exoskeletor in forum Object Oriented Programming
    Replies: 2
    Last Post: January 8th, 2010, 01:03 PM
  4. Binary Search Tree
    By Koren3 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 12th, 2009, 09:27 AM
  5. Binary Search Tree implementation
    By Ceasar in forum Java Theory & Questions
    Replies: 3
    Last Post: October 9th, 2009, 12:23 AM