二叉树

1.每个节点最多只能有两个子节点(可以只有一个节点)
2.左节点和右节点
3.如果该二叉树的所有子节点都在最后一层,并且结点总数=2^n-1,n为层数,则我们称为满二叉树。
4.如果该二叉树的所有叶子节点都在最后一层或者倒数第二层,而且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续,我们称为完全二叉树在这里插入图片描述
在这里插入图片描述

二叉树demo

package algorithm.tree;

public class BinaryTreeDemo {
    public static void main(String[] args) {
        //create a binary tree
        BinaryTree binaryTree = new BinaryTree();
        //创建节点
        HeroNode root = new HeroNode(1, "王二");
        HeroNode one = new HeroNode(2, "张三");
        HeroNode two = new HeroNode(3, "李四");
        HeroNode three = new HeroNode(4, "王五");

        //手动创建二叉树,以后使用递归创建
        root.setLeft(one);
        root.setRight(two);
        two.setRight(three);
        binaryTree.setRoot(root);
        //测试
        System.out.println("前序遍历"); //1 2 3 4
        binaryTree.preOrder();


        System.out.println("中序遍历");// 2 1 3 4
        binaryTree.infixOrder();


        System.out.println("后续遍历");// 2 4 3 1
        binaryTree.backOrder();


        //前序查找
        System.out.println("前序查找");
        System.out.println( binaryTree.preOrderSearch(3));

        System.out.println("中序查找");
        System.out.println(binaryTree.infixOrderSearch(3));

        System.out.println("后序查找");
        System.out.println(binaryTree.postOrderSearch(3));
    }
}

//定义BinaryTree
class BinaryTree {
    private HeroNode root;

    public void setRoot(HeroNode root) {
        this.root = root;
    }

    //前序查找
    public HeroNode preOrderSearch(int no){
        if(root !=null){
           return  root.preOrderSearch(no);
        }else{
            return null;
        }
    }
    //中序查找
    public HeroNode infixOrderSearch(int no){
        if(root !=null){
            return root.infixOrderSearch(no);
        }else {
            return null;
        }
    }
    //后序查找
    public HeroNode postOrderSearch(int no ){
        if(root!=null){
            return root.postOrderSearch(no);
        }else{
            return null;
        }
    }

    //前序遍历
    public void preOrder() {
        if (this.root != null) {
            this.root.preOrder();
        } else {
            System.out.println("当前二叉树为空,无法遍历");
        }
    }

    //中序遍历
    public void infixOrder() {
        if (this.root != null) {
            this.root.midOrder();
        } else {
            System.out.println("当前二叉树为空,无法遍历");
        }
    }

    //后序遍历
    public void backOrder() {
        if (this.root != null) {
            this.root.backOrder();
        } else {
            System.out.println("当前二叉树为空,无法遍历");
        }
    }
}


//创建HeroNode 结点
class HeroNode {
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    //前序遍历查找

    /**
     * @param no 要查找结点的编号
     * @return
     */
    public HeroNode preOrderSearch(int no) {
        System.out.println("进入前序遍历");
        //比较当前节点是不是
        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);
        }
        return resNode;
    }
    //中序遍历查找
    public HeroNode infixOrderSearch(int no){
        //判断当前节点的左子节点是否为空,如果不为空,则递归中序查找
        HeroNode 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 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;
        }
        return resNode;
    }


    //前序遍历
    public void preOrder() {
        System.out.println(this);//输出父节点
        //递归向左子树前序
        if (this.left != null) {
            this.left.preOrder();
        }
        //递归向右子树前序遍历
        if (this.right != null) {
            this.right.preOrder();
        }

    }

    //中序遍历
    public void midOrder() {
        //左子树中序遍历recursion
        if (this.left != null) {
            this.left.midOrder();
        }
        System.out.println(this);//输出
        //右子树中序遍历recursion
        if (this.right != null) {
            this.right.midOrder();
        }
    }

    //后序遍历
    public void backOrder() {
        //左子树后序遍历recursion
        if (this.left != null) {
            this.left.backOrder();
        }
        //右子树后序遍历recursion
        if (this.right != null) {
            this.right.backOrder();
        }
        System.out.println(this);
    }


    @Override
    public String toString() {
        return "HeroNode{" +
                "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 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;
    }
}

