【数据结构】(二叉树的Java与C++实现)

二叉树作为重要的数据结构类型,本篇二叉树内部方法的实现多采用了递归,实现了诸如(四种遍历\结点删除\结点查找\叶子数量\高度\判断是否完全二叉树\某层结点数)等方法,是值得一读的二叉树的实现 ʅ(‾◡◝)ʃ

前言

树结构的知识点比较复杂,在本篇中学习了很多Java用递归方式编写的函数体,值得一读。惰性删除是指当一个元素要被删除时,它仍被保留在树中,只是标记为删除了,这在有重复项的时候很常用,因为此时记录出现频率数的域可以减1,如果树中的实际节点数与“被删除”的节点数相同,那么树的深度预计只上升一个小的常数,因此存在一个与惰性删除相关的非常小的时间损耗,并且,如果被删除的项是重新插入的,那么久避免了分配一个新单元的开销了
IMG_6581.jpg

二叉树的Java实现

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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package BinaryTree;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;

public class BiTree{

private int count;
private Node root;
public int index;
class Node {

public Node lchild;
public Node rchild;
public int data;
public Node(int data) {
this.data = data;
}
public Node getLchild() {
return lchild;
}
public void setLchild(Node lchild) {
this.lchild = lchild;
}
public Node getRchild() {
return rchild;
}
public void setRchild(Node rchild) {
this.rchild = rchild;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
}
//二叉树的建立
public Node CreateBTree(int[] a){
Node root = null;
if(a[index]!='#'){
root = new Node(a[index]);
index++;
root.setLchild(CreateBTree(a));
index++;
root.setRchild(CreateBTree(a));
}
return root;
}
//二叉树节点的删除
public boolean delete(int data) {
Node current = root;
Node parent = root;
boolean isleftchild = false;
while(current.data!=data) {
parent = current;
if(current.data>data) {
isleftchild = true;
current = current.lchild;
}else{
isleftchild = false;
current = current.rchild;
}
if(current == null) {
return false;
}
}
//被删除的节点没有子节点
if(current.lchild==null &&current.rchild==null) {
if(current==root) {
root = null;
}else if(isleftchild) {
parent.lchild = null;
}else {
parent.rchild = null;
}
return true;
}
//被删除的节点有一个子节点
if(current.lchild==null&&current.rchild!=null) {
if(root == current) {
root = current.rchild;
}else if(isleftchild) {
parent.lchild = current.rchild;
}else {
parent.rchild = current.rchild;
}
}else if(current.lchild!=null&&current.rchild==null) {
if(root == current) {
root = current.lchild;
}else if(isleftchild) {
parent.lchild = current.lchild;
}else {
parent.rchild = current.lchild;
}
}
//被删除的节点有两个子节点
if(current.lchild != null && current.rchild != null){
//获取删除节点的后继结点
Node successor = getSuccessor(current);
if (root == current) {
root = successor;
} else if (isleftchild) {
parent.lchild = successor;
} else {
parent.rchild = successor;
}
}
return false;
}
public Node getSuccessor(Node delNode) {
Node successorParent = delNode;
Node successor = delNode;
Node current = delNode.rchild;
while (current != null) {
successorParent = successor;
successor = current;
current = current.lchild;
}
if (successor != delNode.rchild) {
successorParent.lchild = successor.rchild;
successor.rchild = delNode.rchild;
}
return successor;
}
//先序遍历
public void prevOrder(Node root) {
if(root == null) {
return;
}
System.out.print(root.getData()+" ");
prevOrder(root.getLchild());
prevOrder(root.getRchild());
}
//中序遍历
public void inOrder(Node root) {
if(root==null) {
return;
}
inOrder(root.getLchild());
System.out.print(root.getData()+" ");
inOrder(root.getRchild());
}
//后序遍历
public void postOrder(Node root) {
if(root==null) {
return;
}
postOrder(root.getLchild());
postOrder(root.getRchild());
System.out.print(root.getData()+" ");
}
//层序遍历
public void BTreeLevelOrder(){
Node root = this.root;
Queue <Node> queue = new LinkedList<Node>();
LinkedList<Node> list = new LinkedList<Node>();
queue.offer(root);
while(!queue.isEmpty()){
Node pre = queue.poll();
list.add(pre);
if(pre.getLchild()!=null)
queue.offer(pre.getLchild());
if(pre.getRchild()!=null)
queue.offer(pre.getRchild());
}
Iterator<Node> it = list.iterator();
while(it.hasNext()){
Node cur = (Node)it.next();
System.out.print(cur.getData()+" ");
}
}
//获得二叉树的高度
public int getHeight(Node root){
//递归获取
int leftHeight = 0;
int rightHeight = 0;
if(root==null){
return 0;
}else{
leftHeight = getHeight(root.getLchild());
rightHeight = getHeight(root.getRchild());
}
return leftHeight >= rightHeight ? ++leftHeight:++rightHeight; //最终高度应是左右子树高度中最大的一个
}
//获得二叉树的叶子结点
public int getLeaf(Node root) {
if(root == null) {
return 0;
}else if(root.getLchild()==null&&root.getRchild()==null) {
System.out.println("叶子结点:"+root.getData());
return 1;
}
return getLeaf(root.getLchild())+getLeaf(root.getRchild());
}
//获得二叉树某一层的节点
public int getNum(Node root, int deep) {
if(deep == 1) {
if(root==null) {
return 0;
}else {
System.out.println("结点:"+root.getData());
return 1;
}
}
return getNum(root.getLchild(), deep-1)+getNum(root.getRchild(), deep-1);
}
//查找某一值结点
public Node search(Node root, int key) {
if(root==null) {
return null;
}else if(root.getData()==key) {
return root;
}
Node left = search(root.getLchild(), key);
if(left!=null) {
return left;
}
Node right = search(root.getRchild(), key);
if(right!=null) {
return right;
}
return null;
}
//判断某一结点是否存在
public boolean isNull(Node root) {
return root!=null;
}
//判断一棵树是否是完全二叉树
public boolean isCompleteBTree(){
Node root = this.root;
Queue <Node> queue = new LinkedList<Node>();
queue.offer(root);

while(!queue.isEmpty()){
Node pre = queue.poll();
if(pre==null)
break;
queue.offer(pre.getLchild());
queue.offer(pre.getRchild());

}
while(!queue.isEmpty()){
Node cur = queue.poll();
if(cur!=null)
return false;
}
return true;
}
public static void main(String[] args) {
BiTree tree = new BiTree();
int[] a = new int[]{1,2,'#',3,'#',4,'#','#',5,6,'#','#','#' };
tree.root = tree.CreateBTree(a);
System.out.println("先序遍历:");
tree.prevOrder(tree.root);
System.out.println();
System.out.println("中序遍历:");
tree.inOrder(tree.root);
System.out.println();
System.out.println("后序遍历:");
tree.postOrder(tree.root);
System.out.println();
System.out.println("层序遍历:");
tree.BTreeLevelOrder();
System.out.println();
System.out.println("此二叉树的高度为:"+tree.getHeight(tree.root));
System.out.println("此二叉树的叶子结点数:"+tree.getLeaf(tree.root));
System.out.println("第二层的结点树为:"+tree.getNum(tree.root, 2));
Node order =tree.search(tree.root, 6);
System.out.println("查找值为6的结点是否存在:"+tree.isNull(order));
Node order1 = tree.search(tree.root, 7);
System.out.println("查找值为7的结点是否存在:"+tree.isNull(order1));
System.out.println("这是一棵完全二叉树吗:"+tree.isCompleteBTree());
}
}

二叉树的C++实现

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
#include <iostream>
typedef char elemtype;
using namespace std;
//二叉树的存储结构
struct bitree
{
elemtype data;
bitree *lchild,*rchild;
};
//建立二叉树
bitree* create()
{
bitree *root,*s,*q[100];
int front=1,rear=0;
char ch;
root=NULL;
cout<<"请输入结点值,(‘ # ’结束)"<<endl;
cin>>ch;
while(ch!='#')
{
s=NULL;
if(ch!=',')
{
s=new bitree;
s->data=ch;
s->lchild=NULL;
s->rchild=NULL;
}
rear++;
q[rear]=s; // 进队
if(rear==1)
root=s;
else
{
if((s!=NULL)&&(q[front]!=NULL))
{
if(rear%2==0)
q[front]->lchild=s;
else
q[front]->rchild=s;
}
if(rear%2==1)
front++; // 出队
}
cin>>ch;
}
return root;
}
//先序遍历
void preorder(bitree *root)
{
bitree *p;
p=root;
if (p!=NULL)
{
cout<<p->data<<" ";
preorder(p->lchild);
preorder(p->rchild);
}
}
//中序遍历
void inorder(bitree *root)
{
bitree *p;
p=root;
if(p!=NULL)
{
inorder(p->lchild);
cout<<p->data<<" ";
inorder(p->rchild);
}
}
//后序遍历
void postorder(bitree *root)
{
bitree *p;
p=root;
if(p!=NULL)
{
postorder(p->lchild);
postorder(p->rchild);
cout<<p->data<<" ";
}
}
//求二叉树中叶子结点的个数
int LeafCount(bitree *T)
{
if(!T) return 0;
if(!T->lchild&&!T->rchild)
{
return 1;
}
else
{
return LeafCount(T->lchild)+LeafCount(T->rchild);
}
}
//求树的高度
int Height(bitree *root)
{
int L,R;
if(root==NULL)return 0;
else
{
L=Height(root->lchild);
R=Height(root->rchild);
return (L>R)?L+1:R+1;
}
}
int main()
{
bitree *root;
int leaf;
root=create();
cout<<"各种遍历方式对应的遍历结果:"<<endl;
cout<<"先序遍历的结果:"<<endl;
preorder(root);
cout<<endl;
cout<<"中序遍历的结果:"<<endl;
inorder(root);
cout<<endl;
cout<<"后序遍历的结果:"<<endl;
postorder(root);
cout<<endl;
cout<<"树的叶子结点个数为:"<<LeafCount(root)<<endl;
cout<<"树的高度为:"<<Height(root)<<endl;
}
如果觉得还不错的话,把它分享给朋友们吧(ง •̀_•́)ง