树
二叉树
能提高数据存储,读取的效率,比如利用二叉排序树(Binary Sort Tree),既可以保证数据的检索速度,同时也可以保证数据的插入,删除,修改的速度。
分析二叉树的前序,中序,后序的遍历步骤
1.创建一颗二叉树
---
2.前序遍历
2.1先输出当前节点(初始的时候是root节点)2.2如果左子节点不为空,则递归继续前序遍历
2.2如果右子节点不为空,则递归继续前序遍历
---
3.中序遍历
3.1如果当前节点的左子节点不为空,则递归中序遍历,3.2输出当前节点
3.2如果当前节点的右子节点不为空,则递归中序遍历
---
4.后序遍历
4.1如果当前节点的左子节点不为空,则递归后序遍历,
4.2如果当前节点的右子节点不为空,则递归后序遍历
4.3输出当前节点
package com.zhangan.tree;
//创建节点
class HerNode {
private int no;
private String name;
private HerNode left;
private HerNode right;
//左右指针默认为空
public HerNode(int no, String name) {
this.no = no;
this.name = name;
}
@Override
public String toString() {
return "HerNode{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public HerNode getLeft() {
return left;
}
public void setLeft(HerNode left) {
this.left = left;
}
public HerNode getRight() {
return right;
}
public void setRight(HerNode right) {
this.right = right;
}
//前序遍历
public void preOrder() {
System.out.println(this);//先输出父节点
//递归向左子树前序遍历
if (this.left != null) {
this.left.preOrder();
}
//递归向右子树前序遍历
if (this.right != null) {
this.right.preOrder();
}
}
//中序遍历
public void infixOrder() {
//递归向左子树前序遍历
if (this.left != null) {
this.left.infixOrder();
}
System.out.println(this);//输出父节点
//递归向右子树前序遍历
if (this.right != null) {
this.right.infixOrder();
}
}
//后序遍历
public void postOrder() {
//递归向左子树前序遍历
if (this.left != null) {
this.left.postOrder();
}
//递归向右子树前序遍历
if (this.right != null) {
this.right.postOrder();
}
System.out.println(this);//先输出父节点
}
//前序遍历查找
public HerNode preOrderSearch(int no) {
System.out.println("***前序次数***");
//比较当前节点是不是
if (this.no == no) {
return this;
}
HerNode resNode = null;
//1.则判断当前结点的左子节点是否为空,如果不为空,则递归前序查找
//2.如果左递归前序查找,找到结点,则返回
if (this.left != null) {
resNode = this.left.preOrderSearch(no);
}
if (resNode != null) {//找到了则返回
return resNode;
}
//左递归没找到没找到 开始向右递归
if (this.right != null) {
resNode = this.right.preOrderSearch(no);
}
//不管找没找到 都要返回
return resNode;
}
//中序遍历查找
public HerNode infixOrderSearch(int no) {
HerNode resNode = null;
//1.则判断当前结点的左子节点是否为空,如果不为空,则递归中序查找
//2.如果左递归前序查找,找到结点,则返回
if (this.left != null) {
resNode = this.left.infixOrderSearch(no);
}
if (resNode != null) {//找到了则返回
return resNode;
}
System.out.println("……中序次数……");
//比较当前节点是不是
if (this.no == no) {
return this;
}
//左递归没找到没找到 开始向右递归
if (this.right != null) {
resNode = this.right.infixOrderSearch(no);
}
//不管找没找到 都要返回
return resNode;
}
//后序遍历查找
public HerNode postOrderSearch(int no) {
HerNode resNode = null;
//1.则判断当前结点的左子节点是否为空,如果不为空,则递归后中序查找
//2.如果左递归前序查找,找到结点,则返回
if (this.left != null) {
resNode = this.left.postOrderSearch(no);
}
if (resNode != null) {//找到了则返回
return resNode;
}
//左递归没找到没找到 开始向右递归
if (this.right != null) {
resNode = this.right.postOrderSearch(no);
}
if (resNode != null) {//找到了则返回
return resNode;
}
System.out.println("---后序次数---");
//比较当前节点是不是
if (this.no == no) {
return this;
}
//不管找没找到 都要返回
return resNode;
}
//递归删除节点
//如果要删除的节点是叶子节点 则删除该节点
//如果是非叶子节点,则删除该树
public void delNode(int no) {
/**
* 1。因为我们的二叉树是单向的,所以我们是判断当前结点的子结点是否需要删除结点,而不能去判断当前这个结点是不是需要删除结点。
* 2。如果当前结点的左子结点不为空,并且左子结点就是要删除结点,就将this.left = null;并且就返回(结束递归删除)
* 3。如果当前结点的右子结点不为空,并且右子结点就是要删除结点,就将this.right= null ;并且就返回(结束递归删除)
* 4。如果第2和第3步没有删除结点,那么我们就需要向左子树进行递归删除
* 5.如果第4步也没有删除结点,则应当向右子树进行递归删除。
*/
if (this.left != null && this.left.no == no) {
this.left = null;
return;
}
if (this.right != null && this.right.no == no) {
this.right = null;
return;
}
if (this.left != null) {
this.left.delNode(no);
}
if (this.right != null) {
this.right.delNode(no);
}
}
}
//定义一个树
class BinaryTree {
private HerNode root;//根节点
public void setRoot(HerNode root) {
this.root = root;
}
//前序遍历
public void preOrder() {
if (root != null) {
root.preOrder();
} else {
System.out.println("二叉树是空的");
}
}
//中序
public void infixOrder() {
if (root != null) {
root.infixOrder();
} else {
System.out.println("二叉树是空的");
}
}
//后序
public void postOrder() {
if (root != null) {
root.postOrder();
} else {
System.out.println("二叉树是空的");
}
}
//前序查找
public HerNode preOrderSearch(int no) {
if (root != null) {
return root.preOrderSearch(no);
} else {
return null;
}
}
//中序查找
public HerNode infixOrderSearch(int no) {
if (root != null) {
return root.infixOrderSearch(no);
} else {
return null;
}
}
//后序查找
public HerNode postOrderSearch(int no) {
if (root != null) {
return root.postOrderSearch(no);
} else {
return null;
}
}
//删除节点
public void delNode(int no) {
if (root != null) {
//如果只有root一个节点,先判断root是不是
if (root.getNo() == no) {
root = null;
} else {
root.delNode(no);
}
} else {
System.out.println("空树无法删除");
}
}
}
public class BinaryTreeDemo {
public static void main(String[] args) {
//创建一棵二叉树
BinaryTree binaryTree = new BinaryTree();
//创建需要的节点
HerNode root = new HerNode(1, "宋江");
HerNode node2 = new HerNode(2, "吴用");
HerNode node3 = new HerNode(3, "卢俊义");
HerNode node4 = new HerNode(4, "林冲");
HerNode node5 = new HerNode(5, "关胜");
//手动创建二叉树
root.setLeft(node2);
root.setRight(node3);
node3.setLeft(node5);
node3.setRight(node4);
binaryTree.setRoot(root);
// //测试
// System.out.println("前序遍历: ");
// binaryTree.preOrder();
// System.out.println("------------");
//
// System.out.println("中序遍历:");
// binaryTree.infixOrder();
// System.out.println("------------");
//
// System.out.println("后序遍历:");
// binaryTree.postOrder();
//
// System.out.println("==============");
// System.out.println("前序查找↓");
// HerNode search = binaryTree.preOrderSearch(5);
// if (search != null) {
// System.out.println(search.toString());
// } else {
// System.out.println("没有该数据");
// }
//
// System.out.println("中序查找↓");
// HerNode search2 = binaryTree.infixOrderSearch(5);
// if (search != null) {
// System.out.println(search2.toString());
// } else {
// System.out.println("没有该数据");
// }
//
// System.out.println("后序查找↓");
// HerNode search3 = binaryTree.postOrderSearch(5);
// if (search != null) {
// System.out.println(search3.toString());
// } else {
// System.out.println("没有该数据");
// }
//测试删除
System.out.println("删除前");
binaryTree.preOrder();
binaryTree.delNode(3);
System.out.println("删除后");
binaryTree.preOrder();
}
}