删除

package algorithm.tree;

public class BinaryTreeDemo {
    public static void main(String[] args) {
        //create a binary tree
        BinaryTree binaryTree = new BinaryTree();
        //创建节点
        HeroNode root = new HeroNode(1, "王二");
        HeroNode one = new HeroNode(2, "张三");
        HeroNode two = new HeroNode(3, "李四");
        HeroNode three = new HeroNode(4, "王五");
        HeroNode four = new HeroNode(5, "赵六");
        //手动创建二叉树,以后使用递归创建
        root.setLeft(one);
        root.setRight(two);
        two.setRight(three);
        three.setLeft(four);
        binaryTree.setRoot(root);
        
        System.out.println("删除前,前序遍历");
        binaryTree.preOrder();
        binaryTree.delNode(3);
        System.out.println("删除后,前序遍历");
        binaryTree.preOrder();
    }
}

//定义BinaryTree
class BinaryTree {
    private HeroNode root;

    public void setRoot(HeroNode 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 HeroNode preOrderSearch(int no){
        if(root !=null){
           return  root.preOrderSearch(no);
        }else{
            return null;
        }
    }
    //中序查找
    public HeroNode infixOrderSearch(int no){
        if(root !=null){
            return root.infixOrderSearch(no);
        }else {
            return null;
        }
    }
    //后序查找
    public HeroNode postOrderSearch(int no ){
        if(root!=null){
            return root.postOrderSearch(no);
        }else{
            return null;
        }
    }

    //前序遍历
    public void preOrder() {
        if (this.root != null) {
            this.root.preOrder();
        } else {
            System.out.println("当前二叉树为空,无法遍历");
        }
    }

    //中序遍历
    public void infixOrder() {
        if (this.root != null) {
            this.root.midOrder();
        } else {
            System.out.println("当前二叉树为空,无法遍历");
        }
    }

    //后序遍历
    public void backOrder() {
        if (this.root != null) {
            this.root.backOrder();
        } else {
            System.out.println("当前二叉树为空,无法遍历");
        }
    }
}


//创建HeroNode 结点
class HeroNode {
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }
    /**
     *

     1.如果删除的节点是叶子节点,则删除该节点

     2.如果删除的节点是非叶子节点,则删除该子树



     首先要处理:

     1.是否空树

     再进行以下操作

     思路:

     1.如果当前节点的左子节点不为空,且左子节点为要删除的节点,则将this.left置空 并返回

     2.如果当前节点的右子节点不为空,且右子节点为要删除的节点,则将this.right置空 并返回

     3.如果 1,2步骤没有删除节点,就要递归向左/右子树进行递归删除

     4.考虑到如果树是空树root,如果只有一个root结点,则等价将二叉树置空


     */
    public void delNode(int no ){
        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);
        }
    }



    //前序遍历查找
    /**
     * @param no 要查找结点的编号
     * @return
     */
    public HeroNode preOrderSearch(int no) {
        System.out.println("进入前序遍历");
        //比较当前节点是不是
        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);
        }
        return resNode;
    }
    //中序遍历查找
    public HeroNode infixOrderSearch(int no){
        //判断当前节点的左子节点是否为空,如果不为空,则递归中序查找
        HeroNode 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 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;
        }
        return resNode;
    }


    //前序遍历
    public void preOrder() {
        System.out.println(this);//输出父节点
        //递归向左子树前序
        if (this.left != null) {
            this.left.preOrder();
        }
        //递归向右子树前序遍历
        if (this.right != null) {
            this.right.preOrder();
        }

    }

    //中序遍历
    public void midOrder() {
        //左子树中序遍历recursion
        if (this.left != null) {
            this.left.midOrder();
        }
        System.out.println(this);//输出
        //右子树中序遍历recursion
        if (this.right != null) {
            this.right.midOrder();
        }
    }

    //后序遍历
    public void backOrder() {
        //左子树后序遍历recursion
        if (this.left != null) {
            this.left.backOrder();
        }
        //右子树后序遍历recursion
        if (this.right != null) {
            this.right.backOrder();
        }
        System.out.println(this);
    }


    @Override
    public String toString() {
        return "HeroNode{" +
                "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 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;
    }
}

