Java 学习笔记 -- 树

# 树的构建
    public vlass TreeNode<T> {
        /*节点:数据 + 左右孩子*/
        T value;
        TreeNode<T> leftChild;
        TreeNode<T> rightChild;

        /*相关方法*/
        // 添加子树 -- addleft(T value) || addright(T value)
        // 节点相等判断 -- [@override] public boolean equals(Object obj)
        // 哈希值 -- hashCode()
        // 字符形式返回数据内容 toString()
    }


# 数的基本操作 
 public class TreeTools{
     /*判断树节点个数*/
       //传入父节点 --> 左右子树节点个数 + 1

     /*判断树深度*/
       //左右子树深度最大值 + 1
     
     /*前中后序遍历 【重写visitNode()方法】*/ 
       // 前: 头、左、右
       // 中: 左、头、右
       // 后: 左、右、头
     
     /*分层遍历 -- 引入队列实现*/
 }


#代码实现

/*树的节点结构*/
public calss TreeNode<T> {
	T value;

	TreeNode<T> leftChild;
	TreeNode<T> rightChild;

	TreeNode(T value){
		this.value = value;
	}


	public void addLeft(T value){
		TreeNode<T> leftChild = new TreeNode<t>(value);
		this.leftChild = leftChild;
	}

	public void addRight(T value){
		TreeNode<T> rightChild = new TreeNode<T>(value);
		this.rightChild = rightChild;
	}

	@Override
	public boolean equals(Object obj){
		if(!(obj instanceof TreeNode)){
			return false;
		}
		return this.value.equals(((TreeNode<?>)obj).value);
	}

	@Override
	public int hashCode(){
		return this.value.hashCode();
	}

	@override
	public String toString(){
		return this.value == null ? "" : this.value.toString();
	}

}

/*树的基本操作*/
public class TreeTools {
	/*判断树的节点个数*/
	public static <T> int getTreeNum(TreeNode<T> root){
		if(root == null){
			return 0;
		}
		return getTreeNum(root.leftChild) + getTreeNum(root.rightChild) + 1;
	}


	/*判断树的深度*/
	public static <T> int getTreeDepth(TreeNode<T> root){
		if(root == null){
			return 0;
		}

		int leftDepth = getTreeDepth(root.leftChild) + 1;
		int rightDepth = getTreeDepth(root.rightChild) + 1;

		return Math.max(leftDepth, rightDepth);
	}

	/*前序遍历*/
	public static <T> void preOrderTraval(TreeNode<T> root){
		if(root == null){
			return ;
		}

		visitNode(root);
		preOrderTraval(root.leftChild);
		preOrderTraval(root.rightChild);
	}

	/*中序遍历*/
	public static <T> void midOrderTravel(TreeNode<T> root){
		if(root == null){
			return ;
		}

		midOrderTravel(root.leftChild);
		visitNode(root);
		midOrderTravel(root.rightChild);
	}

	/*后序遍历*/
	public static <T> void backOrderTravel(TreeNode<T> root){
		if(root == null){
			return ;
		}

		backOrderTravel(root.leftChild);
		backOrderTravel(root.rightChild);
		visitNode(root);
	}

	/*访问节点*/
	private static <T> void vistNode(TreeNode<T> node){
		System.out.print(node.value + "\t");
	}

	/*分层遍历*/
	//import java.util.Queue; 继承了Collection接口  
	public static <T> void levelTravel(TreeNode<T> root){
		Queue<TreeNode<T>> q = new LinkedList<TreeNode<T>>();
		q.offer(root);//添加元素 队列满返回false
		while(!q.isEmpty()){
			TreeNode<T> temp = q.poll();//移除并返回队列元素
			visitNode(temp);
			if(temp.leftChild != null){
				q.offer(temp.leftChild);
			}
			if(temp.rightChild != null){
				q.offer(temp.rightChild);
			}

		}
	}

	/*求出第K层节点个数*/
	public static <T> int getNumForKlevel(TreeNode<T> root, int k){
		if(root == null || k < 1){
			return 0;
		}
		if(k==1){
			return 1;
		}
		int leftnum = getNumForKlevel(root.leftChild, k-1);
		int rightnum = getNumForKlevel(root.rightChild, k-1);
		
		return leftNum + rightNum;
	}

	/*叶子节点个数*/
	public static <T> int getLeafNum(TreeNode<T> root){
		if(root == null){
			return 0;
		}
		if(root.leftChild == null && root.rightChild == null){
			return 1;
		}
		int leftnum = getLeafNum(root.leftChild);
		int rightnum = getLeafNum(root.rightChild);
		return leftnum + rightnum;
	}

	/*变换根节点左右子树*/
	public static <T> TreeNode<T> exchange(TreeNode<T> root){
		if(root == null){
			return null;
		}
		TreeNode<T> left = exchange(root.leftChild);
		TreeNode<T> right = exchange(root.rightChild);

		root.leftChild = right;
		root.rightChild = left;

		return root;
	}


	/*查看node是否是root子节点*/
	public static <T> boolean nodeIsChild(TreeNode<T> root, TreeNode<T> node){
		if(root == null || node == null){
			return false;
		}
		if(root == node){
			return true;
		}
		boolean isFind = nodeIsChild(root.leftChild, node);
		if(!isFind){
			isFind = nodeIsChild(root.rightChild, node);
		}

		return isFind;
	}

	/*查看公共父节点*/
	public static <T> TreeNode<T> findAllFatherNode(TreeNode<T> root, TreeNode<T> lNode, TreeNode<T> rNode){
		if(lNode == root || rNode == root){
			return root;
		}
		if(root == null || lNode == null || rNode == null)
		{
			return null;
		}

		if(nodeIsChild(root.leftChild, lNode)){
			if(nodeIsChild(root.rightChild, rNode)){
				return root;
			}else{
				return findAllFatherNode(root.leftChild, lNode, rNode);
			}
		}else{
			if(nodeIsChild(root.leftChild, rNode){
				return root;
			}else{
				return findAllFatherNode(root.rightChild, lNode, rNode);	
			}
		}
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值