数据结构之二叉树
本文讲解二叉树的基本操作:
- 查找节点
- 计算树的高度
- 清空树
- 递归遍历:先序遍历、中序遍历、后序遍历
- 按层遍历
来看一下树的结构:
class TreeNode {
String value;
TreeNode left;
TreeNode right;
public TreeNode() {
}
public TreeNode(String value) {
this.value = value;
}
}
首先,为了方便后面看到效果,先手动初始化一个有4个节点的二叉树:
Tree tree = new Tree();
TreeNode root = new TreeNode("root");
TreeNode node1 = new TreeNode("ndoe1");
TreeNode node2 = new TreeNode("ndoe2");
TreeNode node3 = new TreeNode("ndoe3");
root.left = node1;
root.right = node2;
node1.left = node3;
查找节点
//查找节点
public TreeNode findNode(TreeNode treeNode, String value) {
if(null == treeNode)
return null;
if(treeNode.value.equals(value))
return treeNode;
TreeNode leftNode = findNode(treeNode.left, value);//递归左子树
TreeNode rightNode = findNode(treeNode.right, value);//递归右子树
if(leftNode.value.equals(value))
return leftNode;
if(rightNode.value.equals(value))
return rightNode;
return null;
}
计算树的深度
//计算树的深度
//递归方法
public int deepth(TreeNode treeNode) {
if(treeNode == null)
return 0;
int left = deepth(treeNode.left);
int right = deepth(treeNode.right);
return left > right? left + 1: right + 1;
}
清空树
//清空二叉树
public void clearTreeNode(TreeNode treeNode) {
if(null != treeNode) {
clearTreeNode(treeNode.left);
clearTreeNode(treeNode.right);
treeNode = null;
}
}
递归遍历
//遍历1 先序遍历
public void showDLR(TreeNode treeNode) {
if(null != treeNode) {
showData(treeNode);
showDLR(treeNode.left);
showDLR(treeNode.right);
}
}
//遍历2 中序遍历
public void showLDR(TreeNode treeNode) {
if(null != treeNode) {
showLDR(treeNode.left);
showData(treeNode);
showLDR(treeNode.right);
}
}
//遍历3 后序遍历
public void showLRD(TreeNode treeNode) {
if(null != treeNode) {
showLRD(treeNode.left);
showLRD(treeNode.right);
showData(treeNode);
}
}
按层遍历
//遍历4 按层遍历 借助队列 先进先出
public void showByLevel(TreeNode treeNode) {
if(null == treeNode)
return;
LinkedList<TreeNode> list = new LinkedList<>();
TreeNode current;
list.offer(treeNode);//将根节点入队
while(!list.isEmpty()) {
current = list.poll();//队首出队
showData(current);//打印节点
if(null != current.left) {
list.offer(current.left);
}
if(null != current.right) {
list.offer(current.right);
}
}
}
运行结果:
树的深度是:3
先序遍历:
root-->ndoe1-->ndoe3-->ndoe2-->
中序遍历:
ndoe3-->ndoe1-->root-->ndoe2-->
后序遍历:
ndoe3-->ndoe1-->ndoe2-->root-->
按层遍历
root-->ndoe1-->ndoe2-->ndoe3-->
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。