二叉查找树

二叉查找树(binary search tree),二叉查找树在二叉树的基础上增加了以下几个条件:
如果左子树不为空,则左子树上所有节点的值均小于根节点的值
如果右子树不为空,则右子树上所有节点的值均大于根节点的值
左、右子树也都是二叉查找树。
二查查找树的时间复杂度是O(log(n)),但是极端情况会退化成O(n)

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package tree;
 
/**
 * @author: tianhaichao
 * @date: 2022/8/29 14:21
 * @description: 二叉树 有序插入,中序遍历
 */
public class BinaryTree {
    private static TreeNode root;
 
    public static void insertNode(int data) {
        if (root == null) {
            root = new TreeNode(data);
            return;
        } else {
            insertNode(root, data);
        }
    }
 
    /**
     * @author: tianhaichao
     * @date: 2022/9/20 10:14
     * @description:插入数据
     */
    public static void insertNode(TreeNode node, int data) {
        // 如果比当前节点大,放右边
        if (data > node.getData()) {
            // 如果右节点为空,赋值
            if (node.getRightNode() == null) {
                node.setRightNode(new TreeNode(data));
                return;
            } else {
                insertNode(node.getRightNode(), data);
            }
        }
        // 如果比当前节点小,放左边
        if (data <= node.getData()) {
            if (node.getLeftNode() == null) {
                node.setLeftNode(new TreeNode(data));
                return;
            } else {
                insertNode(node.getLeftNode(), data);
            }
        }
    }
 
    /**
     * @author: tianhaichao
     * @date: 2022/9/20 15:10
     * @description:前序遍历
     */
    public static void prepTraversal(TreeNode node) {
        if (node == null) {
            return;
        }
        // 先输出父节点,再输出左节点和右节点
        System.out.print(node.getData() + "  ");
        prepTraversal(node.getLeftNode());
        prepTraversal(node.getRightNode());
    }
 
    public static void main(String[] args) {
        BinaryTree.insertNode(3);
        BinaryTree.insertNode(10);
        BinaryTree.insertNode(5);
        BinaryTree.insertNode(6);
        BinaryTree.insertNode(8);
        BinaryTree.prepTraversal(root);
    }
 
}
 
 
class TreeNode {
    private int data;
    private TreeNode leftNode;
    private TreeNode rightNode;
 
    public TreeNode(int data) {
        this.data = data;
    }
 
    public TreeNode(int data, TreeNode leftNode, TreeNode rightNode) {
        this.data = data;
        this.leftNode = leftNode;
        this.rightNode = rightNode;
    }
 
 
    public int getData() {
        return data;
    }
 
    public void setData(int data) {
        this.data = data;
    }
 
    public TreeNode getLeftNode() {
        return leftNode;
    }
 
    public void setLeftNode(TreeNode leftNode) {
        this.leftNode = leftNode;
    }
 
    public TreeNode getRightNode() {
        return rightNode;
    }
 
    public void setRightNode(TreeNode rightNode) {
        this.rightNode = rightNode;
    }
}

  

posted @   田海超  阅读(30)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示