java实现二叉树

结点类


package tree;

public class TreeNode<T> {
	public T data;  //数据域
	public TreeNode<T> left,right;
	
	public TreeNode(T data, TreeNode<T> left,TreeNode<T> right){
		this.data = data;
		this.left = left;
		this.right = right;
	}
	
	public TreeNode(T data){
		this(data,null, null);
	}
	
	public String toString(){
		return this.data.toString();
	}
	
	//判断是否为页子结点
	public boolean isLeaf(){
		return this.left == null&& this.right == null;
	}
}

二叉树

package tree;

public class Demo1Tree<T> {
	public TreeNode<T> root;
	
	public Demo1Tree(){
		this.root  = null;
	}

	public boolean isEmpty(){
		return this.root == null;
	} 
	
	//插入x作为根结点
	public TreeNode<T> insert(T x){
		return this.root = new TreeNode<T>(x,this.root,null);
	}
	
	//插入x作为parent结点的左右子结点,leftChild == true ,取值为左,反之为右
	public TreeNode<T> insert(TreeNode<T> parent, T x,boolean leftChild){
		if(x == null){
			return null;
		}
		if(leftChild)
			//作为左孩子
			return  parent.left = new TreeNode<T>(x,null,parent.right);
			//作为右孩子
		return parent.right = new TreeNode<T>(x,null,parent.right);
	}
	
	//删除parent结点的左右子树
	
	public void remove(TreeNode<T> parent, boolean leftChild){
		if(leftChild)
			parent.left = null;
		else  
			parent.right = null;
	}
	
	//全部删除
	
	public void clear(){
		this.root = null;
	}
	
	//根次序遍历序列
	public void preorder(){
		preorder(this.root);
		System.out.println();
	}
	
	public void preorder(TreeNode<T> p){
		if(p != null){
			System.out.print(p.data.toString()+ "  ");
			preorder(p.left);
			preorder(p.right);
		}
	}
	
	//获取结点个数
	public int size(){ 
		return size(root);
	}
	
	public int size(TreeNode<T> p){
		if(p == null) return 0;
		
		return 1+size(p.left)+size(p.right);
	}
	
	//获取高度
	public int height(){
		return height(root);
	}
	
	public int height(TreeNode<T> p){
		if(p == null)
			return 0;
		
		int lh = height(p.left); //返回左子树的高度
		int rh = height(p.right); //返回右子树的高度
		return (lh>rh)?lh+1:rh+1;
	}
	
	//查找并返回首次出现的关键字为key的元素
	public T search(T key){
		return searchNode(root,key).data;	
	}
	
//	public TreeNode<T> searchNode(T key){
//		return searchNode(root,key);
//	}
	
	public TreeNode<T> searchNode(TreeNode<T> p, T key){
		if(p == null || key == null)   return null;
		if(p.data.equals(key))  return p;
		
		//递归在左子树中找
		TreeNode<T> find  =  searchNode(p.left,key);
		
		//找不到换右子树
		if(find == null)  find = searchNode(p.right,key);
		return find;
	}
	
	//返回node结点的父母结点,若空树、未找到或node为根结点,则返回null
	public TreeNode<T> getParent(TreeNode<T> node){
		if(root == null || node == null || node ==root)   
			return null;
		return  getParent(root,node);
	}
	
	public TreeNode<T> getParent(TreeNode<T> p , TreeNode<T> node){
		//空树
		if(p == null) 
			return null;
		if(p.left == node || p.right == node)
			return p;
		
		//从左子树找
		TreeNode<T>  find = getParent(p.left,node);
		
		//左边没有换右边找
		if(find == null)
			find = getParent(p.right,node);
		return find;
 	}
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/* * 二叉树节点ADT接口 */ package dsa; public interface BinTreePosition extends Position { //判断是否有父亲(为使代码描述简洁) public boolean hasParent(); //返回当前节点的父节点 public BinTreePosition getParent(); //设置当前节点的父节点 public void setParent(BinTreePosition p); //判断是否为叶子 public boolean isLeaf(); //判断是否为左孩子(为使代码描述简洁) public boolean isLChild(); //判断是否有左孩子(为使代码描述简洁) public boolean hasLChild(); //返回当前节点的左孩子 public BinTreePosition getLChild(); //设置当前节点的左孩子(注意:this.lChild和c.parent都不一定为空) public void setLChild(BinTreePosition c); //判断是否为右孩子(为使代码描述简洁) public boolean isRChild(); //判断是否有右孩子(为使代码描述简洁) public boolean hasRChild(); //返回当前节点的右孩子 public BinTreePosition getRChild(); //设置当前节点的右孩子(注意:this.rChild和c.parent都不一定为空) public void setRChild(BinTreePosition c); //返回当前节点后代元素的数目 public int getSize(); //在孩子发生变化后,更新当前节点及其祖先的规模 public void updateSize(); //返回当前节点的高度 public int getHeight(); //在孩子发生变化后,更新当前节点及其祖先的高度 public void updateHeight(); //返回当前节点的深度 public int getDepth(); //在父亲发生变化后,更新当前节点及其后代的深度 public void updateDepth(); //按照中序遍历的次序,找到当前节点的直接前驱 public BinTreePosition getPrev(); //按照中序遍历的次序,找到当前节点的直接后继 public BinTreePosition getSucc(); //断绝当前节点与其父亲的父子关系 //返回当前节点 public BinTreePosition secede(); //将节点c作为当前节点的左孩子 public BinTreePosition attachL(BinTreePosition c); //将节点c作为当前节点的右孩子 public BinTreePosition attachR(BinTreePosition c); //前序遍历 public Iterator elementsPreorder(); //中序遍历 public Iterator elementsInorder(); //后序遍历 public Iterator elementsPostorder(); //层次遍历 public Iterator elementsLevelorder(); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值