A question about binary tree(java)

  • Context: Java 
  • Thread starter Thread starter transgalactic
  • Start date Start date
  • Tags Tags
    Binary
Click For Summary
SUMMARY

The discussion focuses on building a binary tree in Java that splits a number into its prime factors and organizes them into nodes. The user has implemented a basic structure using classes BTree and BNode but encounters issues with inserting values correctly into the tree. The primary concern is ensuring that all prime factors are represented as leaves in the tree, and there is confusion regarding the node copy constructor and the placement of the insert method within the constructor.

PREREQUISITES
  • Java programming fundamentals
  • Understanding of binary tree data structures
  • Knowledge of prime factorization algorithms
  • Experience with object-oriented programming concepts in Java
NEXT STEPS
  • Review Java binary tree implementation techniques
  • Study prime factorization algorithms and their applications
  • Learn about constructors and method overloading in Java
  • Explore debugging techniques for tree data structures in Java
USEFUL FOR

Java developers, computer science students, and anyone interested in implementing and debugging binary tree structures for mathematical computations.

transgalactic
Messages
1,386
Reaction score
0
ok my gole is to build a software that
takes a numbers and splits it like this into nodes of a tree
Code:
         48
     8        6 
  2  4       2 3
 1 2  2    1 2  1 3
     1 2

i have tried some code but it doesn't give me the right resolt
i tried to implement it in this code
Code:
public class main {
 
 
	public static void main(String[] args) {
		
 
 
BTree t=new BTree();
t.insert(16);
 
	}
 
}
 
 
 
public class BTree {
BNode root;
	BTree()
{
	root=null;
}
	
	public  void insert(int num){
		 root=new BNode(num);
		 root.insert(num);
		 
	 }
	
	
}
 
 
public class BNode {
	private BNode left;
	private BNode right;
 
private int data;
 
BNode(){
	left=null;
	right=null;
}
BNode(int num){
	this.data=num;
}
public void insert(int num){
	int sm=small(num);
	int large=num/sm;
      this.data=num;
	if (num>large){
	this.right=new BNode(sm);
	this.left=new BNode (large);
	left.insert(large);
	}
	
}
public static int small(int num){
	double sqr=Math.sqrt(num);
	int small=(int)Math.floor(sqr);
	while (num%small!=0){
		small--;
	}
	return small;
}
 
 
}
 
Technology news on Phys.org
Are you having trouble with the factoring part, or with the display part?
 
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 don't 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
insert(num)
inside the constructor
i can't 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
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K