二叉树的创建及遍历

来源:互联网 发布:淘宝误删收藏宝贝恢复 编辑:程序博客网 时间:2024/06/05 07:13
在计算机科学中,二叉树是每个节点最多有两个子树的树结构。通常子树被称作“左子树”(left subtree)和“右子树”(right subtree)。二叉树常被用于实现二叉查找树和二叉堆。

二叉树的每个结点至多只有二棵子树(不存在度大于2的结点),二叉树的子树有左右之分,次序不能颠倒。二叉树的第i层至多有2^{i-1}个结点;深度为k的二叉树至多有2^k-1个结点;对任何一棵二叉树T,如果其终端结点数为n_0,度为2的结点数为n_2,则n_0=n_2+1。

一棵深度为k,且有2^k-1个节点称之为满二叉树;深度为k,有n个节点的二叉树,当且仅当其每一个节点都与深度为k的满二叉树中,序号为1至n的节点对应时,称之为完全二叉树。

二叉树有五种基本形态:


二叉树的遍历:
所谓遍历(Traversal)是指沿着某条搜索路线,依次对树中每个结点均做一次且仅做一次访问。访问结点所做的操作依赖于具体的应用问 题。 遍历是二叉树上最重要的运算之一,是二叉树上进行其它运算之基础。
创建二叉树如下图,然后用递归法遍历;

JAVA代码实现:
package tree;import java.util.LinkedList;import java.util.List;public class binaryTree {    static String str;    char[] arr = str.toCharArray();//内部类,节点Node    private static class Node {        Node leftSubTree;//左子树        Node rightSubTree;//右子树        char ch;        Node(char ch01) {            this.leftSubTree = null;            this.rightSubTree = null;            this.ch = ch01;        }    }    private static List<binaryTree.Node> nodeList = null;    public void createBinaryTree() {        //将char型数组转化为节点并存入链表中        nodeList = new LinkedList<>();        for (int nodeIndex = 0; nodeIndex < arr.length; nodeIndex++) {            nodeList.add(new binaryTree.Node(arr[nodeIndex]));        }        //按照二叉树的层序生成方式节点之间的数字关系建立二叉树        for (int parentIndex = 0; parentIndex < arr.length / 2 - 1; parentIndex++) {            nodeList.get(parentIndex).leftSubTree = nodeList.get(parentIndex * 2 + 1);            nodeList.get(parentIndex).rightSubTree = nodeList.get(parentIndex * 2 + 2);        }        //当节点的数量为偶数时,最后一个父节点没有右子树        int lastParentIndex = arr.length / 2 - 1;        System.out.println("最后一个父节点的位置: " + lastParentIndex);        nodeList.get(lastParentIndex).leftSubTree = nodeList.get(lastParentIndex * 2 + 1);        if (arr.length % 2 == 1) {            nodeList.get(lastParentIndex).rightSubTree = nodeList.get(lastParentIndex * 2 + 2);        }    }    //递归法遍历二叉树    //先序遍历    public static void preOrderTraverse(Node node) {        if (node == null) {            return;        }        System.out.print(node.ch + "---");        preOrderTraverse(node.leftSubTree);        preOrderTraverse(node.rightSubTree);    }    //中序遍历    public static void inOrderTraverse(Node node) {        if (node == null) {            return;        }        inOrderTraverse(node.leftSubTree);        System.out.print(node.ch + "---");        inOrderTraverse(node.rightSubTree);    }    //后续遍历    public static void postOrderTraverse(Node node) {        if (node == null) {            return;        }        postOrderTraverse(node.leftSubTree);        postOrderTraverse(node.rightSubTree);        System.out.print(node.ch + "---");    }    public static void main(String[] args) {        binaryTree.str = "ABCDEFGHIJ";        binaryTree binTree = new binaryTree();        binTree.createBinaryTree();        Node root = nodeList.get(0);        System.out.println("先序遍历:");        preOrderTraverse(root);        System.out.println();        System.out.println("中序遍历:");        inOrderTraverse(root);        System.out.println();        System.out.println("后序遍历:");        postOrderTraverse(root);    }}
运行结果:
最后一个父节点的位置: 4先序遍历:A---B---D---H---I---E---J---C---F---G---中序遍历:H---D---I---B---J---E---A---F---C---G---后序遍历:H---I---D---J---E---B---F---G---C---A---



0 0