二叉树的基础遍历
1:前序遍历:先访问根结点,然后再访问左子树,最后访问右子树;
2:中序遍历:先访问左子树,然后再访问根结点,最后访问右子树;
3:后序遍历:先访问左子树,然后再访问右子树,最后访问根结点;
示例如下:
前序遍历的API设计:
<pre class="prettyprint hljs vim" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public Queue<Key> preErgodic():使用前序遍历,获取整个树中的所有键
private void preErgodic(Node x,Queue<Key> keys):使用前序遍历,把指定树x中的所有键放入到keys队列中</pre>
实现步骤:
- 把当前结点的key放入到队列中;
- 找到当前结点的左子树,如果不为空,递归遍历左子树;
- 找到当前结点的右子树,如果不为空,递归遍历右子树;
代码实现:
<pre class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//使用前序遍历获取整个树中所有的键
public Queue<Key> preErgoidic(){
Queue<Key> keys = new Queue<>();
preErgoidic(root,keys);
return keys;
}
//使用前序遍历获取指定树x的所有键,并放到Keys队列中
private void preErgoidic(Node x, Queue<Key> keys){
if(x == null){
return;
}
//把x结点的key放入到keys中
keys.enQueue(x.key);
//递归遍历x结点的左子树
if(x.left != null){
preErgoidic(x.left,keys);
}
//递归遍历x结点的右子树
if(x.right != null){
preErgoidic(x.right,keys);
}
}</pre>
中序遍历的API设计:
<pre class="prettyprint hljs fsharp" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public Queue midErgodic():使用中序遍历,获取整个树中的所有键
private void midErgodic(Node x,Queue keys):使用中序遍历,把指定树x中的所有键放入到keys队列中</pre>
实现步骤:
- 找到当前结点的左子树,如果不为空,递归遍历左子树;
- 把当前结点的key放入到队列中;
- 找到当前结点的右子树,如果不为空,递归遍历右子树;
代码实现:
<pre class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//使用中序遍历获取整个树中所有的键
public Queue<Key> midErgoidic(){
Queue<Key> keys = new Queue<>();
midErgoidic(root,keys);
return keys;
}
//使用中序遍历获取指定树x的所有键,并放到Keys队列中
private void midErgoidic(Node x, Queue<Key> keys) {
if(x == null){
return;
}
//递归遍历x结点的左子树
if(x.left != null){
midErgoidic(x.left,keys);
}
//把x结点的key放入到keys中
keys.enQueue(x.key);
//递归遍历x结点的右子树
if(x.right != null){
midErgoidic(x.right,keys);
}
}</pre>
后序遍历的API设计:
<pre class="prettyprint hljs fsharp" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public Queue afterErgodic():使用中序遍历,获取整个树中的所有键
private void afterErgodic(Node x,Queue keys):使用中序遍历,把指定树x中的所有键放入到keys队列中</pre>
实现步骤:
- 找到当前结点的左子树,如果不为空,递归遍历左子树;
- 找到当前结点的右子树,如果不为空,递归遍历右子树;
- 把当前结点的key放入到队列中;
代码实现:
<pre class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//后续遍历
public Queue<Key> afterErgoidic(){
Queue<Key> keys = new Queue<>();
afterErgoidic(root,keys);
return keys;
}
//使用中序遍历获取指定树x的所有键,并放到Keys队列中
private void afterErgoidic(Node x, Queue<Key> keys) {
if(x == null){
return;
}
//递归遍历x结点的左子树
if(x.left != null){
afterErgoidic(x.left,keys);
}
//递归遍历x结点的右子树
if(x.right != null){
afterErgoidic(x.right,keys);
}
//把x结点的key放入到keys中
keys.enQueue(x.key);
}</pre>