<h4>最下面给出不用返回值和用返回值的二叉树插入Java代码</h4> <h4>建立二叉树的代码,在java中创建二叉树的方法注意用返回值,因为不存在c语言中的引用传递,在java中只有值传递没有使用返回值为错误。</h4><h4>那么为什么之前的例子中将对象作为参数时,对对象内容的更改还是正确的,也没有使用返回值,二者看似矛盾,其实并不矛盾,</h4><h4>以前都没有理解到这个本质,本质就是参数中,确实是有一个临时变量,交换形式对象参数,实际的参数不会改变,</h4><h4>但是改变形式参数的引用所指向的内容,即这个对象本身改变,其实参所引用的对象也是同一个对象,当然该对象也会发生变化,</h4><h4>因为实参和形参引用的都是同一个对象,只不过是两份地址的拷贝。</h4><p><strong>即便是c语言如果不用引用传递-&,就是说用指针,也需要用返回值的方法建立二叉树,才能将已建立好的二叉树头指针返回给打印函数的参数。</strong></p><p><strong>当然java中可以采用对私有变量等操作,即不不使用参数传递,而仅仅是创建函数和打印函数共同操作的是同一个变量也可以吧,估计递归就不好用了</strong></p> <strong>使用无返回值的insert方法,new的对象在方法结束后就被销毁,插入失败</strong> <a target=_blank href="http://zhidao.baidu.com/link?url=qoGArQDhFXjrHXfrfc3QrNP3wCiBsJyEEOt-A7UwLoM7ELrMsX0wlgSBEnSBdM1ObJ2LNtQ_UVuL6AG47skXvK">百度知道引用</a> <a target=_blank href="http://blog.csdn.net/tianlincao/article/details/6875593">Java传值</a> <a target=_blank href="http://www.cnblogs.com/wangzhewang/archive/2011/09/30/2196744.html">Java实现二叉树</a> </pre><pre name="code" class="java">/* * A single LinkedList for clarify java's parameter in function * so insert node to a tree in java you should have a return value * * Reference: * * http://zhidao.baidu.com/link?url=qoGArQDhFXjrHXfrfc3QrNP3wCiBsJyEEOt-A7UwLoM7ELrMsX0wlgSBEnSBdM1ObJ2LNtQ_UVuL6AG47skXvK * * http://blog.csdn.net/tianlincao/article/details/6875593 * * http://www.cnblogs.com/wangzhewang/archive/2011/09/30/2196744.html * */ public class test { public static void main(String args[]) { Item it=new Item(); it.setString("one"); // System.out.println(it.getString()); it.modifyDesc(it, "1"); it.modifyDesc(it, "3"); it.modifyDesc(it, "5"); it.modifyDesc(it, "2"); it.modifyDesc(it, "6"); it.modifyDesc(it, "0"); //or Item.modify... is OK // System.out.println(it.getString()); while (it != null) { System.out.println(it.getString()); it = it.next; } } } class Item{ private String desc; Item next; public String getString() { return desc; } public void setString(String s) { desc=s; } public static void modifyDesc(Item item, String str) { //TODO !!! /* * java cannot change the function reference, such as item=new Item(); * and item = it ... * but can change it value and value's reference * such as item.next=new Item(); * */ // item=new Item(); // System.out.println("create obj and delete it at the end of function"); // item.setString("change"); while (item.next != null) { item = item.next; } item.next=new Item(); item.next.setString(str); } }
插入节点代码
private void insert(E e, TreeNode<E> currentRoot) { if(currentRoot.data.compareTo(e) > 0){ // node.data > e if (currentRoot.left == null) { currentRoot.left = new TreeNode<>(); currentRoot.left.data = e; currentRoot.left.left = null; currentRoot.left.right = null; this.size++; System.out.println("insert success " + e); return; }else { insert(e, currentRoot.left); } }else if(currentRoot.data.compareTo(e) < 0){ if (currentRoot.right == null) { currentRoot.right = new TreeNode<>(); currentRoot.right.data = e; currentRoot.right.left = null; currentRoot.right.right = null; this.size++; System.out.println("insert success " + e); return; }else { insert(e, currentRoot.right); } } } public TreeNode<E> insertNode(E e,TreeNode<E> currentRoot){ if(currentRoot == null){ return new TreeNode<E>(e); } if(currentRoot.data.compareTo(e) > 0){ currentRoot.left = insertNode(e,currentRoot.left); }else if(currentRoot.data.compareTo(e) < 0){ currentRoot.right = insertNode(e,currentRoot.right); }else{/*equal,do nothing*/} this.size++; return currentRoot; }