java 链表 树

<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;
    }



/* * 基于链表实现结构 */ package dsa; public class TreeLinkedList implements Tree { private Object element;//根节点 private TreeLinkedList parent, firstChild, nextSibling;//父亲、长子及最大的弟弟 //(单节点)构造方法 public TreeLinkedList() { this(null, null, null, null); } //构造方法 public TreeLinkedList(Object e, TreeLinkedList p, TreeLinkedList c, TreeLinkedList s) { element = e; parent = p; firstChild = c; nextSibling = s; } /*---------- Tree接口中各方法的实现 ----------*/ //返回当前节点中存放的对象 public Object getElem() { return element; } //将对象obj存入当前节点,并返回此前的内容 public Object setElem(Object obj) { Object bak = element; element = obj; return bak; } //返回当前节点的父节点;对于根节点,返回null public TreeLinkedList getParent() { return parent; } //返回当前节点的长子;若没有孩子,则返回null public TreeLinkedList getFirstChild() { return firstChild; } //返回当前节点的最大弟弟;若没有弟弟,则返回null public TreeLinkedList getNextSibling() { return nextSibling; } //返回当前节点后代元素的数目,即以当前节点为根的子的规模 public int getSize() { int size = 1;//当前节点也是自己的后代 TreeLinkedList subtree = firstChild;//从长子开始 while (null != subtree) {//依次 size += subtree.getSize();//累加 subtree = subtree.getNextSibling();//所有孩子的后代数目 } return size;//即可得到当前节点的后代总数 } //返回当前节点的高度 public int getHeight() { int height = -1; TreeLinkedList subtree = firstChild;//从长子开始 while (null != subtree) {//依次 height = Math.max(height, subtree.getHeight());//在所有孩子中取最大高度 subtree = subtree.getNextSibling(); } return height+1;//即可得到当前节点的高度 } //返回当前节点的深度 public int getDepth() { int depth = 0; TreeLinkedList p = parent;//从父亲开始 while (null != p) {//依次 depth++; p = p.getParent();//访问各个真祖先 } return depth;//真祖先的数目,即为当前节点的深度 } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值