[基础数据结构|Java实现]二:二叉树(一篇搞懂二叉树基本操作)
本篇文章主要包括以下几个方面:二叉树的定义、二叉树的3种dfs遍历、二叉树的bfs遍历(层次遍历)、根据指定格式的描述来构造二叉树、根据后序和中序遍历的序列构造二叉树。
第一部分:理论知识
二叉树的定义
每个父节点下面有0~2个节点,最上面的叫根节点,下面分别叫左右节点。每个节点都满足这种递归定义的要求。
3种dfs遍历
分别是先序遍历,中序遍历,和后序遍历
举例:
先序遍历结果(根左右):124536
中序遍历结果(左根右):425163
后序遍历结果(左右根):452631
BFS层次遍历
上下左右(一层一层遍历):123456
BFS:
1->2->3->4->5->6->
DFS_Mid
4 2 5 1 6 3
DFS_Last
4 5 2 6 3 1
DFS_First
1 2 4 5 3 6
第二部分:代码实现
二叉树结构定义
二叉树结构定义,主要考虑以下几个点:值的保存,保存此节点的左右子节点。
private static class Node{
int val;
Node left;
Node right;
public Node(){
this.left = null;
this.right = null;
}
public Node(int val){
this.val = val;
this.left = null;
this.right = null;
}
}
3种DFS遍历代码实现
直接按照概念,然后使用递归调用即可。
例如先序遍历:先读取当前节点的值,然后再对当前左子节点遍历,最后再遍历当前节点的右子节点。
public static void DFS_First(Node root){//先序遍历 VLR
if(root != null)
System.out.print(root.val+" ");
if(root.left != null)
DFS_First(root.left);
if(root.right != null)
DFS_First(root.right);
}
public static void DFS_Last(Node root){//后序遍历 LRV
if(root.left != null)
DFS_Last(root.left);
if(root.right != null)
DFS_Last(root.right);
if(root != null)
System.out.print(root.val+" ");
}
public static void DFS_Mid(Node root){//中序遍历
if(root.left != null)
DFS_Mid(root.left);
if(root != null)
System.out.print(root.val+" ");
if(root.right != null)
DFS_Mid(root.right);
}
BFS层次遍历实现
需要借助一个队列来存。然后一边读一边存即可。
步骤:
1.先将根节点加入队列中
2.从队列中取队首元素,然后输出这个节点。
3.将此节点的左节点加入队列,将此节点的右节点压入队列。
4.重复2-3步,直到队列中没有元素。
static Queue<Node> printQueu = new ArrayDeque<>();
public static void BFS_Tree(Node root){
printQueu.add(root);
for(;;){
Node node = printQueu.poll();
if(node == null)
break;
System.out.print(node.val+"->");
if(node.left != null)
printQueu.add(node.left);
if(node.right != null)
printQueu.add(node.right);
}
}
第三部分:构造一棵二叉树
1.根据指定输入格式构造一棵二叉树。
每个结点都按照从根结点到它的移动序列给出
样例输入:(11,LL) (7,LLL) (8,R) (5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) ()
使用全局变量boot来保存根节点,temp来保存当前位置的节点,然后一直遍历字符串,并判断是否是LR,如果是则判断当前节点是否有子节点,如果没有则创建,然后进入下一层并截取字符串,一直重复这个步骤即可。
/**
* 根据定位来创建二叉树
* @param val
* @param position
*/
public static void addNode(int val, String position){
if(position == null || position.equals("")){
if(temp == null)
temp = new Node(val);
else{
temp.val = val;
}
temp.used = true;
if(boot == null){
boot = temp;
}
temp = boot;
return;
}
if(boot == null){
boot = temp;
}
if(position.charAt(0) == 'L'){
if(temp.left == null){
temp.left = new Node();
}
temp = temp.left;
}else if(position.charAt(0) == 'R'){
if(temp.right == null){
temp.right = new Node();
}
temp = temp.right;
}
if(position.length()==1){
position = "";
}else {
position = position.substring(1);
}
addNode(val,position);
}
/**
* 判断二叉树是否构造失败
* @return
*/
public static boolean checkTree( Node root){
if(root != null && !root.used)
return false;
if(root.left != null)
return checkTree(root.left);
if(root.right != null)
return checkTree(root.right);
return true;
}
2.按照后续遍历与中序遍历序列构造二叉树
后序遍历:4 5 2 6 3 1
中序遍历:4 2 5 1 6 3
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vBBZ9C3x-1648797308492)(image/image-20220401040905878.png)]
思路:
后序遍历的最后一个节点是根节点,然后去中序遍历中找左、右子树的节点,拆分成左右子树之后,再去找左右子树的根节点,一直重复这个步骤。
例如:
第一次,根:1,左子树节点 4,2,5,右子树节点6,3
第二次,对于左子树,根节点去后序遍历里面找(从后面开始找)2,对于右子树节点,根节点3.
/**
* 根据中序遍历和后续遍历结果,构造二叉树
* @param midArray
* @param lastArray
* @return
*/
public static Node createTree(int[] midArray, int[] lastArray, int mid_begin, int mid_last) {
Node node = null;
//value 是当前创建节点的值,index是其在中序遍历中的位置
int value ;
int index = 0;
boolean flag = false;
for (int m = lastArray.length - 1; m >= 0; m--) {
for (int n = mid_begin; n < mid_last; n++) {
if (lastArray[m] == midArray[n]) {
value = lastArray[m];
index = n;
flag = true;
node = new Node(value);
break;
}
}
if (flag)
break;
}
if (node != null) {
//中序遍历,左子序列
if(index != mid_begin){
node.left = createTree(midArray, lastArray,mid_begin,index);
}
if ((index + 1) < mid_last){
node.right = createTree(midArray, lastArray,index+1,mid_last);
}
}
return node;
}
完整代码
package 二叉树;
import java.util.ArrayDeque;
import java.util.Queue;
public class BTree {
static Queue< Node> printQueu = new ArrayDeque<>();
//根节点
static Node boot = null;
static Node temp = new Node();
private static class Node{
//用来判断当前节点是否已经被使用过
boolean used;
Node left;
Node right;
int val;
public Node(int val){
this.left = null;
this.right = null;
this.val = val;
this.used = true;
}
public Node(){
this.left = null;
this.right = null;
this.used = false;
}
}
//DFS遍历
public static void DFS_First( Node root){//先序遍历 VLR
if(root != null)
System.out.print(root.val+" ");
if(root.left != null)
DFS_First(root.left);
if(root.right != null)
DFS_First(root.right);
}
public static void DFS_Last( Node root){//后序遍历 LRV
if(root.left != null)
DFS_Last(root.left);
if(root.right != null)
DFS_Last(root.right);
if(root != null)
System.out.print(root.val+" ");
}
public static void DFS_Mid( Node root){//中序遍历
if(root.left != null)
DFS_Mid(root.left);
if(root != null)
System.out.print(root.val+" ");
if(root.right != null)
DFS_Mid(root.right);
}
//BFS遍历
public static void BFS_Tree( Node root){
printQueu.add(root);
for(;;){
Node node = printQueu.poll();
if(node == null)
break;
System.out.print(node.val+"->");
if(node.left != null)
printQueu.add(node.left);
if(node.right != null)
printQueu.add(node.right);
}
}
/**
* 根据中序遍历和后续遍历结果,构造二叉树
* @param midArray
* @param lastArray
* @return
*/
public static Node createTree(int[] midArray, int[] lastArray, int mid_begin, int mid_last) {
Node node = null;
//value 是当前创建节点的值,index是其在中序遍历中的位置
int value ;
int index = 0;
boolean flag = false;
for (int m = lastArray.length - 1; m >= 0; m--) {
for (int n = mid_begin; n < mid_last; n++) {
if (lastArray[m] == midArray[n]) {
value = lastArray[m];
index = n;
flag = true;
node = new Node(value);
break;
}
}
if (flag)
break;
}
if (node != null) {
//中序遍历,左子序列
if(index != mid_begin){
node.left = createTree(midArray, lastArray,mid_begin,index);
}
if ((index + 1) < mid_last){
node.right = createTree(midArray, lastArray,index+1,mid_last);
}
}
return node;
}
/**
* 根据定位来创建二叉树
* @param val
* @param position
*/
public static void addNode(int val, String position){
if(position == null || position.equals("")){
if(temp == null)
temp = new Node(val);
else{
temp.val = val;
}
temp.used = true;
if(boot == null){
boot = temp;
}
temp = boot;
return;
}
if(boot == null){
boot = temp;
}
if(position.charAt(0) == 'L'){
if(temp.left == null){
temp.left = new Node();
}
temp = temp.left;
}else if(position.charAt(0) == 'R'){
if(temp.right == null){
temp.right = new Node();
}
temp = temp.right;
}
if(position.length()==1){
position = "";
}else {
position = position.substring(1);
}
addNode(val,position);
}
/**
* 判断二叉树是否构造失败
* @return
*/
public static boolean checkTree( Node root){
if(root != null && !root.used)
return false;
if(root.left != null)
return checkTree(root.left);
if(root.right != null)
return checkTree(root.right);
return true;
}
}