为什么需要树
二叉树介绍
二叉树遍历节点思路分析及代码实现
代码实现:
package 树;
public class BinaryTreeDemo {
public static void main(String[] args) {
//测试
//创建节点
HeroNode root = new HeroNode(1,"宋江"); //宋江作为根节点传入树中建树
HeroNode n2 = new HeroNode(2,"吴用");
HeroNode n3 = new HeroNode(3,"卢俊义");
HeroNode n4 = new HeroNode(4,"林冲");
//说明,我们先手动创建二叉树,后面可用递归方式创建二叉树
BinaryTree tree = new BinaryTree(root); //宋江为根节点
root.left = n2; //按照博客上面的图,根节点的左子节点挂吴用
root.right = n3; //右子节点挂卢俊义
root.right.right = n4; //右子节点的右子节点挂林冲,等价于n3.right = n4
//测试前序遍历
System.out.println("前序遍历:"); //输出1,2,3,4
tree.preOrder();
//测试中序遍历
System.out.println("中序遍历:");
tree.infixOrder(); //输出2,1,3,4
//测试后序遍历
System.out.println("后序遍历:");
tree.postOrder(); //输出2,4,3,1
}
}
//定义BinaryTree 二叉树
class BinaryTree{
//使用一个根节点来创建二叉树
public HeroNode root; //如果要创建一个二叉树,那么我们需要一个根节点
//构造方法创建二叉树
public BinaryTree(HeroNode 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("二叉树为空,无法遍历");
}
}
//先创建HeroNode节点
class HeroNode{
public int no;
public String name;
public HeroNode left; //指向左子节点的指针
public HeroNode right; //指向右子节点的指针
public HeroNode(int no, String name) {
this.no = no;
this.name = name;
}
//打印节点信息
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
//编写前序遍历的方法
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); //先输出父节点
}
}
再来做个题巩固一下:
应该遍历出来的顺序在如下测试代码中可以看到:
//3号节点的左子节点挂一个关胜
HeroNode n5 = new HeroNode(5,"关胜");
n3.left = n5;
//测试前序遍历
System.out.println("前序遍历:"); //输出1,2,3,5,4
tree.preOrder();
//测试中序遍历
System.out.println("中序遍历:");
tree.infixOrder(); //输出2,1,5,3,4
//测试后序遍历
System.out.println("后序遍历:");
tree.postOrder(); //输出2,5,4,3,1
二叉树查找节点思路及代码实现
package 树;
public class BinaryTreeDemo {
public static void main(String[] args) {
//测试
//创建节点
HeroNode root = new HeroNode(1,"宋江"); //宋江作为根节点传入树中建树
HeroNode n2 = new HeroNode(2,"吴用");
HeroNode n3 = new HeroNode(3,"卢俊义");
HeroNode n4 = new HeroNode(4,"林冲");
//说明,我们先手动创建二叉树,后面可用递归方式创建二叉树
BinaryTree tree = new BinaryTree(root); //宋江为根节点
root.left = n2; //按照博客上面的图,根节点的左子节点挂吴用
root.right = n3; //右子节点挂卢俊义
root.right.right = n4; //右子节点的右子节点挂林冲,等价于n3.right = n4
//3号节点的左子节点挂一个关胜
HeroNode n5 = new HeroNode(5,"关胜");
n3.left = n5;
//测试前序遍历
System.out.println("前序遍历:"); //输出1,2,3,5,4
tree.preOrder();
//测试中序遍历
System.out.println("中序遍历:");
tree.infixOrder(); //输出2,1,5,3,4
//测试后序遍历
System.out.println("后序遍历:");
tree.postOrder(); //输出2,5,4,3,1
//测试查找,查找关胜
System.out.println("前序查找:");
tree.preOrderSearch(n5);
System.out.println("中序查找:");
tree.infixOrderSearch(n5);
System.out.println("后序查找:");
tree.postOrderSearch(n5);
}
}
//定义BinaryTree 二叉树
class BinaryTree{
//使用一个根节点来创建二叉树
public HeroNode root; //如果要创建一个二叉树,那么我们需要一个根节点
//构造方法创建二叉树
public BinaryTree(HeroNode root) {
this.root = root;
}
//前序遍历
public void preOrder(){
if(root != null) root.preOrder();
else System.out.println("二叉树为空,无法遍历");
}
//前序查找
public void preOrderSearch(HeroNode n){
if(root != null) {
HeroNode node = root.preOrderSearch(n.no);
System.out.println("查找到的英雄是:"+node);
}
else System.out.println("二叉树为空,无法遍历");
}
//中序遍历
public void infixOrder(){
if(root != null) root.infixOrder();
else System.out.println("二叉树为空,无法遍历");
}
//中序查找
public void infixOrderSearch(HeroNode n){
if(root != null) {
HeroNode node = root.infixOrderSearch(n.no);
System.out.println("查找到的英雄是:"+node);
}
else System.out.println("二叉树为空,无法遍历");
}
//后序遍历
public void postOrder(){
if(root != null) root.postOrder();
else System.out.println("二叉树为空,无法遍历");
}
//后序查找
public void postOrderSearch(HeroNode n){
if(root != null) {
HeroNode node = root.postOrderSearch(n.no);
System.out.println("查找到的英雄是:"+node);
}
else System.out.println("二叉树为空,无法遍历");
}
}
//先创建HeroNode节点
class HeroNode{
public int no;
public String name;
public HeroNode left; //指向左子节点的指针
public HeroNode right; //指向右子节点的指针
public HeroNode(int no, String name) {
this.no = no;
this.name = name;
}
//打印节点信息
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
//编写前序遍历的方法
public void preOrder(){
System.out.println(this); //先输出父节点
if(this.left != null) this.left.preOrder(); //递归向左子树前序遍历
if(this.right != null) this.right.preOrder(); //递归向右子树前序遍历
}
//编写前序查找的方法
/**
*
* @param no 按编号查找
* @return 找到则返回节点,没找到返回null
*/
public HeroNode preOrderSearch(int no){
if(this.no == no) return this; //先输出父节点
//1、判断当前节点的左子节点是否为空,不为空则递归前序查找
//2、如果左递归前序查找到了则返回
HeroNode 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);
if(resNode != null) return resNode; //说明在右子树中找到了
return resNode;
}
//编写中序遍历的方法
public void infixOrder(){
if(this.left != null) this.left.infixOrder(); //递归向左子树中序遍历
System.out.println(this); //先输出父节点
if(this.right != null) this.right.infixOrder(); //递归向右子树中序遍历
}
//编写中序查找的方法
/**
*
* @param no 按编号查找
* @return 找到则返回节点,没找到返回null
*/
public HeroNode infixOrderSearch(int no){
//1、判断当前节点的左子节点是否为空,不为空则递归前序查找
//2、如果左递归前序查找到了则返回
HeroNode resNode = null;
if(this.left != null) resNode = this.left.infixOrderSearch(no);
if(resNode != null) return resNode; //说明在左子树中找到了
if(this.no == no) return this; //先输出父节点
//1、判断当前节点的右子节点是否为空,不为空则递归前序查找
//2、如果右递归前序查找,找到则返回
if(this.right != null) resNode = this.right.infixOrderSearch(no);
return resNode;
}
//编写后序遍历的方法
public void postOrder(){
if(this.left != null) this.left.postOrder(); //递归向左子树后序遍历
if(this.right != null) this.right.postOrder(); //递归向右子树后序遍历
System.out.println(this); //先输出父节点
}
//编写后序查找的方法
/**
*
* @param no 按编号查找
* @return 找到则返回节点,没找到返回null
*/
public HeroNode postOrderSearch(int no){
//1、判断当前节点的左子节点是否为空,不为空则递归前序查找
//2、如果左递归前序查找到了则返回
HeroNode resNode = null;
if(this.left != null) resNode = this.left.postOrderSearch(no);
if(resNode != null) return resNode; //说明在左子树中找到了
//1、判断当前节点的右子节点是否为空,不为空则递归前序查找
//2、如果右递归前序查找,找到则返回
if(this.right != null) resNode = this.right.postOrderSearch(no);
if(resNode != null) return resNode; //说明在右子树中找到了
if(this.no == no) return this; //先输出父节点
return resNode;
}
}
二叉树删除节点思路及代码实现
删除节点究竟怎么删除,我们先做一个删除节点的规范,如下:
先实现简单的,后面会实现比较难的删除算法。
思路:
代码实现:
package 树;
public class BinaryTreeDemo {
public static void main(String[] args) {
//测试
//创建节点
HeroNode root = new HeroNode(1,"宋江"); //宋江作为根节点传入树中建树
HeroNode n2 = new HeroNode(2,"吴用");
HeroNode n3 = new HeroNode(3,"卢俊义");
HeroNode n4 = new HeroNode(4,"林冲");
//说明,我们先手动创建二叉树,后面可用递归方式创建二叉树
BinaryTree tree = new BinaryTree(root); //宋江为根节点
root.left = n2; //按照博客上面的图,根节点的左子节点挂吴用
root.right = n3; //右子节点挂卢俊义
root.right.right = n4; //右子节点的右子节点挂林冲,等价于n3.right = n4
//3号节点的左子节点挂一个关胜
HeroNode n5 = new HeroNode(5,"关胜");
n3.left = n5;
//先用前序遍历看看
tree.preOrder(); //输出1,2,3,5,4
//现在我们删除5号节点关胜
tree.delNode(n5.no);
System.out.println("--------------------删除后-------------------");
//再用前序遍历看看是否被删除
tree.preOrder(); //输出1,2,3,4
}
}
//定义BinaryTree 二叉树
class BinaryTree{
//使用一个根节点来创建二叉树
public HeroNode root; //如果要创建一个二叉树,那么我们需要一个根节点
//构造方法创建二叉树
public BinaryTree(HeroNode root) {
this.root = root;
}
//删除节点
public void delNode(int no){
if(root != null){
//一定要先判断我们的root是不是就是要删除的节点
if(root.no == no) root = null;
else root.delNode(no); //递归删除
}else{
System.out.println("空树,无法删除");
}
}
//前序遍历
public void preOrder(){
if(root != null) root.preOrder();
else System.out.println("二叉树为空,无法遍历");
}
//前序查找
public void preOrderSearch(HeroNode n){
if(root != null) {
HeroNode node = root.preOrderSearch(n.no);
System.out.println("查找到的英雄是:"+node);
}
else System.out.println("二叉树为空,无法遍历");
}
//中序遍历
public void infixOrder(){
if(root != null) root.infixOrder();
else System.out.println("二叉树为空,无法遍历");
}
//中序查找
public void infixOrderSearch(HeroNode n){
if(root != null) {
HeroNode node = root.infixOrderSearch(n.no);
System.out.println("查找到的英雄是:"+node);
}
else System.out.println("二叉树为空,无法遍历");
}
//后序遍历
public void postOrder(){
if(root != null) root.postOrder();
else System.out.println("二叉树为空,无法遍历");
}
//后序查找
public void postOrderSearch(HeroNode n){
if(root != null) {
HeroNode node = root.postOrderSearch(n.no);
System.out.println("查找到的英雄是:"+node);
}
else System.out.println("二叉树为空,无法遍历");
}
}
//先创建HeroNode节点
class HeroNode{
public int no;
public String name;
public HeroNode left; //指向左子节点的指针
public HeroNode right; //指向右子节点的指针
public HeroNode(int no, String name) {
this.no = no;
this.name = name;
}
//打印节点信息
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
//递归删除节点
//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(); //递归向右子树前序遍历
}
//编写前序查找的方法
/**
*
* @param no 按编号查找
* @return 找到则返回节点,没找到返回null
*/
public HeroNode preOrderSearch(int no){
if(this.no == no) return this; //先输出父节点
//1、判断当前节点的左子节点是否为空,不为空则递归前序查找
//2、如果左递归前序查找到了则返回
HeroNode 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);
if(resNode != null) return resNode; //说明在右子树中找到了
return resNode;
}
//编写中序遍历的方法
public void infixOrder(){
if(this.left != null) this.left.infixOrder(); //递归向左子树中序遍历
System.out.println(this); //先输出父节点
if(this.right != null) this.right.infixOrder(); //递归向右子树中序遍历
}
//编写中序查找的方法
/**
*
* @param no 按编号查找
* @return 找到则返回节点,没找到返回null
*/
public HeroNode infixOrderSearch(int no){
//1、判断当前节点的左子节点是否为空,不为空则递归前序查找
//2、如果左递归前序查找到了则返回
HeroNode resNode = null;
if(this.left != null) resNode = this.left.infixOrderSearch(no);
if(resNode != null) return resNode; //说明在左子树中找到了
if(this.no == no) return this; //先输出父节点
//1、判断当前节点的右子节点是否为空,不为空则递归前序查找
//2、如果右递归前序查找,找到则返回
if(this.right != null) resNode = this.right.infixOrderSearch(no);
return resNode;
}
//编写后序遍历的方法
public void postOrder(){
if(this.left != null) this.left.postOrder(); //递归向左子树后序遍历
if(this.right != null) this.right.postOrder(); //递归向右子树后序遍历
System.out.println(this); //先输出父节点
}
//编写后序查找的方法
/**
*
* @param no 按编号查找
* @return 找到则返回节点,没找到返回null
*/
public HeroNode postOrderSearch(int no){
//1、判断当前节点的左子节点是否为空,不为空则递归前序查找
//2、如果左递归前序查找到了则返回
HeroNode resNode = null;
if(this.left != null) resNode = this.left.postOrderSearch(no);
if(resNode != null) return resNode; //说明在左子树中找到了
//1、判断当前节点的右子节点是否为空,不为空则递归前序查找
//2、如果右递归前序查找,找到则返回
if(this.right != null) resNode = this.right.postOrderSearch(no);
if(resNode != null) return resNode; //说明在右子树中找到了
if(this.no == no) return this; //先输出父节点
return resNode;
}
}
顺序存储二叉树思路及实现
代码实现上述需求:
package 树;
//顺序存储二叉树
public class ArrBinaryTreeDemo {
public static void main(String[] args) {
int arr[] = {1,2,3,4,5,6,7};
ArrBinaryTree binaryTree = new ArrBinaryTree(arr);
//测试前序
System.out.println("前序遍历:");
binaryTree.preOreder(0); //1,2,4,5,3,6,7
//测试中序
System.out.println("中序遍历:");
binaryTree.infixOreder(0); //4,2,5,1,6,3,7
//测试后序
System.out.println("后序遍历:");
binaryTree.postOreder(0); //4,5,2,6,7,3,1
}
}
//编写一个ArrBinaryTree,实现顺序存储二叉树遍历
class ArrBinaryTree{
private int[] arr; //存储数据节点的数组
public ArrBinaryTree(int[] arr){
this.arr = arr;
}
//编写一个方法,完成顺序存储二叉树的前序遍历
//index即思路图中的n值,指的是元素的下标
public void preOreder(int index){
//如果数组为空,或者arr.length == 0
if(arr == null || arr.length == 0){
System.out.println("数组为空,无法遍历");
}
//输出当前元素,即前序遍历顺序:根、左、右
System.out.println(arr[index]);
//数组下标不越界时,递归前序遍历
//递归向左
if((index*2+1) < arr.length) preOreder(index*2+1);
//递归向右
if((index*2+2) < arr.length) preOreder(index*2+2);
}
//中序遍历
public void infixOreder(int index){
//如果数组为空,或者arr.length == 0
if(arr == null || arr.length == 0){
System.out.println("数组为空,无法遍历");
}
//数组下标不越界时,递归中序遍历
//递归向左
if((index*2+1) < arr.length) infixOreder(index*2+1);
System.out.println(arr[index]);
//递归向右
if((index*2+2) < arr.length) infixOreder(index*2+2);
}
//后序遍历
public void postOreder(int index){
//如果数组为空,或者arr.length == 0
if(arr == null || arr.length == 0){
System.out.println("数组为空,无法遍历");
}
//数组下标不越界时,递归后序遍历
//递归向左
if((index*2+1) < arr.length) postOreder(index*2+1);
//递归向右
if((index*2+2) < arr.length) postOreder(index*2+2);
System.out.println(arr[index]);
}
}
线索化二叉树
问题引入:
基本介绍:
线索化二叉树的意思是将一个数列构建成二叉树之后,按照原来数列中的前驱和后继顺序将每个叶子节点的左指针和右指针充分利用起来让其指向前驱节点和后继节点的一种二叉树形式。
实现思路:
代码实现上述案例:
package 树.线索化二叉树;
//线索化二叉树
public class ThreadedBinaryTreeDemo {
public static void main(String[] args) {
//测试
HeroNode root = new HeroNode(1, "tom");
HeroNode n2 = new HeroNode(3, "jack");
HeroNode n3 = new HeroNode(6, "smith");
HeroNode n4 = new HeroNode(8, "marry");
HeroNode n5 = new HeroNode(10, "king");
HeroNode n6 = new HeroNode(14, "dim");
root.setRight(n3);
root.setLeft(n2);
n2.setRight(n5);
n2.setLeft(n4);
n3.setLeft(n6);
//测试线索化
threadedBinaryTree threadedBinaryTree = new threadedBinaryTree();
threadedBinaryTree.setRoot(root);
threadedBinaryTree.threadedNodes();
//以10号节点为例
HeroNode leftNode = n5.getLeft();
HeroNode rightNode = n5.getRight();
System.out.println("10号节点的前驱节点是 = "+leftNode);
System.out.println("10号节点的后继节点是 = "+rightNode);
}
}
//创建ThreadedBinaryTree
//实现线索化功能
class threadedBinaryTree {
private HeroNode root; //父节点
//为了实现线索化,需要创建一个指向当前节点的前驱节点的指针
//在递归进行线索化时,pre总是保留前一个节点
private HeroNode pre = null;
//只需要设置父节点就好了,因为后面有遍历操作,所以不需要get方法
//你要创建一个二叉树,那必然要有节点,所以是HeroNode类型
public void setRoot(HeroNode root) {
this.root = root; //因为重名,所以加this表示该root是当前二叉树类的对象的属性
}
//重载一下threadedNodes
public void threadedNodes(){
this.threadedNodes(root);
}
//编写对二叉树进行中序线索化的方法
public void threadedNodes(HeroNode node){
//如果node==null,不能线索化
if(node == null) return;
//先线索化左子树
threadedNodes(node.getLeft());
//线索化当前节点(有难度)
//处理当前节点的前驱节点
//以8节点来理解
//8节点的.left==null,8节点的.leftType = 1
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());
}
//删除节点
public void delNode(int no){
if(root != null){
if(root.getNo() == no) root = null;
else root.delNode(no);
}else{
System.out.println("空树,无法删除");
}
}
//前序遍历
public void preOrder(){//这里的preOrder方法是BinaryTree类里的,下面的调用的HeroNode类里的,没有关系
if(this.root != null){ //这里的this是只当前对象:当前的BinaryTree对象
this.root.preOrder(); //这里的root是HeroNode类型的,所以可调用HeroNode类里的方法
}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 HeroNode preOrderSearch(int no){
//如果根节点不为空,则二叉树存在然后进行递归前序查找
if(this.root != null) return this.root.preOrdersearch(no);
else return null;
}
//中序查找
public HeroNode infixOrderSearch(int no){
if(this.root != null) return this.root.infixOrderSearch(no);
else return null;
}
//后序查找
public HeroNode postOrderSearch(int no){
if(this.root != null) return root.postOrderSearch(no);
else return null;
}
}
//创建HeroNode,因为处于同一个包下,所以继承一下进行修改就行
class HeroNode{
private int no;
private String name;
private HeroNode left; //默认为null,因为并不清楚后续有无子节点
private HeroNode right; //默认为null,因为并不清楚后续有无子节点
//说明
//1.如果leftType == 0表示指向的是左子树,为1则表示指向前驱节点
//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 HeroNode(int no,String name){
this.no = no;
this.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 HeroNode getLeft() {
return left;
}
public void setLeft(HeroNode left) {
this.left = left;
}
public HeroNode getRight() {
return right;
}
public void setRight(HeroNode right) {
this.right = right;
}
//为了方便遍历,重写toString
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
'}';
//不重写left和right是因为递归输出的时候会重复出现相同节点的信息
}
//递归删除节点
//1.如果删除的节点是叶子节点,则删除该节点
//2.如果删除的节点是非叶子节点,则删除该子树
public void delNode(int no) { //no是要删除的英雄编号
//如果当前节点的左子节点不为空,并且左子节点就是要删除节点,就将this.left=null,并且返回(结束递归并删除)
if(this.left != null && this.left.no == no){
this.left = null;
return;
}
//如果当前节点的左子节点不为空,并且左子节点就是要删除节点,就将this.left=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);
}
//前序遍历查找:找到返回该HeroNode,没找到就返回null
public HeroNode preOrdersearch(int no){ //查找序号为no的节点
System.out.println("进入前序遍历"); //观察查找次数
//比较当前节点是不是该no
if(this.no == no) return this; //返回当前对象
//1.判断当前节点的左子节点是否为空,如果不为空,则递归前序查找
//2.如果左递归前序查找,找到节点,则返回
HeroNode resNode = null; //接收结果的一个Node
if(this.left != null) resNode = this.left.preOrdersearch(no);
if(resNode != null) return resNode; //resNode不为空,说明找到了
//1.右递归前序查找,找到节点,则返回,否则继续判断
//2.当前的节点的右子节点是否为空,如果不空,则继续向右递归前序查找
if(this.right != null) resNode = this.right.preOrdersearch(no);
return resNode;
}
//中序查找
public HeroNode infixOrderSearch(int no){
//判断当前节点的左子节点是否为空,如果不为空,则递归前序查找
HeroNode resNode = null;
if(this.left != null) resNode = this.left.infixOrderSearch(no);
//resNode不为空,说明找到了,返回节点结束方法
if(resNode != null) return resNode;
//还没找到,则与当前节点比较,如果找到,返回当前节点
System.out.println("进入中序查找"); //计算查找次数用的,不重要
if(this.no == no) return this;
//当前节点也不是,则右递归进行查找
if(this.right != null) resNode = this.right.infixOrderSearch(no);
//右递归中不管找不找到都要返回resNode
return resNode;
}
//后序查找
public HeroNode postOrderSearch(int no){
HeroNode 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;
else return resNode; //result最初为空嘛,和null差不多的
}
}
遍历线索化二叉树
关键代码实现,也是使用上面的案例:
算法实现(这个方法应该放在树的类中):
//遍历线索化二叉树的方法
public void threadedList(){
//定义一个变量,存储当前遍历的节点,从root开始
HeroNode node = root;
while (node != null){
//循环的找到leftType == 1的节点,第一个找到就是8节点
//后面随着遍历而变化,因为当leftType==1时,说明该节点是按照线索化
//处理后的有效节点
while (node.getLeftType() == 0){
node = node.getLeft();
}
//打印当前这个节点
System.out.println(node);
//如果当前节点的右指针指向的是后继节点,就一直输出
while (node.getRightType() == 1){
//获取到当前节点的后继节点
node = node.getRight();
}
//替换这个遍历的节点
node = node.getRight();
}
}