二叉树的基本操作(代码示例及测试类代码及运行截图)

二叉树的基本操作

// 前序遍历
void preOrderTraversal(Node root);
// 中序遍历
void inOrderTraversal(Node root);
// 后序遍历
void postOrderTraversal(Node root);
// 遍历思路-求结点个数
static int size = 0;
void getSize1(Node root);
// 子问题思路-求结点个数
int getSize2(Node root);
// 遍历思路-求叶子结点个数
static int leafSize = 0;
void getLeafSize1(Node root);
// 子问题思路-求叶子结点个数
int getLeafSize2(Node root);
// 子问题思路-求第 k 层结点个数
int getKLevelSize(Node root);
// 获取二叉树的高度
int getHeight(Node root);
// 查找 val 所在结点,没有找到返回 null
// 按照 根 -> 左子树 -> 右子树的顺序进行查找
// 一旦找到,立即返回,不需要继续在其他位置查找
Node find(Node root, int val);

代码示例:

class BTNode {
    
    
    public char val;//定义一个节点存放字母
    public BTNode left;
    public BTNode right;

    public BTNode(char val) {
    
    //提供一个构造方法
        this.val = val;
    }
}



public class BinaryTree {
    
    
    /**
     * 首先创建二叉树
     * @return
     */
    public BTNode creatTree(){
    
    
        BTNode A = new BTNode('A');
        BTNode B = new BTNode('B');
        BTNode C = new BTNode('C');
        BTNode D = new BTNode('D');
        BTNode E = new BTNode('E');
        BTNode F = new BTNode('F');
        BTNode G = new BTNode('G');
        BTNode H = new BTNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        E.right= H;
        C.left = F;
        C.right= G;
        return A;

    }
    // 前序遍历
    void preOrderTraversal(BTNode root){
    
    //输入的参数为根节点
        if (root == null) return;
        System.out.print(root.val);
        preOrderTraversal(root.left);
        preOrderTraversal(root.right);


    }
    // 中序遍历
    void inOrderTraversal(BTNode root){
    
    
        if(root == null) return;
        inOrderTraversal(root.left);
        System.out.print(root.val);
        inOrderTraversal(root.right);


    }
    // 后序遍历
    void postOrderTraversal(BTNode root){
    
    
        if(root == null) return;
        postOrderTraversal(root.left);
        postOrderTraversal(root.right);
        System.out.print(root.val);


    }
    // 遍历思路-求结点个数
    static int size = 0;
    void getSize1(BTNode root){
    
    
        if(root == null) return;
        size++;
        getSize1(root.right);
        getSize1(root.left);


    }
    // 子问题思路-求结点个数
    int getSize2(BTNode root){
    
    
        if(root == null) return 0;
        return getSize2(root.left) + getSize2(root.right) + 1;


    }
    // 遍历思路-求叶子结点个数
    static int leafSize = 0;
    void getLeafSize1(BTNode root){
    
    
        if(root == null) return;
        if(root.left == null && root.right == null) {
    
    
            leafSize++;
        }
        getLeafSize1(root.left);
        getLeafSize1(root.right);

    }
    // 子问题思路-求叶子结点个数
    int getLeafSize2(BTNode root){
    
    
        if(root == null) return 0;
        if(root.left == null && root.right == null) {
    
    
            return 1;
        }
        return getLeafSize2(root.left) + getLeafSize2(root.right);

    }
    // 子问题思路-求第 k 层结点个数
    int getKLevelSize(BTNode root, int k){
    
    
        if(root == null) {
    
    
            return 0;
        }
        if(k == 1) {
    
    
            return 1;
        }
        return getKLevelSize(root.left,k-1)+
                getKLevelSize(root.right,k-1);

    }
    // 获取二叉树的高度
    int getHeight(BTNode root) {
    
    
        if(root == null) return 0;

        return getHeight(root.left) > getHeight(root.right) ?
                getHeight(root.left)+1 : getHeight(root.right)+1;
    }


    BTNode find(BTNode root, int val) {
    
    
        if(root== null) return null;
        if(root.val == val){
    
    
            return root;
        }
        BTNode ret = find(root.left,val);
            if(ret == null){
    
    
                return ret;
            }
        ret = find(root.right,val);
        if(ret == null){
    
    
            return ret;
        }
        return null;
    }
}

测试类代码:

/**
 * 二叉树基础操作测试类
 */

public class TestDemo {
    
    
    public static void main(String[] args) {
    
    
        BinaryTree binaryTree = new BinaryTree();
        BTNode root = binaryTree.creatTree();
        binaryTree.preOrderTraversal(root);
        System.out.println();
        binaryTree.inOrderTraversal(root);
        System.out.println();
        binaryTree.postOrderTraversal(root);
        System.out.println();
        binaryTree.getSize1(root);
        System.out.println(binaryTree.size);
        System.out.println(binaryTree.getSize2(root));
        binaryTree.getLeafSize1(root);
        System.out.println(binaryTree.leafSize);
        System.out.println(binaryTree.getLeafSize2(root));
        System.out.println(binaryTree.getKLevelSize(root,4));

       // System.out.println(binaryTree.getKLevelSize(root,4));

        //System.out.println(binaryTree.getHeight(root));
        BTNode ret = binaryTree.find(root,'A');
        if(ret == null) {
    
    
            System.out.println("没有找到");
            return;
        }
        System.out.println(ret.val);
    }
    }


运行代码截图:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44436675/article/details/113285801
今日推荐