二叉树

Posted 夏末的秋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二叉树相关的知识,希望对你有一定的参考价值。

package com.m01.program.maven_1;

import java.util.Stack;

public class TestBinaryTree {

    public static void main(String[] args) {
        TreeNode node=getBinaryTree();
        node=destroy(node);
        System.out.println(node);
        Stack<TreeNode> s=new Stack();
        
    }
    //创建二叉树
    static TreeNode getBinaryTree(){
        TreeNode node1=new TreeNode("A");
        TreeNode node2=new TreeNode("B");
        TreeNode node3=new TreeNode("C");
        TreeNode node4=new TreeNode("BB1");
        TreeNode node5=new TreeNode("BB2");
        TreeNode node6=new TreeNode("CC1");
        TreeNode node7=new TreeNode("CC2");
        node1.setLeftNode(node2);
        node1.setRightNode(node3);
        node2.setLeftNode(node4);
        node2.setRightNode(node5);
        node3.setLeftNode(node6);
        node3.setRightNode(node7);
        return node1;
    }
    //输出节点
    static void out(TreeNode<?> node){
        System.out.println(node.getData());
    }
    //先序遍历二叉树
    static void xianIterator(TreeNode<?> node){
        out(node);
        TreeNode leftNode=node.getLeftNode();
        TreeNode rightNode=node.getRightNode();
        if(leftNode!=null){
            xianIterator(leftNode);
        }
        if(rightNode!=null){
            xianIterator(rightNode);
        }
    }
    //中序遍历二叉树
    static void zhongIterator(TreeNode<?> node){
        TreeNode<?> leftNode=node.getLeftNode();
        TreeNode<?> rightNode=node.getRightNode();
        if(leftNode!=null){
            zhongIterator(leftNode);
        }
        out(node);
        if(rightNode!=null){
            zhongIterator(rightNode);
        }
    }
    //后序遍历二叉树
    static void houIterator(TreeNode<?> node){
        TreeNode<?> leftNode=node.getLeftNode();
        TreeNode<?> rightNode=node.getRightNode();
        if(rightNode!=null){
            out(rightNode);
        }
        if(leftNode!=null){
            out(leftNode);
        }
        out(node);
    }
    //判断 二叉树的高度
    public static int getHeight(TreeNode<?> node){
        if(node==null){
            return 0;
        }else{
            int left=getHeight(node.getLeftNode());
            int right=getHeight(node.getRightNode());
            return left>=right?left+1:right+1;
        }
    }
    //得到树中的对象数量
    public static int getAllObjs(TreeNode<?> node){
        if(node==null){
            return 0;
        }else{
            TreeNode leftNode=node.getLeftNode();
            TreeNode rightNode=node.getRightNode();
            int leftNum=getAllObjs(leftNode);
            int rightNum=getAllObjs(rightNode);
            return leftNum+rightNum+1;
        }
    }
    //删除node以及其以下的数据
    static TreeNode<?> destroy(TreeNode<?> node){
        System.out.println("--");
        if(node!=null){
            TreeNode leftNode=node.getLeftNode();
            TreeNode rightNode=node.getRightNode();
            destroy(leftNode);
            destroy(rightNode);
            node=null;
        }
        return node;
    }
}
/*
 * 定义节点
 */
class TreeNode<T>{
    private T data;
    private TreeNode<T> leftNode;
    private TreeNode<T> rightNode;
    
    public TreeNode() {
        
    }
    public TreeNode(T data, TreeNode<T> leftNode, TreeNode<T> rightNode) {
        super();
        this.data = data;
        this.leftNode = leftNode;
        this.rightNode = rightNode;
    }
    public TreeNode(T data) {
        this.data = data;
    }
    public TreeNode<T> getLeftNode() {
        return leftNode;
    }
    public void setLeftNode(TreeNode<T> leftNode) {
        this.leftNode = leftNode;
    }
    public TreeNode<T> getRightNode() {
        return rightNode;
    }
    public void setRightNode(TreeNode<T> rightNode) {
        this.rightNode = rightNode;
    }
    public T getData() {
        return data;
    }
    public void setData(T data) {
        this.data = data;
    }
    @Override
    public String toString() {
        return "TreeNode [data=" + data + ", leftNode=" + leftNode + ", rightNode=" + rightNode + "]";
    }
    
}

 

以上是关于二叉树的主要内容,如果未能解决你的问题,请参考以下文章

数据结构 二叉树

数据结构二叉树经典基础习题

数据结构 二叉树的简单理解和代码实现

用c语言写二叉树,源代码。

数据结构中二叉树的顺序存储结构代码怎么编写?

二叉树练习题