8. Trees
A tree structure usually consists of a parent node and several child nodes. Its query and addition and deletion efficiency are very high. Any multi-fork tree can be converted into the form of a binary tree, so the study of binary trees does not lose generality.
-
Binary tree : each node can only have at most 2 nodes;
-
Full binary tree : all leaf nodes are at the same level;
-
Complete binary tree : all leaf nodes correspond one-to-one with nodes numbered 1-n in the corresponding full binary tree;
The traversal method of the binary tree: (the output order of the parent node can determine the traversal method)
-
Preorder traversal: first output the parent node, and then preorder traverse the left subtree and right subtree;
-
Inorder traversal: traverse the left subtree in inorder first, then output the parent node, and then traverse the right subtree in inorder;
-
Post-order traversal: traverse the right subtree in sequence, then traverse the left sub-tree post-order, and then output the parent node;
Among them, there are two ways of traversal: recursion and iteration. The DFS algorithm is used to implement pre-middle-post-order traversal; the BFS algorithm is used to implement layer-order traversal.
8.1, binary tree and related operations
Node composition: there is only one root node:MyNode root
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {
}
TreeNode(int val) {
this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
The implementation of traversal : introduce pre-order traversal, and post-order traversal is similar.
/**
* 结点方法:先序遍历
*/
public void preTraverse(){
//先输出当前结点
System.out.println(this);
//递归遍历左子树
if(this.getLeft()!=null){
this.left.preTraverse();
}
//递归遍历右子树
if(this.getRight()!=null){
this.right.preTraverse();
}
}
/**
* 二叉树方法:先序遍历
*/
public void preTraverse(){
if(root!=null){
root.preTraverse();
}else {
System.out.println("二叉树为空!无法遍历!");
}
}
Iterative traversal:
/**
* 统一一下
* @param root
* @return
*/
//前序
public static List<Integer> preOrder(TreeNode root){
List<Integer> list = new ArrayList();
Stack<TreeNode> stack = new Stack();
TreeNode cur = root;
while(cur!=null || !stack.isEmpty()){
//一直往左压入栈
while(cur!=null){
list.add(cur.val);
stack.push(cur);
cur = cur.left;
}
//弹一个出来从
cur = stack.pop();
cur = cur.right;
}
return list;
}
//中序
public List<Integer> inorderTraversal(TreeNode root) {
if(root == null){
return new ArrayList();
}
List<Integer> list = new ArrayList();
Stack<TreeNode> stack = new Stack();
TreeNode cur = root;
while(cur != null || !stack.isEmpty()){
while(cur!=null){
stack.push(cur);
cur = cur.left;
}
cur = stack.pop();
list.add(cur.val);
cur = cur.right;
}
return list;
}
//后序遍历,非递归
public static List<Integer> postOrder(TreeNode root){
Stack<TreeNode> stack = new Stack<>();
List<Integer> list = new ArrayList<>();
TreeNode cur = root;
TreeNode p = null;//用来记录上一节点
while(!stack.isEmpty() || cur != null){
while(cur != null){
stack.push(cur);
cur = cur.left;
}
cur = stack.peek();
// 后序遍历的过程中在遍历完左子树跟右子树cur都会回到根结点。所以当前不管是从左子树还是右子树回到根结点都不应该再操作了,应该退回上层。
// 如果是从右边再返回根结点,应该回到上层。
//主要就是判断出来的是不是右子树,是的话就可以把根节点=加入到list了
if(cur.right == null || cur.right == p){
list.add(cur.val);
stack.pop();
p = cur;
cur = null;
}else{
cur = cur.right;
}
}
return list;
}
Level-order traversal of a binary tree:
public List<List<Integer>> levelOrder(TreeNode root) {
// BFS算法
List<List<Integer>> res = new ArrayList();
if(root == null) return res;
Queue<TreeNode> queue = new LinkedList();
// 队列中存放节点
queue.offer(root);
// 控制纵向
while(!queue.isEmpty()){
int size = queue.size();
List<Integer> list = new ArrayList();
for(int i=0;i<size;i++){
TreeNode curr = queue.poll();
list.add(curr.val);
if(curr.left!=null){
queue.add(curr.left);
}
if(curr.right!=null){
queue.add(curr.right);
}
}
res.add(list);
}
return res;
}
Lookup implementation:
/**
* 中序查找指定结点
* @param no
* @return
*/
public MyNode inOrderFind(int no){
MyNode node = null;
//递归遍历左子树
if(this.left!=null){
node = this.left.inOrderFind(no);
}
//左遍历结束后,查看是否找到,不为空即找到
if(node!=null){
return node;
}
if(this.no==no){
return this;
}
//否则右序遍历
if(this.right!=null){
node = this.right.inOrderFind(no);
}
return node;
}
/**
* 中序查找
* @param no
* @return
*/
public MyNode inOrderFind(int no){
if(root!=null){
return root.inOrderFind(no);
}else {
return null;
}
}
The implementation of delete node:
Convention: If the deleted node is a leaf node, it will be deleted directly, and if it is not a leaf node, the node and all its child nodes will be deleted.
/**
* 删除结点
* @param no
*/
public void deleteNode(int no) {
if (root != null) {
//如果当前结点为待删除结点,直接删除
if (root.getNo() == no) {
root=null;
return;
}
root.deleteNode(no);
}else {
System.out.println("二叉树为空!不能删除!");
}
}
/**
* 删除结点
* 思路:找到当前结点的子结点是否为需要删除结点,是,直接置空,否则,向左递归删除,然后向右递归
* @param no
*/
public void deleteNode(int no){
if(this.left!=null && this.left.no==no){
this.left=null;
return;
}
if(this.right!=null&& this.right.no==no){
this.right=null;
return;
}
if(this.left!= null){
this.left.deleteNode(no);
}
if(this.right!= null){
this.right.deleteNode(no);
}
}