JAVA二叉树的基本操作

时间:2021-07-30 10:26:46

记录二叉树的基本操作DEMO

1、创建一个二叉树类

这里约束了泛型只能为实现了Comparable这个接口的类型。

?
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
/**
 * @author JackHui
 * @version BinaryTree.java, 2020年03月05日 12:45
 */
public class BinaryTree<T extends Comparable> {
 
    //树根
    BinaryTreeNode root;
 
    public boolean deleteData(T data) {
        if (root.data.equals(data)) {
            root = null;
            return true;
        }
        return root.deleteNode(data);
    }
 
    public T frontSearch(T data) {
        return (T) root.frontSearch(data);
    }
 
    public T midSearch(T data) {
        return (T) root.midSearch(data);
    }
 
    public T rearSearch(T data) {
        return (T) root.rearSearch(data);
    }
 
    public void frontEach() {
        this.root.frontEach();
    }
 
    public void midEach() {
        this.root.midEach();
    }
 
    public void rearEach() {
        this.root.rearEach();
    }
 
 
    public BinaryTreeNode getRoot() {
        return root;
    }
 
    public void setRoot(BinaryTreeNode root) {
        this.root = root;
    }
}

2、然后创建二叉树的节点

?
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package binarytree;
 
/**
 * @author JackHui
 * @version BinaryTreeNode.java, 2020年03月06日 10:24
 */
public class BinaryTreeNode<T extends Comparable> {
    T data;
    BinaryTreeNode lChild;
    BinaryTreeNode rChild;
 
    public BinaryTreeNode(T data) {
        this.data = data;
    }
 
    //先序遍历
    public void frontEach() {
        System.out.print(this.data + "\t");
        if (lChild != null) {
            lChild.frontEach();
        }
        if (rChild != null) {
            rChild.frontEach();
        }
    }
 
    //中序遍历
    public void midEach() {
        if (lChild != null) {
            lChild.frontEach();
        }
        System.out.print(this.data + "\t");
        if (rChild != null) {
            rChild.frontEach();
        }
    }
 
    //后序遍历
    public void rearEach() {
        if (lChild != null) {
            lChild.frontEach();
        }
        if (rChild != null) {
            rChild.frontEach();
        }
        System.out.print(this.data + "\t");
    }
 
    //先序查找
    public T frontSearch(T data) {
        T target = null;
        System.out.println("[先序遍历]当前遍历到的元素:" + this.data + "\t查找的元素:" + data + "\t" + (this.data.compareTo(data) == 0 ? "查找到元素:" + data : ""));
        if (this.data.compareTo(data) == 0) {
            return data;
        } else {
            if (lChild != null && (target = (T) lChild.frontSearch(data)) != null) {
                return target;
            }
            if (rChild != null && (target = (T) rChild.frontSearch(data)) != null) {
                return target;
            }
        }
        return target;
    }
 
    //中序查找
    public T midSearch(T data) {
        T target = null;
        if (lChild != null && (target = (T) lChild.midSearch(data)) != null) {
            return target;
        }
        System.out.println("[中序遍历]当前遍历到的元素:" + this.data + "\t查找的元素:" + data + "\t" + (this.data.compareTo(data) == 0 ? "查找到元素:" + data : ""));
        if (this.data.compareTo(data) == 0) {
            return data;
        } else {
            if (rChild != null && (target = (T) rChild.midSearch(data)) != null) {
                return target;
            }
        }
        return target;
    }
 
    //后序查找
    public T rearSearch(T data) {
        T target = null;
        if (lChild != null && (target = (T) lChild.rearSearch(data)) != null) {
            return target;
        }
        if (rChild != null && (target = (T) rChild.rearSearch(data)) != null) {
            return target;
        }
        System.out.println("[后续遍历]当前遍历到的元素:" + this.data + "\t查找的元素:" + data + "\t" + (this.data.compareTo(data) == 0 ? "查找到元素:" + data : ""));
        if (this.data.compareTo(data) == 0) {
            return data;
        }
        return target;
    }
 
    //根据值删除节点
    public boolean deleteNode(T data) {
        System.out.println("[节点删除]当前遍历到的父节点:" + this.data + "\t" + "匹配的节点数据:" + data);
        //判断左子树是否匹配
        if (this.lChild != null && (this.lChild.data.compareTo(data) == 0)) {
            System.out.println("[节点删除]当前遍历到的父节点:" + this.data + "\t" + "匹配的节点数据:" + data + "\t节点删除成功!");
            this.lChild = null;
            return true;
        } else if (this.rChild != null && (this.rChild.data.compareTo(data) == 0)) {
            System.out.println("[节点删除]当前遍历到的父节点:" + this.data + "\t" + "匹配的节点数据:" + data + "\t节点删除成功!");
            this.rChild = null;
            return true;
        }
        if (this.lChild != null && this.lChild.deleteNode(data)) {
            return true;
        }
        if (this.rChild != null && this.rChild.deleteNode(data)) {
            return true;
        }
        return false;
    }
 
    public T getData() {
        return data;
    }
 
    public void setData(T data) {
        this.data = data;
    }
 
    public BinaryTreeNode getlChild() {
        return lChild;
    }
 
    public void setlChild(BinaryTreeNode lChild) {
        this.lChild = lChild;
    }
 
    public BinaryTreeNode getrChild() {
        return rChild;
    }
 
    public void setrChild(BinaryTreeNode rChild) {
        this.rChild = rChild;
    }
}

到此这篇关于JAVA二叉树的基本操作DEMO的文章就介绍到这了,更多相关JAVA二叉树内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_41311209/article/details/104695849