树: 高效的查找与搜索语义
O(log n)–树的高度
树的基础概念
1、非线性的数据结构,根朝上,叶朝下。
2、只有根节点没有前驱节点。
3、从根节点出发分出的M个子集,彼此不能相交
4、树的边个数为X,树中节点个数n,x=n-1;
5、节点和树的“度”:
节点的度:该节点中包含子树个数称为该节点的度
树的度:树中最大节点的度为该树的度
5、叶子节点:度为0的节点
6、非叶子节点:度不为0
7、节点的层次(高度):根节点为第一层,依次类推
树的高度:当前树中节点层次最大的即为树的高度
二叉树
1、什么是二叉树:
树中节点最大的度为2的树称为二叉树
2、性质:
1、在深度为K的二叉树中,最多存在2^k -1个节点
2、在第K层最多有2^(K-1)个节点
3、由于二叉树都满足节点个数n和边长x具备:x = n-1;
推论:在度为2的节点和度为0的节点有以下关系:n0 =n2 +1;即叶子节点总比非叶子节点多一个
完全二叉树
1、什么是完全二叉树?
满二叉树缺了一个右下角。
1、在完全二叉树中不存在只有右子树没有左子树的节点
2、若存在度为1的节点,这个节点必然只有左子树,且这个节点有且仅有一个。
2、完全二叉树的编号问题
①若根节点从1开始编号
②若根节点从0开始编号
二分搜索树(BST)
1、节点的值之间有一个大小关系:左子树值<根节点值<右子树值
2、查找一个元素就是二分查找
【中序遍历】:非递减序列。
平衡二叉树
1、该树中任意一个节点的左右子树高度差<=1
2、AVL ->严格平衡BST
3、RBTree -> “黑节点”严格平衡的BST
二叉树的遍历
【遍历:按照一定顺序访问这个集合的所有元素,做到不重复、不遗漏】
1、深度优先遍历(DFS):前序遍历、中序遍历、后序遍历
【栈(数组)实现】
①递归实现前中后序遍历:
public class MyBinaryTree<E> {
TreeBode<Character> root;
public void build() {
TreeBode<Character> node1 = new TreeBode('A');
TreeBode<Character> node2 = new TreeBode('B');
TreeBode<Character> node3 = new TreeBode('C');
TreeBode<Character> node4 = new TreeBode('D');
TreeBode<Character> node5 = new TreeBode('E');
TreeBode<Character> node6 = new TreeBode('F');
TreeBode<Character> node7 = new TreeBode('G');
TreeBode<Character> node8 = new TreeBode('H');
node1.left = node2;
node1.right = node3;
node2.left = node4;
node2.right = node5;
node5.left = node7;
node7.right = node8;
node3.right = node6;
root = node1;
}
/**
* 传入一个二叉树结点按照前序遍历
*
* @param root
*/
public void preOder(TreeBode root) {
if (root == null) {
return;
}
System.out.print(root.val + " ");
preOder(root.left);
preOder(root.right);
}
/**
* 传入一个二叉树头节点进行中序遍历输出
*
* @param root
*/
public void inOder(TreeBode root) {
if (root == null) {
return;
}
inOder(root.left);
System.out.print(root.val + " ");
inOder(root.right);
}
/**
* 传入一个二叉树头节点进行后序遍历输出
*
* @param root
*/
public void postOder(TreeBode root) {
if (root == null) {
return;
}
postOder(root.left);
postOder(root.right);
System.out.print(root.val + " ");
}
}
②深度优先遍历非递归写法
/**
* 实现前序遍历的非递归写法,借助栈
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> ret = new ArrayList<>();//接收和输出
if (root == null) {
return ret;
}
Deque<TreeNode> stack = new ArrayDeque<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
ret.add(node.val);
//因为用的栈所以先入栈右子树
if (node.right != null) {
stack.push(node.right);
}
if (node.left != null) {
stack.push(node.left);
}
}
return ret;
}
}
/**
* 实现二叉树中序遍历的非递归写法
*/
class Solution1 {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> ret = new ArrayList<>();
if (root == null) {
return ret;
}
Deque<TreeNode> stack = new ArrayDeque<>();
TreeNode cur = root;
while (cur != null || !stack.isEmpty()) {
//1、一直向左走到空
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
//2、cur已经为空,弹出栈顶就是第一个左子树为空的节点
cur = stack.pop();
ret.add(cur.val);
//继续访问右子树
cur = cur.right;
}
return ret;
}
}
/**
* 实现二叉树后序遍历的非递归写法
*/
class Solution3 {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> ret = new ArrayList<>();
if (root == null) {
return ret;
}
Deque<TreeNode> stack = new ArrayDeque<>();
TreeNode cur = root;
TreeNode prev = null;//上一个处理完的节点
while (cur != null || !stack.isEmpty()) {
//1、一直向左走
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
//2、检查栈顶元素情况
cur = stack.pop();
if (cur.right == null || cur.right == prev) {
//此时右子树为空或者已经处理完了,就可以添加到数组中
ret.add(cur.val);
//cur就是处理完的节点
prev =cur;
//将cur处理完后制空,否则会陷入死循环
cur = null;
} else {
//此时cur的右子树不为空,且没有被处理。访问右子树
stack.push(cur);
cur = cur.right;
}
}
return ret;
}
}
2、广度优先遍历(BFS):层序遍历
【队列(链表)实现】
/**
* 传入一个二叉树头节点进行层序遍历输出
*
* @param root
*/
public void levelOrder(TreeBode<E> root) {
Queue<TreeBode<E>> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int n = queue.size();//当前层有多少个数
for (int i = 0; i < n; i++) {
TreeBode<E> node = queue.poll();
System.out.print(node.val + " ");
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
}
}
3、二叉树完全性检验:
public boolean isCompleteTree(TreeNode root) {
Deque<TreeNode> queue = new LinkedList<>();
queue.offer(root);
boolean isSecondStep = false;
while (!queue.isEmpty()) {
TreeNode cur = queue.poll();
if (!isSecondStep) {//第一阶段
if (cur.left != null && cur.right != null) {
queue.offer(cur.left);
queue.offer(cur.right);
} else if (cur.left != null) {
//只没有右孩子节点
isSecondStep = true;
queue.offer(cur.left);
} else if (cur.right != null) {
//没有左孩子节点
return false;
}else {
//叶子节点
isSecondStep =true;
}
} else {
//第二阶段都得是叶子节点
if (cur.left != null || cur.right != null) {
return false;
}
}
}
return true;
}
4、求二叉树最大宽度
public class Num662 {
public int widthOfBinaryTree(TreeNode root) {
if (root == null) {
return 0;
}
LinkedList<TreeNode> ret = new LinkedList<>();
int maxWidth = 0;
ret.offer(root);
root.val = 0;
while (!ret.isEmpty()) {
int count = ret.size();
int width = ret.getLast().val - ret.getFirst().val + 1;
for (int i = 0; i < count; i++) {
TreeNode temp = ret.poll();
if (temp.left != null) {
ret.offer(temp.left);
temp.left.val = temp.val * 2 + 1;
}
if (temp.right != null) {
ret.offer(temp.right);
temp.right.val = temp.val * 2 + 2;
}
}
if (width > maxWidth) {
maxWidth = width;
}
}
return maxWidth;
}
}