in the codes, the p1.root is always null, even if i know the root
I expect the element can be inserted normally, and the function of insert can be valid.
I set a tree structure and set a function “insert” to add elements to the tree.
Can anyone tell me why my root is always null and why my insert method is not assigning a value to the root? Any tips on better design for data structures in Java would also be much appreciated.
<code>public class TreeDemo {public static class TreeNode{public int val;public TreeNode left;public TreeNode right;public TreeNode(int x){val=x;}}public static class Tree{public TreeNode root;public Tree(){System.out.println("construct");root=null;}public void Travel(TreeNode root){if(root==null){return;}else{Travel(root.left);System.out.println(root.val);Travel(root.right);}}// public void Delete(int val){// if(root==null){// System.out.println("wu");// }// if(root.val==val){//// }// }}public static boolean Search(TreeNode root,int val){if(root==null){return false;}if(root.val==val){return true;}else if(root.val<val){return Search(root.left,val);}else{return Search(root.right,val);}}public static void insert(TreeNode root,int val){if(root==null){System.out.println("null");root=new TreeNode(val);System.out.println(root);return;}if(root.val>=val){insert(root.right,val);}else{insert(root.left,val);}}public static void main(String[] args) {//System.out.println("hello world");Tree p1=new Tree();System.out.println(p1.root);p1.root=new TreeNode(10);System.out.println(p1.root.val);System.out.println(p1.root);System.out.println("----");insert(p1.root,20);insert(p1.root,20);insert(p1.root,30);insert(p1.root,40);insert(p1.root,50);insert(p1.root,60);System.out.println("---------------");p1.Travel(p1.root);}}</code><code>public class TreeDemo { public static class TreeNode{ public int val; public TreeNode left; public TreeNode right; public TreeNode(int x){val=x;} } public static class Tree{ public TreeNode root; public Tree(){ System.out.println("construct"); root=null; } public void Travel(TreeNode root){ if(root==null){ return; }else{ Travel(root.left); System.out.println(root.val); Travel(root.right); } } // public void Delete(int val){ // if(root==null){ // System.out.println("wu"); // } // if(root.val==val){ // // } // } } public static boolean Search(TreeNode root,int val){ if(root==null){ return false; } if(root.val==val){ return true; }else if(root.val<val){ return Search(root.left,val); }else{ return Search(root.right,val); } } public static void insert(TreeNode root,int val) { if(root==null){ System.out.println("null"); root=new TreeNode(val); System.out.println(root); return; } if(root.val>=val){ insert(root.right,val); }else{ insert(root.left,val); } } public static void main(String[] args) { //System.out.println("hello world"); Tree p1=new Tree(); System.out.println(p1.root); p1.root=new TreeNode(10); System.out.println(p1.root.val); System.out.println(p1.root); System.out.println("----"); insert(p1.root,20); insert(p1.root,20); insert(p1.root,30); insert(p1.root,40); insert(p1.root,50); insert(p1.root,60); System.out.println("---------------"); p1.Travel(p1.root); } } </code>public class TreeDemo { public static class TreeNode{ public int val; public TreeNode left; public TreeNode right; public TreeNode(int x){val=x;} } public static class Tree{ public TreeNode root; public Tree(){ System.out.println("construct"); root=null; } public void Travel(TreeNode root){ if(root==null){ return; }else{ Travel(root.left); System.out.println(root.val); Travel(root.right); } } // public void Delete(int val){ // if(root==null){ // System.out.println("wu"); // } // if(root.val==val){ // // } // } } public static boolean Search(TreeNode root,int val){ if(root==null){ return false; } if(root.val==val){ return true; }else if(root.val<val){ return Search(root.left,val); }else{ return Search(root.right,val); } } public static void insert(TreeNode root,int val) { if(root==null){ System.out.println("null"); root=new TreeNode(val); System.out.println(root); return; } if(root.val>=val){ insert(root.right,val); }else{ insert(root.left,val); } } public static void main(String[] args) { //System.out.println("hello world"); Tree p1=new Tree(); System.out.println(p1.root); p1.root=new TreeNode(10); System.out.println(p1.root.val); System.out.println(p1.root); System.out.println("----"); insert(p1.root,20); insert(p1.root,20); insert(p1.root,30); insert(p1.root,40); insert(p1.root,50); insert(p1.root,60); System.out.println("---------------"); p1.Travel(p1.root); } }
New contributor
xiaofeiChen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.