- 前序遍历:根结点 —-> 左子树 —-> 右子树
- 中序遍历:左子树—-> 根结点 —-> 右子树
- 后序遍历:左子树 —-> 右子树 —-> 根结点
- 层次遍历:仅仅需按层次遍历就可以
比如。求以下二叉树的各种遍历
前序遍历:1 2 4 5 7 8 3 6
中序遍历:4 2 7 5 8 1 3 6
后序遍历:4 7 8 5 2 6 3 1
层次遍历:1 2 3 4 5 6 7 8
广度优先搜索BFS(层序遍历)队列-先进先出
深度优先遍历DFS(前序遍历)-回溯递归
B树和B+树的区别、还有hash索引的区别?
1) B树每个节点都存储了key和data,B+树的data只存储在叶子节点上。
节点不存储data,就可以存储更多的key,使得树变矮,查询操作效率更高,执行的越快。
2) 树的所有叶子节点构成了一个有序列表,可以按照关键码的次序遍历全部记录。
由于数据顺序排列并相连,所以便于区间查找和搜索。而B树需要每一层的递归遍历。
为什么不使用哈希索引:
哈希索引的绝对优势是在没有出现哈希碰撞时查询时间复杂度是O(1),查询速度非常快,但他有很多缺点;
不支持范围查询
不支持索引完成排序
B树(为磁盘而生)
B树(英语:B-tree)是一种自平衡的树,能够保持数据有序。这种数据结构能够让查找数据、顺序访问、插入数据及删除的动作,都在对数时间内完成。
与自平衡二叉查找树不同,B树适用于读写相对大的数据块的存储系统,例如磁盘。B树减少定位记录时所经历的中间过程,从而加快存取速度。B树这种数据结构可以用来描述外部存储。这种数据结构常被应用在数据库和文件系统的实现上。
B+树
也是多路平衡查找树,其与B树的区别主要在于:
B树中每个节点(包括叶节点和非叶节点)都存储真实的数据,B+树中只有叶子节点存储真实的数据,非叶节点只存储键。在MySQL中,这里所说的真实数据,可能是行的全部数据(如Innodb的聚簇索引),也可能只是行的主键(如Innodb的辅助索引),或者是行所在的地址(如MyIsam的非聚簇索引)。
B树中一条记录只会出现一次,不会重复出现,而B+树的键则可能重复重现——一定会在叶节点出现,也可能在非叶节点重复出现。
B+树的叶节点之间通过双向链表链接。
B树中的非叶节点,记录数比子节点个数少1;而B+树中记录数与子节点个数相同。
平衡二叉树&红黑树
平衡二叉树
查找、插入和删除在平均和最坏情况下的时间复杂度都是O ( log n )
由于旋转的耗时,AVL树在删除数据时效率很低;在删除操作较多时,维护平衡所需的代价可能高于其带来的好处,因此AVL实际使用并不广泛。
# 平衡二叉树AVLclass TreeNode(object):def __init__(self,val):self.val = valself.left = Noneself.right = Noneself.height = 1# AVL tree classclass AVL_Tree(object):def get_height(self, root):if not root:return 0return root.heightdef get_balance(self, root):"""balace factor of tree"""if not root:return 0return self.get_height(root.left) - self.get_height(root.right)def right_rotate(self, root):new_root = root.leftT3 = new_root.rightnew_root.right = rootroot.left = T3# update heightsroot.height = 1 + max(self.get_height(root.left), self.get_height(root.right))new_root.height = 1 + max(self.get_height(new_root.left), self.get_height(new_root.right))return new_rootdef left_rotate(self, root):new_root = root.rightT2 = new_root.left# rotationnew_root.left = rootroot.right = T2# update heightsroot.height = 1 + max(self.get_height(root.left), self.get_height(root.right))new_root.height = 1 + max(self.get_height(new_root.left), self.get_height(new_root.right))return new_rootdef insert(self, root, key):# step 1. perform normal BSTif root:print(root.val)if not root:return TreeNode(key)elif key < root.val:root.left = self.insert(root.left, key)else:root.right = self.insert(root.right, key)# step 2. update the height of the ancestor noderoot.height = 1 + max(self.get_height(root.left), self.get_height(root.right))# step 3. get the balance factorbalance = self.get_balance(root)# step 4. If the node is unbalanced, then try out the 4 cases# case 1. left leftif balance > 1 and key < root.left.val:return self.right_rotate(root)#case 2. right rightif balance < -1 and key > root.right.val:return self.left_rotate(root)# case 3. left rightif balance > 1 and key > root.left.val:root.left = self.left_rotate(root.left)return self.right_rotate(root)# case 4. right leftif balance < -1 and key < root.right.val:root.right = self.right_rotate(root.right)return self.left_rotate(root)return rootdef preorder(self, root, result=[]):"""Parent - left - right"""if not root:return resultresult.append(root.val)result = self.preorder(root.left, result)result = self.preorder(root.right, result)return result
红黑树
红黑树(英语:Red–black tree)是一种自平衡二叉查找树,是在计算机科学中用到的一种数据结构,典型用途是实现关联数组。它可以在 O ( log n ) {\displaystyle {\text{O}}(\log n)}O(logn)时间内完成查找、插入和删除,这里的 n 是树中元素的数目。