顺序二叉树

顺序存储二叉树

1.顺序二叉树通常只考虑完全二叉树

2.第n个元素的左子节点为2*n +1

3.第n个元素的右子节点为2*n+2

4.第n个元素的父节点为(n-1)/2

  1. n:表示二叉树中的第几个元素(按0开始编号)

public class ArrayBinaryTreeDemo {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7,};

        ArrayBinaryTree arrayBinaryTree = new ArrayBinaryTree(arr);
        System.out.println("顺序二叉树前序遍历");
        arrayBinaryTree.preOrder();
        System.out.println("顺序二叉树中序遍历");
        arrayBinaryTree.infixOrder();
        System.out.println("顺序二叉树后序遍历");
        arrayBinaryTree.postOrder();
    }
}

//编写一个ArrayBinaryTree,实现顺序存储二叉树
class ArrayBinaryTree {
    private int[] arr;//存储数据结点的数组

    public ArrayBinaryTree(int[] arr) {
        this.arr = arr;
    }

    //编写一个方法,完成顺序存储二叉树的前序遍历
    public void preOrder(){
       this.preOrder(0);
    }
    /**
     * @param index 数组下标
     */
    public void preOrder(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){
            preOrder(2 * index + 1);
        }
        //向右递归
        if((index*2+2)<arr.length){
            preOrder(2*index+2);
        }
    }

    public void infixOrder(){
        this.infixOrder(0);
    }
    public void infixOrder(int index){
        if(arr==null || arr.length<=0){
            System.out.println("数组为空,不能遍历");
        }else {
            //左递归
            if((index*2+1)<arr.length){
                infixOrder(index*2+1);
            }
            System.out.println(arr[index]);
            //有递归
            if((index*2+2)<arr.length){
                infixOrder(index*2+2);
            }
        }
    }
    public void postOrder(){
        this.postOrder(0);
    }
    public void postOrder(int index){
        if(arr==null || arr.length<=0){
            System.out.println("数组为空,不能遍历");
        }else {
            //左递归
            if((index*2+1)<arr.length){
                infixOrder(index*2+1);
            }
            //右递归
            if((index*2+2)<arr.length){
                infixOrder(index*2+2);
            }
            System.out.println(arr[index]);
        }
    }
}

线索化二叉树

线索化二叉树

基本介绍:

1.n个结点的二叉链表中含有n+1【公式 2n-(n-1)=n+1】个空指针域。利用二叉链表中的空指针域,存放指向该结点在某种次序下的前驱和后序结点的指针

2.这种加上了线索的二叉链表称为线索链表,相应的二叉树称为线索二叉树(Threaded BinaryTree)。根据线索性质的不同,线索二叉树科分为前序线索二叉树中序线索二叉树后序线索二叉树

notice:

1.left有可能指向左结点,也有可能指向前驱结点;

2.right有可能指向右结点,也有可能指向后继结点;

package algorithm.tree.binaryTree;

import java.util.HashMap;

