A question about binary tree(java)

  • Java
  • Thread starter transgalactic
  • Start date
  • Tags
    Binary
In summary, The goal of the conversation is to build a software that takes a number and splits it into nodes of a tree. The person has tried to implement the code but it did not give the desired result. They are having trouble with putting the values inside the nodes and understanding where they made a mistake in the tree methods. They were told that their node copy constructor is bad and they need to put insert(num) inside the constructor. They want all the prime numbers that multiply to give the original number to be in the leaves of the tree.
  • #1
transgalactic
1,395
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
  • #2
Are you having trouble with the factoring part, or with the display part?
 
  • #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 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
 

1. What is a binary tree in Java?

A binary tree in Java is a type of data structure that is composed of nodes, where each node has at most two children, referred to as the left and right child. It is a popular way to organize data in a hierarchical structure, with the topmost node being the root and the bottommost nodes being the leaves.

2. How do you implement a binary tree in Java?

To implement a binary tree in Java, you can use the built-in Tree or Map data structures in the Java Collections Framework. Alternatively, you can create a custom class for a binary tree where each node contains a reference to its left and right child, as well as the data stored in the node.

3. What operations can you perform on a binary tree in Java?

Some common operations that can be performed on a binary tree in Java include inserting a new node, deleting a node, searching for a specific value, traversing the tree to access all the nodes, and balancing the tree to maintain its efficiency.

4. What are the advantages of using a binary tree in Java?

A binary tree offers efficient data storage and retrieval as it is a self-balancing data structure. It also allows for easy sorting and searching of data, making it useful for applications that require quick access to data. Additionally, it is relatively easy to implement and understand compared to other data structures.

5. How do you traverse a binary tree in Java?

There are three common ways to traverse a binary tree in Java - in-order, pre-order, and post-order. In-order traversal visits the left child, then the current node, and then the right child. Pre-order traversal visits the current node, then the left child, and then the right child. Post-order traversal visits the left child, then the right child, and finally the current node. These traversals can be implemented recursively or iteratively using stacks or queues.

Similar threads

  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
4
Views
822
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
2
Views
603
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top