盒子
盒子
文章目录
  1. 二叉树节点定义
  2. 二叉树实现

'有序二叉树实现及相关遍历'

# 实现思路

  • 如果左子树不为空,那么左子树上的所有值都均小于它的根节点的值
  • 如果右子树不为空,那么右子树上的所有值都均大于或等于它的根节点的值
  • 左,右子树也为二叉排序树

二叉树节点定义

  • 当前节点值
  • 当前节点的左节点
  • 当前节点的右节点

代码如下,set与get方法省略

1
2
3
4
5
6
7
8
9
10
11
12
/**
* 当前节点的值
*/
T data;
/**
* 当前节点的左节点
*/
Node left;
/**
* 当前节点的右节点
*/
Node right;

二叉树实现

  • 排序二叉树构建实现思路:

    判断当前节点与新增的节点的大小,没有增加上,则变化当前节点为遍历的节点,继续遍历即可

  • 前,中,后排序,使用递归的方式(前中后都是根据root来的)

  • 层序遍历采用队列
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
package cn.gxm.binarytree;

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

/**
* @author GXM
* @date 2019/5/13
*
* 排序二叉树相关实现
*/
public class Binarytree {
private Node<Integer> root;

public void insert(Integer data){
Node<Integer> newNode = new Node<Integer>(data);
if(root == null){
root = newNode;
return;
}

Node curNode = root;
while (true){
//小于当前节点去找左节点
if((Integer)newNode.data < (Integer) curNode.data){
if(curNode.left == null){
curNode.left = newNode;
break;
}
curNode = curNode.left;
}else {
//大于当前节点或者等于 都找找右节点
if(curNode.right == null){
curNode.right = newNode;
break;
}
curNode = curNode.right;
}
}

}

/**
* 根据数据构建有序二叉树
* @param arr
*/
public Node<Integer> buildTree(Integer []arr){
for (Integer tmp : arr){
insert(tmp);
}
return root;
}

/**
* 前序遍历
*/
public void preTraversing(Node<Integer> root){
Node<Integer> cur = root;
if (cur != null){
System.out.print(cur.data);
preTraversing(cur.left);
preTraversing(cur.right);
}
}

/**
* 中序遍历
*/
public void midTraversing(Node<Integer> root){
Node<Integer> cur = root;
if (cur != null){
midTraversing(cur.left);
System.out.print(cur.data);
midTraversing(cur.right);
}
}

/**
* 后序遍历
*/
public void postTraversing(Node<Integer> root){
Node<Integer> cur = root;
if (cur != null){
postTraversing(cur.left);
postTraversing(cur.right);
System.out.print(cur.data);
}
}

/**
* 层序遍历
* @param root
*/
public void SequenceTraversing(Node<Integer> root){
Queue<Node> queue = new LinkedList<>();
queue.add(root);
// 不空就一直取
while (!queue.isEmpty()){
// poll以后队列中就没有了,指针移到下一个node上,所以我们这里获取
Node poll = queue.poll();
System.out.print(poll.data);
if(poll.left!=null){
queue.add(poll.left);
}
if(poll.right!=null){
queue.add(poll.right);
}
}
}


public static void main(String[] args) {
Binarytree bt = new Binarytree();
Node<Integer> root = bt.buildTree(new Integer[]{2, 8, 7, 4, 9, 3, 1, 6, 7, 5});
System.out.println("前序");
bt.preTraversing(root);

System.out.println();
System.out.println("中序");
bt.midTraversing(root);

System.out.println();
System.out.println("后序");
bt.postTraversing(root);

System.out.println();
System.out.println("层序");
bt.SequenceTraversing(root);
}
}
}

结果

1
2
3
4
5
6
7
8
前序
2187436579
中序
1234567789
后序
1356477982
层序
2187947365

构建的有序二叉树如下
有序二叉树

希望对您有所帮助
May all the ordinary are great, all the ignoble bloom
  • smile