public class ThreadedBinaryTreeDemo {
    public static void main(String[] args) {
        //testing inifx threaded binarytree functionality
        HeroNode root = new HeroNode(1, "tom");
        HeroNode node2 = new HeroNode(3, "jack");
        HeroNode node3 = new HeroNode(6, "smith");
        HeroNode node4 = new HeroNode(8, "mery");
        HeroNode node5 = new HeroNode(10, "king");
        HeroNode node6 = new HeroNode(14, "dim");

        root.setLeft(node2);
        root.setRight(node3);
        node2.setLeft(node4);
        node2.setRight(node5);
        node3.setLeft(node6);

        //testing threaded infix binaryTree
        ThreadedBinaryTree threadedBinaryTree = new ThreadedBinaryTree(root);
        threadedBinaryTree.threadedNodes();

        //测试 以10号节点为例
        HeroNode left = node5.getLeft();
        HeroNode right = node5.getRight();
        System.out.println("previous node;"+left);
        System.out.println("next node:"+right);
    }
}

class  ThreadedBinaryTree{
    private HeroNode root;
    //为了实现线索化,需要创建一个指向当前节点的前驱节点的指针域
    //在递归进行线索化时,pre 总是保留前一个节点
    private HeroNode pre;


    public ThreadedBinaryTree(HeroNode root) {
        this.root = root;
    }
    //线索化

    public void threadedNodes(){
        this.threadedNodes(this.root);
    }
    /**
     *
     * @param node 当前需要线索化的结点
     */
    public void threadedNodes(HeroNode node){
        //如果node==null,不能线索化
        if(node==null){
            return;
        }

        //1.先线索化左子树
        threadedNodes(node.getLeft());
        //2.线索化当前结点
        //处理当前节点的前驱节点
        if(node.getLeft()==null){
            //让当前结点的左指针指向前驱结点
            node.setLeft(pre);
            //修改当前结点的左指针类型
            node.setLeftTyoe(1);
        }
        //处理后继节点
        if(pre!=null && pre.getRight()==null){
            //让前驱节点的右指针指向当前节点
            pre.setRight(node);
            //改变前驱节点的右指针类型
            pre.setRightType(1);
        }
        //每处理一个节点后,让当前节点是下一个节点的前驱节点
        pre=node;
        //3.线索化右子树
        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("empty tree cannot del");
        }
    }

    public HeroNode preOrderSearch(int no){
        if(root !=null){
            return root.preOrderSearch(no);
        }else{
            return null;
        }
    }
    public HeroNode infixOrderSearch(int no){
        if(root!=null){
            return root.infixOrderSearch(no);
        }else {
            return null;
        }
    }
    public HeroNode postOrderSearch(int no){
        if(root!=null){
            return root.postOrderSearch(no);
        }else{
            return null;
        }
    }
    public void preOrder(){
        if(this.root!=null){
            this.root.preOrder();
        }else{
            System.out.println("empty tree cannot traverse");
        }
    }
    public void infixOrder(){
        if(this.root!=null){
            this.root.infixOrder();
        }else{
            System.out.println("empty tree cannot traverse");
        }
    }
    public void postOrder(){
        if(this.root!=null){
            this.root.postOrder();
        }else{
            System.out.println("empty tree canno traverse");
        }
    }
}

class HeroNode{
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    //说明
    //1.如果leftType=0,表示指向左子树,若为1 指向前驱结点
    //2. rightType=0 ... 同上
    private int leftTyoe;
    private int rightType;

    public int getLeftTyoe() {
        return leftTyoe;
    }

    public void setLeftTyoe(int leftTyoe) {
        this.leftTyoe = leftTyoe;
    }

    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 void delNode(int no){
        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);
        }
    }
    public HeroNode preOrderSearch(int no){
        System.out.println("entrance of preOrderSearch");
        if(this.no == no){
            return this;
        }
        HeroNode resNode = null;
        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 HeroNode infixOrderSearch(int no ){
        HeroNode resNode = null;
        if(this.left!=null){
            resNode = this.left.infixOrderSearch(no);
        }
        if(resNode!=null){
            return resNode;
        }
        System.out.println("entrance of infixOrderSearch");
        if(this.no==no){
            return this;
        }
        if(this.right!=null){
            resNode = this.right.infixOrderSearch(no);
        }
        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("entrance of postOrderSearch");
        if(this.no==no){
            return this;
        }
        return resNode;
    }
    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);
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", left=" + left +
                ", right=" + right +
                ", leftTyoe=" + leftTyoe +
                ", rightType=" + rightType +
                '}';
    }

    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;
    }
}

