在 testMain.java class 中非常插入(int 数据)函数调用之前将根设置为 null,如 btree.insert(1);和 btree.insert(2);
Root is set null before very insert(int data) function call in testMain.java class like btree.insert(1); and btree.insert(2);
below is the code for BinaryTree.java insertion . Every time i insert a new node , the root is null.What i want , after 1st insert , root should not be null and it should remember the 1st insert and then for 2nd insert, the condition if (root ==null) should be false.
import java.util.LinkedList;
public class BinaryTree {
private BTNode root;
public int size;
public BinaryTree(){
this.root= null;
}
// public BTNode getRoot(){
// return this.root;
// }
public void insert(int data){
insert(data, root);
//calling insert function that takes data and root to insert
}
private void insert (int data, BTNode root){
//case 1: no element in binary tree
if (root == null){
// if root is null create a new BTNode and make it root
BTNode newN= new BTNode(data);
newN.setLeft(null);
newN.setRight(null);
root =newN;
//System.out.println(root.getData());
size++;
return;
//return root.getData();
}
//case2: tree not empty
//create a queue and traverse each node left-right
LinkedList<BTNode> q = new LinkedList<BTNode>();
q.addFirst(root);
while(!(q.isEmpty())){ //if queue not empty
BTNode temp= (BTNode) q.removeFirst();
//check left
if (temp.getLeft()==null){
//create a node and set left
BTNode newN= new BTNode(data);
newN.setLeft(null);
newN.setRight(null);
temp.setLeft(newN);
size++;
return;
//return root.getData();
}
else{
q.addLast(temp.getLeft());
}
//check right in case left is not null
if (temp.getRight()==null){
//create a node and set right
BTNode newN= new BTNode(data);
newN.setLeft(null);
newN.setRight(null);
temp.setRight(newN);
size++;
return;
//return root.getData();
}
else{
q.addLast(temp.getRight());
}
}//while loop ends here
return ;
}// insert(data,root) function ends here
}//class ends here
below is code for BTNode.java
public class BTNode {
int data;
private BTNode left=null;
private BTNode right=null;
public BTNode(){
}
public BTNode(int data){
this.data=data;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public BTNode getLeft() {
return left;
}
public void setLeft(BTNode left) {
this.left = left;
}
public BTNode getRight() {
return right;
}
public void setRight(BTNode right) {
this.right = right;
}
}
TestMain.java
public class TestMain {
public static void main(String[] args){
BinaryTree btree = new BinaryTree();
btree.insert(1);
//System.out.println(btree.getRoot().getData());
btree.insert(2);
btree.insert(3);
btree.insert(4);
btree.insert(5);
btree.insert(6);
}
}
TestMain.Java
public class TestMain {
public static void main(String[] args){
BinaryTree btree = new BinaryTree();
btree.insert(1);
System.out.println(btree.getRoot().getData());
}
二叉树内部:
public BTNode getRoot(){
return this.root;
}
private void insert (int data, BTNode rootParameter){ // your problem is here
//case 1: no element in binary tree
if (root == null){
// if root is null create a new BTNode and make it root
BTNode newN= new BTNode(data);
newN.setLeft(null);
newN.setRight(null);
root =newN;
//System.out.println(root.getData());
size++;
return;
//return root.getData();
}
// other part of your code
}
问题是您正在将根分配给参数,但您应该将其分配给方法之外的根变量。你现在不会得到空引用。
below is the code for BinaryTree.java insertion . Every time i insert a new node , the root is null.What i want , after 1st insert , root should not be null and it should remember the 1st insert and then for 2nd insert, the condition if (root ==null) should be false.
import java.util.LinkedList;
public class BinaryTree {
private BTNode root;
public int size;
public BinaryTree(){
this.root= null;
}
// public BTNode getRoot(){
// return this.root;
// }
public void insert(int data){
insert(data, root);
//calling insert function that takes data and root to insert
}
private void insert (int data, BTNode root){
//case 1: no element in binary tree
if (root == null){
// if root is null create a new BTNode and make it root
BTNode newN= new BTNode(data);
newN.setLeft(null);
newN.setRight(null);
root =newN;
//System.out.println(root.getData());
size++;
return;
//return root.getData();
}
//case2: tree not empty
//create a queue and traverse each node left-right
LinkedList<BTNode> q = new LinkedList<BTNode>();
q.addFirst(root);
while(!(q.isEmpty())){ //if queue not empty
BTNode temp= (BTNode) q.removeFirst();
//check left
if (temp.getLeft()==null){
//create a node and set left
BTNode newN= new BTNode(data);
newN.setLeft(null);
newN.setRight(null);
temp.setLeft(newN);
size++;
return;
//return root.getData();
}
else{
q.addLast(temp.getLeft());
}
//check right in case left is not null
if (temp.getRight()==null){
//create a node and set right
BTNode newN= new BTNode(data);
newN.setLeft(null);
newN.setRight(null);
temp.setRight(newN);
size++;
return;
//return root.getData();
}
else{
q.addLast(temp.getRight());
}
}//while loop ends here
return ;
}// insert(data,root) function ends here
}//class ends here
below is code for BTNode.java
public class BTNode {
int data;
private BTNode left=null;
private BTNode right=null;
public BTNode(){
}
public BTNode(int data){
this.data=data;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public BTNode getLeft() {
return left;
}
public void setLeft(BTNode left) {
this.left = left;
}
public BTNode getRight() {
return right;
}
public void setRight(BTNode right) {
this.right = right;
}
}
TestMain.java
public class TestMain {
public static void main(String[] args){
BinaryTree btree = new BinaryTree();
btree.insert(1);
//System.out.println(btree.getRoot().getData());
btree.insert(2);
btree.insert(3);
btree.insert(4);
btree.insert(5);
btree.insert(6);
}
}
TestMain.Java
public class TestMain {
public static void main(String[] args){
BinaryTree btree = new BinaryTree();
btree.insert(1);
System.out.println(btree.getRoot().getData());
}
二叉树内部:
public BTNode getRoot(){
return this.root;
}
private void insert (int data, BTNode rootParameter){ // your problem is here
//case 1: no element in binary tree
if (root == null){
// if root is null create a new BTNode and make it root
BTNode newN= new BTNode(data);
newN.setLeft(null);
newN.setRight(null);
root =newN;
//System.out.println(root.getData());
size++;
return;
//return root.getData();
}
// other part of your code
}
问题是您正在将根分配给参数,但您应该将其分配给方法之外的根变量。你现在不会得到空引用。