14 Replies - 17316 Views - Last Post: 24 April 2011 - 08:23 PM Rate Topic: -----

#1 charli  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-April 11

Create a Binary Search Tree in Java.

Posted 24 April 2011 - 06:15 PM

Hello, I am creating a Binary Search Tree but I want to create a more than three child under each parent, and I want to insert data in all of the children upon one 'if' condition. please help me in this. following is my node class:

 class bstNode{
    private int data;         
    private bstNode a, b, c, d;
            
    public bstNode(int data)
    {
        this.data = data;
        a = null;
        b = null; 
        c = null;  
        d = null;  
    }	
}


and here is the Tree code and I am getting an error in this, here is the code:

import java.util.*;
import java.io.*;

public class BinarySearchTree {
      private bstNode root;
 
      public BinarySearchTree() {
            root = null;
      }

//-----------------------------------------------------------------------------//

 public boolean insert(bstNode node, int data){
 	if(root != null){
        root = new bstNode(data);
		  return true;
		  }
		  else if(root != null ){
			for(int i=0; i<=4; i++){
				a = new bstNode(value);
				b = new bstNode(value);
				c = new bstNode(value);
				d = new bstNode(value);
			}
				
		}
	}
  	 
}



please help me in this.
Thanks.

This post has been edited by pbl: 24 April 2011 - 08:09 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Create a Binary Search Tree in Java.

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10387
  • View blog
  • Posts: 38,438
  • Joined: 27-December 08

Re: Create a Binary Search Tree in Java.

Posted 24 April 2011 - 06:18 PM

By definition, a Binary Tree can have no more than 2 children. You can still have a search tree with more than 2 children, but it won't be a binary search tree.

Also, what specific errors are you encountering?
Was This Post Helpful? 2
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Create a Binary Search Tree in Java.

Posted 24 April 2011 - 06:23 PM

This a binary search tree so: 2 nodes

private bstNode left,right;

why: private bstNode a, b, c, d; ?
to support 4 nodes ???

When you will have to support 26 nodes will you do

private bstNode a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z; ???

View Postpbl, on 24 April 2011 - 08:22 PM, said:

This a binary search tree so: 2 nodes

private bstNode left,right;

why: private bstNode a, b, c, d; ?
to support 4 nodes ???

When you will have to support 26 nodes will you do

private bstNode a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z; ???

Ninja'd by macosxnerd101 :)... more politically correct than me :)
Was This Post Helpful? 2
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10387
  • View blog
  • Posts: 38,438
  • Joined: 27-December 08

Re: Create a Binary Search Tree in Java.

Posted 24 April 2011 - 06:23 PM

Great minds think alike. :)
Was This Post Helpful? 1
  • +
  • -

#5 charli  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-April 11

Re: Create a Binary Search Tree in Java.

Posted 24 April 2011 - 07:02 PM

ok, sorry every body. can any one tell me how do I create search tree with more than two child.
please help, thanks.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Create a Binary Search Tree in Java.

Posted 24 April 2011 - 07:09 PM

The root has a left and right node
each of these nodes has a left and right node
and so on

http://www.java-tips...on-in-java.html
http://www.java2s.co.../BinaryTree.htm

http://www.roseindia...tree-code.shtml
Was This Post Helpful? 1
  • +
  • -

#7 charli  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-April 11

Re: Create a Binary Search Tree in Java.

Posted 24 April 2011 - 07:13 PM

so can I create more than 2 child in BinaryTree under each node?
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Create a Binary Search Tree in Java.

Posted 24 April 2011 - 07:18 PM

View Postcharli, on 24 April 2011 - 09:13 PM, said:

so can I create more than 2 child in BinaryTree under each node?

The primary principle behind binary tree is
- you have a node with 2 sons
- all of these 2 nodes can have 2 great-sons
- all of these 4 grant-sons nodes can have 2 great-great-sons
- ...

you can go millions of descendants that way but always by 2 (pairs)
Was This Post Helpful? 1
  • +
  • -

#9 charli  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-April 11

Re: Create a Binary Search Tree in Java.

Posted 24 April 2011 - 07:23 PM

so,is it the search tree, which can let me create a multiple child under one parent? (as "macosxnerd101" said.) if so please tell me how to create search tree. and how to create more than two node under each node. please help, thanks.
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Create a Binary Search Tree in Java.

Posted 24 April 2011 - 07:28 PM

Look at the links in my post #6
Was This Post Helpful? 1
  • +
  • -

#11 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7565
  • View blog
  • Posts: 12,697
  • Joined: 19-March 11

Re: Create a Binary Search Tree in Java.

Posted 24 April 2011 - 08:03 PM

View Postcharli, on 24 April 2011 - 09:23 PM, said:

so,is it the search tree, which can let me create a multiple child under one parent? (as "macosxnerd101" said.) if so please tell me how to create search tree. and how to create more than two node under each node. please help, thanks.


You can easily create a tree with any number of children. Simplest way would be to give each Node an arraylist<Node> of descendants. Sorted.

This won't be the most searchable tree ever made, but it might be good for modelling some relationships - for example, if you wanted to describe evolutionary hierarchies, there's no limits on how many species you might say descend from species S. Or you might want to describe a family tree - I've heard tell that some couples have more than two children, so you'd need something like this for that purpose.

If you want to constrain the number of descendants, use an array of Nodes rather than four or six or however many individually named variables. Never treat four things which make up a collection as four separate objects. You only have so many hairs on your head, no need to rip them all out on this one program.
Was This Post Helpful? 3
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Create a Binary Search Tree in Java.

Posted 24 April 2011 - 08:09 PM

Topic title renamed to be more descriptive with reallity
Was This Post Helpful? 1
  • +
  • -

#13 charli  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-April 11

Re: Create a Binary Search Tree in Java.

Posted 24 April 2011 - 08:09 PM

Nice, thanks that helped a lot.
Thanks a lot.
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10387
  • View blog
  • Posts: 38,438
  • Joined: 27-December 08

Re: Create a Binary Search Tree in Java.

Posted 24 April 2011 - 08:18 PM

Also, check out the Java Data Structures Resource Thread. We've got a lot of good resources you may find helpful. :)
Was This Post Helpful? 2
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Create a Binary Search Tree in Java.

Posted 24 April 2011 - 08:23 PM

View Postcharli, on 24 April 2011 - 10:09 PM, said:

Nice, thanks that helped a lot.
Thanks a lot.

And don't forget to click +1 for all the ones who helped you :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1