中序遍历线索化二叉树
package algorithm.tree.binaryTree;

import java.util.HashMap;

public class ThreadedBinaryTreeDemo {
    public static void main(String[] args) {
        //testing inifx threaded binarytree functionality
        HeroNode root = new HeroNode(1, "tom");
        HeroNode node2 = new HeroNode(3, "jack");
        HeroNode node3 = new HeroNode(6, "smith");
        HeroNode node4 = new HeroNode(8, "mery");
        HeroNode node5 = new HeroNode(10, "king");
        HeroNode node6 = new HeroNode(14, "dim");

        root.setLeft(node2);
        root.setRight(node3);
        node2.setLeft(node4);
        node2.setRight(node5);
        node3.setLeft(node6);

        //testing threaded infix binaryTree
        ThreadedBinaryTree threadedBinaryTree = new ThreadedBinaryTree(root);
        threadedBinaryTree.threadedNodes();

        //测试 以10号节点为例
        HeroNode left = node5.getLeft();
        HeroNode right = node5.getRight();
        System.out.println("previous node;" + left);
        System.out.println("next node:" + right);

        //当线索化二叉树后,不能再使用原来的遍历方法
    }
}

class ThreadedBinaryTree {
    private HeroNode root;
    //为了实现线索化,需要创建一个指向当前节点的前驱节点的指针域
    //在递归进行线索化时,pre 总是保留前一个节点
    private HeroNode pre;


    public ThreadedBinaryTree(HeroNode root) {
        this.root = root;
    }
    //线索化

    public void threadedNodes() {
        this.threadedNodes(this.root);
    }

    //中序遍历线索化二叉树
    public void threadedList() {
        //定义一个变量,存储当前遍历的结点,从root开始
        HeroNode node = root;
        while (node != null) {
            //循环的找到leftType == 1 的结点, 第一个找到就是8结点
            //后面随着遍历而变化,因为当leftType==1时,
            //说明该结点是按照线索化处理后的有效结点
            while(node.getLeftTyoe()==0){
                node=node.getLeft();
            }

            //打印当前这个结点
            System.out.println(node);
            //如果当前结点的右指针指向的是后续结点,就一直输出
            while (node.getRightType() == 1) {
                //获取到当前结点的后续结点
                node = node.getRight();
                System.out.println(node);
            }
            //替换这个遍历的结点
            node=node.getRight();
        }

    }

    /**
     * @param node 当前需要线索化的结点
     */
    public void threadedNodes(HeroNode node) {
        //如果node==null,不能线索化
        if (node == null) {
            return;
        }

        //1.先线索化左子树
        threadedNodes(node.getLeft());
        //2.线索化当前结点
        //处理当前节点的前驱节点
        if (node.getLeft() == null) {
            //让当前结点的左指针指向前驱结点
            node.setLeft(pre);
            //修改当前结点的左指针类型
            node.setLeftTyoe(1);
        }
        //处理后继节点
        if (pre != null && pre.getRight() == null) {
            //让前驱节点的右指针指向当前节点
            pre.setRight(node);
            //改变前驱节点的右指针类型
            pre.setRightType(1);
        }
        //每处理一个节点后,让当前节点是下一个节点的前驱节点
        pre = node;
        //3.线索化右子树
        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("empty tree cannot del");
        }
    }

    public HeroNode preOrderSearch(int no) {
        if (root != null) {
            return root.preOrderSearch(no);
        } else {
            return null;
        }
    }

    public HeroNode infixOrderSearch(int no) {
        if (root != null) {
            return root.infixOrderSearch(no);
        } else {
            return null;
        }
    }

    public HeroNode postOrderSearch(int no) {
        if (root != null) {
            return root.postOrderSearch(no);
        } else {
            return null;
        }
    }

    public void preOrder() {
        if (this.root != null) {
            this.root.preOrder();
        } else {
            System.out.println("empty tree cannot traverse");
        }
    }

    public void infixOrder() {
        if (this.root != null) {
            this.root.infixOrder();
        } else {
            System.out.println("empty tree cannot traverse");
        }
    }

    public void postOrder() {
        if (this.root != null) {
            this.root.postOrder();
        } else {
            System.out.println("empty tree canno traverse");
        }
    }
}

