第二十天算法设计

BinaryTree 类实现:
java
package suanfa;

public class BinaryTree {
// 定义树的节点类
private static class Node {
int value;
Node left, right;

    public Node(int value) {
        this.value = value;
        left = right = null;
    }
}

private Node root;

// 构造函数,初始化空树
public BinaryTree() {
    root = null;
}

// 插入节点到二叉树
public void insert(int value) {
    root = insertRec(root, value);
}

// 递归的插入节点
private Node insertRec(Node root, int value) {
    // 如果树为空,返回新节点
    if (root == null) {
        root = new Node(value);
        return root;
    }

    // 否则,递归地插入到左子树或右子树
    if (value < root.value) {
        root.left = insertRec(root.left, value);
    } else if (value > root.value) {
        root.right = insertRec(root.right, value);
    }

    // 返回未改变的节点指针
    return root;
}

// 前序遍历:根 -> 左 -> 右
public void preOrder() {
    preOrderRec(root);
}

private void preOrderRec(Node root) {
    if (root != null) {
        System.out.print(root.value + " ");
        preOrderRec(root.left);
        preOrderRec(root.right);
    }
}

// 中序遍历:左 -> 根 -> 右
public void inOrder() {
    inOrderRec(root);
}

private void inOrderRec(Node root) {
    if (root != null) {
        inOrderRec(root.left);
        System.out.print(root.value + " ");
        inOrderRec(root.right);
    }
}

// 后序遍历:左 -> 右 -> 根
public void postOrder() {
    postOrderRec(root);
}

private void postOrderRec(Node root) {
    if (root != null) {
        postOrderRec(root.left);
        postOrderRec(root.right);
        System.out.print(root.value + " ");
    }
}

// 查找最小值
public int findMin() {
    return findMinRec(root);
}

private int findMinRec(Node root) {
    if (root.left == null) {
        return root.value;
    }
    return findMinRec(root.left);
}

// 查找最大值
public int findMax() {
    return findMaxRec(root);
}

private int findMaxRec(Node root) {
    if (root.right == null) {
        return root.value;
    }
    return findMaxRec(root.right);
}

// 查找节点
public boolean search(int value) {
    return searchRec(root, value);
}

private boolean searchRec(Node root, int value) {
    if (root == null) {
        return false;
    }
    if (value == root.value) {
        return true;
    }
    return value < root.value ? searchRec(root.left, value) : searchRec(root.right, value);
}

}
BinaryTreeTest 类:
java
package suanfa;

public class BinaryTreeTest {
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();

    // 插入节点
    tree.insert(50);
    tree.insert(30);
    tree.insert(20);
    tree.insert(40);
    tree.insert(70);
    tree.insert(60);
    tree.insert(80);

    // 前序遍历 (根 -> 左 -> 右)
    System.out.print("前序遍历: ");
    tree.preOrder();
    System.out.println();

    // 中序遍历 (左 -> 根 -> 右)
    System.out.print("中序遍历: ");
    tree.inOrder();
    System.out.println();

    // 后序遍历 (左 -> 右 -> 根)
    System.out.print("后序遍历: ");
    tree.postOrder();
    System.out.println();

    // 查找最小值
    System.out.println("最小值: " + tree.findMin());

    // 查找最大值
    System.out.println("最大值: " + tree.findMax());

    // 查找节点
    System.out.println("是否包含节点40: " + tree.search(40)); // true
    System.out.println("是否包含节点100: " + tree.search(100)); // false
}

}
录制: untitled2 – Insertion.java
录制文件:https://meeting.tencent.com/crm/KzGGkGE85d

posted @   申shen  阅读(1)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示