Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

A question about binary tree(java)

  1. Mar 18, 2008 #1
    ok my gole is to build a software that
    takes a numbers and splits it like this into nodes of a tree
    Code (Text):

         8        6
      2  4       2 3
     1 2  2    1 2  1 3
         1 2
    i have tried some code but it doesnt give me the right resolt
    i tried to implement it in this code
    Code (Text):

    public class main {
        public static void main(String[] args) {
    BTree t=new BTree();
    public class BTree {
    BNode root;
        public  void insert(int num){
             root=new BNode(num);
    public class BNode {
        private BNode left;
        private BNode right;
    private int data;
    BNode(int num){
    public void insert(int num){
        int sm=small(num);
        int large=num/sm;
        if (num>large){
        this.right=new BNode(sm);
        this.left=new BNode (large);
    public static int small(int num){
        double sqr=Math.sqrt(num);
        int small=(int)Math.floor(sqr);
        while (num%small!=0){
        return small;
  2. jcsd
  3. Mar 18, 2008 #2


    User Avatar
    Science Advisor
    Homework Helper

    Are you having trouble with the factoring part, or with the display part?
  4. Mar 18, 2008 #3
    i have build a method inside BNode which gets a prime number out of it
    so i can get the other big number by big=num/small

    i have trouble with putting the values inside the nodes
    i dont quite understand where did i made a mistake in this tree methods
    i was tald that my node copy constructor is bad that i need to put
    inside the constructor
    i cant understand where is the mistake??

    i want that all of the prime numbers that by multiplicationgives us the origiginal given number
    will be in the leaves of the tree
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook