二叉树的创立和简单操作
作者:互联网
文章目录
节点类
//静态内部节点类
private static class Node{
Object data;//存放数据
Node left;//左孩子
Node right;//右孩子
public Node(Object data){
this.data = data;
}
}
建立二叉树
public Node createTree(Object[] objects){
System.out.println(objects);
for(Object object : objects)
list.add(new Node(object));
for(int parentIndex = 0; parentIndex < objects.length /2 - 1; parentIndex++){
list.get(parentIndex).left = list.get(2 * parentIndex + 1);
list.get(parentIndex).right = list.get(2 * parentIndex + 2);
}
//对最后节点的父节点进行单独设置,若元素个数为奇数,则可以构建右孩子,否则不能构建
int lastParentIndex = objects.length / 2 - 1;//objects.length>>1 - 1
list.get(lastParentIndex).left = list.get(2 * lastParentIndex + 1);
if(objects.length % 2 == 1){
list.get(lastParentIndex).right = list.get(2 * lastParentIndex + 2);
}
root = list.get(0);
return root;
}
递归实现三种遍历
//前序递归遍历
public void preOrder(Node root){
if(root != null){
System.out.print(root.data + " ");
preOrder(root.left);
preOrder(root.right);
}
}
//中序递归遍历
public void inOrder(Node root){
if(root != null){
inOrder(root.left);
System.out.print(root.data + " ");
inOrder(root.right);
}
}
//后序递归遍历
public void postOrder(Node root){
if(root != null){
postOrder(root.left);
postOrder(root.right);
System.out.print(root.data + " ");
}
}
非递归实现三种遍历
//前序非递归遍历
public void preOrderByStack(Node root){
if(root == null)
return;
Stack<Node> stack = new Stack<>();//建立辅助栈
Node temp = root;
while (temp != null || !stack.isEmpty()){
while (temp != null){
System.out.print(temp.data + " ");//先序遍历时先打印当前节点值
stack.push(temp);
temp = temp.left;
}
//依次出栈,若当前节点的右子树不为空,则将当前引用指向右节点,重新进行上一步循环
if (!stack.isEmpty()){
temp = stack.pop();
temp = temp.right;
}
}
}
//中序非递归遍历
public void inOrderByStack(Node root){
Node temp = root;
Stack<Node> stack = new Stack<>();
while (temp != null || !stack.isEmpty()){
while (temp != null){
stack.push(temp);
temp = temp.left;
}
if(!stack.isEmpty()){
temp = stack.pop();
System.out.print(temp.data + " ");
temp = temp.right;
}
}
}
//后序非递归遍历
public void postOrderByStack(Node root){
if(root == null)
return;
Node temp = root;
Stack<Node> stack = new Stack<>();
stack.push(root);
Node pre = null;
Node cur = null;
while (!stack.isEmpty()){
cur = stack.peek();
//1.当前节点的左右子树都为空,则可以直接打印节点值 2.当前节点前一个访问节点不为空,若前一个节点是当前节点的左孩子,说明
//当前节点的右孩子为空,则可以直接访问,如果是右孩子,则两个节点都已经访问过了
if(cur.left == null && cur.right == null || pre != null &&(pre == cur.left || pre == cur.right)){
System.out.print(cur.data + " ");
stack.pop();
pre = cur;
}else{//若当前节点即不是叶子节点,而且也不是前一个访问节点的子节点,先将右孩子入栈,再将左孩子入栈
if(cur.right != null)
stack.push(cur.right);
if(cur.left != null)
stack.push(cur.left);
}
}
}
获取树的深度
//递归获取树的深度
public int getDepth(Node root){
if(root == null)
return 0;
int lDeep = getDepth(root.left);
int rDeep = getDepth(root.right);
return lDeep > rDeep ? lDeep + 1 : rDeep + 1;
}
//非递归获取树的深度
public int getDepthByQueue(Node root){
if(root == null)
return 0;
Queue<Node> queue = new LinkedList<>();
Node current = null;
queue.offer(root);
int cur, last;
int level = 0;
while (!queue.isEmpty()){
cur = 0;
last = queue.size(); //记录当前层需要遍历的节点个数
while (cur < last){
current = queue.poll();
cur++;
//当前节点的左右节点不为空的加入队列,等待下一层遍历
if(current.left != null)
queue.offer(current.left);
if(current.right != null)
queue.offer(current.right);
}
level++;//每遍历一遍层数加一
}
return level;
}
层序遍历
//层序遍历
//利用队列先进先出的特点依次将每一层的每一个节点的左右子节点加入队列,可以保持每一层输出的顺序
public void floorTravel(Node root){
Queue<Node> queue = new LinkedList<>();
queue.offer(root);
Node temp = null;
while (!queue.isEmpty()){
temp = queue.peek();
//将当前节点的左右孩子依次加入队列
if(temp.left != null)
queue.offer(temp.left);
if(temp.right != null)
queue.offer(temp.right);
System.out.print(queue.poll().data + " ");
}
}
交换每个节点的左右孩子
//递归实现交换子节点
public void exchangeChild(Node root){
if(root != null){
Node temp = root.left;
root.left = root.right;
root.right = temp;
exchangeChild(root.left);
exchangeChild(root.right);
}
}
//非递归实现交换左右子树
public void exchangeChildByQueue(Node root){
Queue<Node> queue = new LinkedList<>();
queue.offer(root);
Node temp;
while (!queue.isEmpty()){
Node left = null;
Node right = null;
temp = queue.poll();
//将存在的孩子节点加入队列进行下一层的交换
if(temp.left != null){
left = temp.left;
queue.offer(left);
}
if(temp.right != null){
right = temp.right;
queue.offer(right);
}
temp.left = right;
temp.right = left;
}
}
标签:Node,right,temp,二叉树,简单,创立,null,root,left 来源: https://blog.csdn.net/qq_41040289/article/details/98472466