class HeroNode {
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    //说明
    //1.如果leftType=0,表示指向左子树,若为1 指向前驱结点
    //2. rightType=0 ... 同上
    private int leftTyoe;
    private int rightType;

    public int getLeftTyoe() {
        return leftTyoe;
    }

    public void setLeftTyoe(int leftTyoe) {
        this.leftTyoe = leftTyoe;
    }

    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 void delNode(int no) {
        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);
        }
    }

    public HeroNode preOrderSearch(int no) {
        System.out.println("entrance of preOrderSearch");
        if (this.no == no) {
            return this;
        }
        HeroNode resNode = null;
        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 HeroNode infixOrderSearch(int no) {
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.infixOrderSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        System.out.println("entrance of infixOrderSearch");
        if (this.no == no) {
            return this;
        }
        if (this.right != null) {
            resNode = this.right.infixOrderSearch(no);
        }
        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("entrance of postOrderSearch");
        if (this.no == no) {
            return this;
        }
        return resNode;
    }

    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);
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", left=" + left +
                ", right=" + right +
                ", leftTyoe=" + leftTyoe +
                ", rightType=" + rightType +
                '}';
    }

    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;
    }
}

堆排序(二叉树的应用)

package algorithm.tree;

public class HeapSort {
    public static void main(String[] args) {
        int arr[] = {};

    }

    //method for head sort.
    public static void heapSort(int arr[]) {
        System.out.println("this method for heap sort");
        int temp = 0;
        //分步完成

//        adjust(arr,1,arr.length);
//        adjust(arr,0,arr.length);
        //将无序序列构造成一个堆,根据升序降序需求选择大顶堆或小顶堆
        for(int i = arr.length/2 -1;i>=0;i--){
            adjust(arr,i, arr.length);
        }

        /**
         *  1.将堆顶元素与末尾元素交换,将最大元素”沉“到数组末端
         *  2.重新调整结构,使其满足堆定义,然后继续交换堆顶元素与当前末尾元素;反复执行
         */
        for(int j = arr.length-1;j>0;j--){
            //exchange
            temp = arr[j];
            arr[j]=arr[0];
            arr[0] = temp;
            adjust(arr,0,j);
        }
    }

    //将一个数组(二叉树),调整成一个大顶堆
    /**
     * 功能:完成index为i的非叶子节点所在的树,调整为大顶堆
     *
     * @param arr    待调整的数组
     * @param i      表示非叶子节点的index
     * @param length 表示对多少个元素继续调整,length 是在逐渐的减少
     */
    public static void adjust(int[] arr, int i, int length) {

        int temp = arr[i];//先取出当前元素的值,保存在临时变量
        //开始调整
        //调整左子节点
        for (int k = i * 2 + 1; k < length; k = k * 2 + 1) {
            if(arr[k] < arr[k+1]){// 说明 left node < right node
                k++; //k point to right node.
            }
            if(arr[k] > temp){ // 如果子节点 > 父节点 交换
                arr[i] = arr[k]; // 把较大的值,赋给当前节点
                i = k;// i 指向k,继续循环比较
            }else{
                break;//
            }
        }
        //当 for 循环结束后,我们已经将 以 i 为父节点的树的最大值,放在了 最顶(局部)
        arr[i] = temp;//将tmep值放到调整后的位置
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值