完全二叉树代码复习用

package algorithom;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
/**
 * 定义二叉树节点元素
 * @author bubble
 *
 */
class Node {
    public Node leftchild;
    public Node rightchild;
    public int data;
    public Node(int data) {
        this.data = data;
    }
}
public class TestBinTree {
    /**
     * 将一个arry数组构建成一个完全二叉树
     * @param arr 需要构建的数组
     * @return 二叉树的根节点
     */
    public Node initBinTree(int[] arr) {
        /*数组的长度为1时返回值为自己的节点*/
        if(arr.length == 1) {
            return new Node(arr[0]);
        }
        /*数组长度不为一,则一个个加进去数组中,数组存储节点*/
        List<Node> nodeList = new ArrayList<>();
        for(int i = 0; i < arr.length; i++) {
            nodeList.add(new Node(arr[i]));
        }
        /*实现关系的依赖,因为这里是完全二叉树,所以可以用下标的方式来保证关系*/
        int temp = 0;
        /*temp可以用来定义左右孩子的位置*/
        while(temp <= (arr.length - 2) / 2) { //注意这里,数组的下标是从零开始的
            if(2 * temp + 1 < arr.length)
                /*自己节点的左孩子是多少*/
                nodeList.get(temp).leftchild = nodeList.get(2 * temp + 1);
            if(2 * temp + 2 < arr.length)
                /*自己节点的右孩子是多少*/
                nodeList.get(temp).rightchild = nodeList.get(2 * temp + 2);
                /*每次左右孩子赋值后自增操作*/
            temp++;
        }
        /*最后返回第一个节点,因为已经关联上了*/
        return nodeList.get(0);
    }
    /**
     * 层序遍历二叉树
     * @param root 二叉树根节点
     * @param nodeQueue ,用到的队列数据结构
     */
    public void trivalBinTree(Node root, Queue<Node> nodeQueue) {
        nodeQueue.add(root);
        Node temp = null;
        while ((temp = nodeQueue.poll()) != null) {
            System.out.print(temp.data + " ");
            if (temp.leftchild != null) {
                nodeQueue.add(temp.leftchild);
            }
            if (temp.rightchild != null) {
                nodeQueue.add(temp.rightchild);
            }
        }
    }
    /**
     * 先序遍历
     * @param root 二叉树根节点
     */
    public void preTrival(Node root) {
        if(root == null) {
            return;
        }
        System.out.print(root.data + " ");
        preTrival(root.leftchild);
        preTrival(root.rightchild);
    }
    /**
     * 中序遍历
     * @param root 二叉树根节点
     */
    public void midTrival(Node root) {
        if(root == null) {
            return;
        }
        midTrival(root.leftchild);
        System.out.print(root.data + " ");
        midTrival(root.rightchild);
    }
    /**
     * 后序遍历
     * @param root 二叉树根节点
     */
    public void afterTrival(Node root) {
        if(root == null) {
            return;
        }
        afterTrival(root.leftchild);
        afterTrival(root.rightchild);
        System.out.print(root.data + " ");
    }
    public static void main(String[] args) {
        TestBinTree btree = new TestBinTree();
        int[] arr = new int[] {1,2,3,4,5,6,7};
        Node root = btree.initBinTree(arr);
        Queue<Node> nodeQueue = new ArrayDeque<>();
        System.out.println("测试结果:");
        System.out.println("层序遍历:");
        btree.trivalBinTree(root, nodeQueue);
        System.out.println("\n先序遍历:");
        btree.preTrival(root);
        System.out.println("\n中序遍历:");
        btree.midTrival(root);
        System.out.println("\n后序遍历:");
        btree.afterTrival(root);
    }
}
全部评论

相关推荐

2025-12-28 16:32
重庆邮电大学 Java
程序员花海:1.技能放最后,来面试默认你都会,技能没啥用 2.实习写的看起来没啥含金量,多读读部门文档,包装下 接LLM这个没含金量 也不要用重构这种 不会给实习生做的 3.抽奖这个还是Demo项目,实际在公司里面要考虑策略,满减,触发点,触发规则 库存 之类的,不是这个项目这么简单 4.教育背景提前,格式为 教育背景 实习 项目 技能 自我评价
简历被挂麻了,求建议
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务