二叉树的基本使用实现
一、二叉树的概念
- 树有很多种,每个节点最多只能有两个子节点的一种形式称为二叉树
- 二叉树的子节点分为左节点和右节点
- 如果该二叉树的所有叶子节点都在最后一层,并且结点总数 s = 2 n − 1 s=2^n -1 s=2n−1,n 为层数,则我们称为满二叉树
- 如果该二叉树的所有叶子节点都在最后一层或者倒数第二层,而且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续,我们称为完全二叉树
二、二叉树遍历、查找、删除
1. 创建节点类
/**
* 创建节点类,实现当前节点的遍历、查找、删除
*/
class Node {
private int no;
private Node left; //默认null
private Node right; //默认null
public Node(int no) {
this.no = no;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
@Override
public String toString() {
return "Node [no=" + no + "]";
}
// 递归删除结点
// 1.如果删除的节点是叶子节点,则删除该节点
// 2.如果删除的节点是非叶子节点,则删除该子树
public void delNode(int no) {
// 如果当前结点的左子结点不为空,并且左子结点就是要删除结点,就将this.left = null;并且就返回(结束递归删除)
if(this.left != null && this.left.no == no) {
this.left = null;
return;
}
// 如果当前结点的右子结点不为空,并且右子结点就是要删除结点,就将this.right= null;并且就返回(结束递归删除)
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);
}
}
// 前序遍历(父->左->右)
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 Node preOrderSearch(int no) {
System.out.println("进入前序遍历查找");
// 比较当前结点是不是
if(this.no == no) {
return this;
}
// 1.则判断当前结点的左子节点是否为空,如果不为空,则递归前序查找
// 2.如果左递归前序查找,找到结点,则返回
Node resNode = null;
if(this.left != null) {
resNode = this.left.preOrderSearch(no);
}
if(resNode != null) {
return resNode;
}
// 1.则判断当前结点的右子节点是否为空,如果不为空,则递归前序查找
// 2.如果右递归前序查找,找到结点,则返回
if(this.right != null) {
resNode = this.right.preOrderSearch(no);
}
return resNode;
}
// 中序遍历查找
public Node infixOrderSearch(int no) {
Node resNode = null;
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 Node postOrderSearch(int no) {
Node resNode = null;
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;
}
}
2. 创建二叉树类
/**
* 创建二叉树类向外提供二叉树的遍历、查找、删除功能
*/
class BinaryTree {
private Node root;
public void setRoot(Node root) {
this.root = root;
}
// 删除结点
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 void preOrder() {
if(this.root != null) {
this.root.preOrder();
}else {
System.out.println("二叉树为空,无法遍历");
}
}
// 中序遍历
public void infixOrder() {
if(this.root != null) {
this.root.infixOrder();
}else {
System.out.println("二叉树为空,无法遍历");
}
}
// 后序遍历
public void postOrder() {
if(this.root != null) {
this.root.postOrder();
}else {
System.out.println("二叉树为空,无法遍历");
}
}
// 前序遍历查找
public Node preOrderSearch(int no) {
if(root != null) {
return root.preOrderSearch(no);
} else {
return null;
}
}
// 中序遍历查找
public Node infixOrderSearch(int no) {
if(root != null) {
return root.infixOrderSearch(no);
}else {
return null;
}
}
// 后序遍历查找
public Node postOrderSearch(int no) {
if(root != null) {
return this.root.postOrderSearch(no);
}else {
return null;
}
}
}
3. 测试二叉树
public class BinaryTreeDemo {
public static void main(String[] args) {
// 创建一颗二叉树
BinaryTree binaryTree = new BinaryTree();
// 创建需要的结点
Node root = new Node(1);
Node node2 = new Node(2);
Node node3 = new Node(3);
Node node4 = new Node(4);
Node node5 = new Node(5);
// 手动创建该二叉树
root.setLeft(node2);
root.setRight(node3);
node3.setRight(node4);
node3.setLeft(node5);
binaryTree.setRoot(root);
System.out.println("前序遍历"); // 1,2,3,5,4
binaryTree.preOrder();
System.out.println("中序遍历");
binaryTree.infixOrder(); // 2,1,5,3,4
System.out.println("后序遍历");
binaryTree.postOrder(); // 2,5,4,3,1
System.out.println("前序遍历方式查找~~~");
Node resNode1 = binaryTree.preOrderSearch(5);
if (resNode1 != null) {
System.out.printf("找到了,信息为 no=%d", resNode1.getNo());
} else {
System.out.printf("没有找到 no = %d 的节点", 5);
}
System.out.println("中序遍历方式查找~~~");
Node resNode2 = binaryTree.infixOrderSearch(5);
if (resNode2 != null) {
System.out.printf("找到了,信息为 no=%d", resNode2.getNo());
} else {
System.out.printf("没有找到 no = %d 的节点", 5);
}
System.out.println("后序遍历方式查找~~~");
Node resNode = binaryTree.postOrderSearch(5);
if (resNode != null) {
System.out.printf("找到了,信息为 no=%d", resNode.getNo());
} else {
System.out.printf("没有找到 no = %d 的节点", 5);
}
System.out.println("删除前,前序遍历");
binaryTree.preOrder(); // 1,2,3,5,4
binaryTree.delNode(5);
System.out.println("删除后,前序遍历");
binaryTree.preOrder(); // 1,2,3,4
}
}
三、线索化二叉树
1. 线索化二叉树基本介绍
(1)n个结点的二叉链表中含有n+1个空指针域。利用二叉链表中的空指针域,存放指向该结点在某种遍历次序下的前驱和后继结点的指针,这种附加的指针称为"线索"。这种加上了"线索"的二叉链表称为线索链表,相应的二叉树称为线索二叉树。
(2)线索二叉树可分为前序线索二叉树、中序线索二叉树和后序线索二叉树三种。
(3)因为线索化后,各个结点指向有变化,因此原来的遍历方式不能使用,这时需要使用新的方式遍历线索化二叉树,各个节点可以通过线型方式遍历,因此无需使用递归方式,这样也提高了遍历的效率。
2. 实现线索化二叉树
(1)创建节点类
/**
* 创建Node 结点
*/
class Node {
private int no;
private Node left; // 默认null
private Node right; // 默认null
// 如果leftType == 0表示指向的是左子树,如果1表示指向前驱结点
// 如果rightType == 0表示指向是右子树,如果1表示指向后继结点
private int leftType;
private int rightType;
public int getLeftType() {
return leftType;
}
public void setLeftType(int leftType) {
this.leftType = leftType;
}
public int getRightType() {
return rightType;
}
public void setRightType(int rightType) {
this.rightType = rightType;
}
public Node(int no) {
this.no = no;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
@Override
public String toString() {
return "HeroNode [no=" + no + "]";
}
}
(2)实现线索二叉树功能
/**
* 实现了中序线索化功能的二叉树
*/
class ThreadedBinaryTree {
private Node root;
// 在递归进行中序线索化时,pre总是保留前一个结点
private Node pre = null;
public ThreadedBinaryTree(Node root) {
this.root = root;
}
// 中序遍历线索化二叉树的方法
public void threadedList() {
// 定义一个变量,存储当前遍历的结点,从root开始
Node node = root;
while (node != null) {
while (node.getLeftType() == 0) {
node = node.getLeft();
}
// 打印当前这个结点
System.out.println(node);
// 如果当前结点的右指针指向的是后继结点,就一直输出
while (node.getRightType() == 1) {
// 获取到当前结点的后继结点
node = node.getRight();
System.out.println(node);
}
// 替换这个遍历的结点
node = node.getRight();
}
}
// 重载threadedNodes方法
public void threadedNodes() {
this.threadedNodes(root);
}
// 编写对二叉树进行中序线索化的方法
public void threadedNodes(Node node) {
// 如果node==null, 不能线索化
if (node == null) {
return;
}
// (一)线索化左子树
threadedNodes(node.getLeft());
// (二)线索化当前结点
// 处理当前结点的前驱结点
if (node.getLeft() == null) {
// 让当前结点的左指针指向前驱结点
node.setLeft(pre);
// 修改当前结点的左指针的类型
node.setLeftType(1);
}
// 处理后继结点
if (pre != null && pre.getRight() == null) {
// 让前驱结点的右指针指向当前结点
pre.setRight(node);
// 修改前驱结点的右指针类型
pre.setRightType(1);
}
// 每处理一个结点后,让当前结点是下一个结点的前驱结点
pre = node;
// (三)线索化右子树
threadedNodes(node.getRight());
}
}
(3)测试线索二叉树
public class ThreadedBinaryTreeDemo {
public static void main(String[] args) {
Node root = new Node(1);
Node node2 = new Node(3);
Node node3 = new Node(6);
Node node4 = new Node(8);
Node node5 = new Node(10);
Node node6 = new Node(14);
root.setLeft(node2);
root.setRight(node3);
node2.setLeft(node4);
node2.setRight(node5);
node3.setLeft(node6);
// 测试中序线索化
ThreadedBinaryTree threadedBinaryTree = new ThreadedBinaryTree(root);
threadedBinaryTree.threadedNodes();
// 以10号节点测试
Node leftNode = node5.getLeft();
Node rightNode = node5.getRight();
System.out.println("10号结点的前驱结点是 =" + leftNode); // 3
System.out.println("10号结点的后继结点是 =" + rightNode); // 1
// 当线索化二叉树后,能在使用原来的遍历方法
System.out.println("使用线索化的方式遍历线索化二叉树");
threadedBinaryTree.threadedList(); // 8, 3, 10, 1, 14, 6
}
}