B树:
B-Tree 被称为自平衡树,因为它的节点在中序遍历中排序。在 B 树中,一个节点可以有两个以上的子节点。 B 树的高度为 logM N(其中“M”是树的顺序,N 是节点数)。每次更新时都会自动调整高度。在 B 树中,数据按特定顺序排序,最低值在左侧,最高值在右侧。在 B 树中插入数据或键比二叉树更复杂。
B-Tree必须满足一些条件:
- B-tree 的所有叶子节点必须在同一层。
- 在 B 树的叶子节点之上,应该没有空的子树。
- B-树的高度应尽可能低。
B+树
B+ 树通过仅在树的叶节点存储数据指针来消除用于索引的 B 树的缺点。因此,B+树的叶节点结构与B树的内部节点结构大不相同。这里需要注意的是,由于数据指针只存在于叶节点,所以叶节点必须将所有的键值连同它们对应的数据指针存储到磁盘文件块中,以便访问它们。此外,叶节点被链接以提供对记录的有序访问。因此,叶节点形成索引的第一级,内部节点形成多级索引的其他级别。叶子节点的一些关键值也出现在内部节点中,简单地充当控制记录搜索的媒介。
我们来看看B树和B+树的区别:
S.NO | B tree | B+ tree |
---|---|---|
1. | All internal and leaf nodes have data pointers | Only leaf nodes have data pointers |
2. | Since all keys are not available at leaf, search often takes more time. | All keys are at leaf nodes, hence search is faster and accurate.. |
3. | No duplicate of keys is maintained in the tree. | Duplicate of keys are maintained and all nodes are present at leaf. |
4. | Insertion takes more time and it is not predictable sometimes. | Insertion is easier and the results are always the same. |
5. | Deletion of internal node is very complex and tree has to undergo lot of transformations. | Deletion of any node is easy because all node are found at leaf. |
6. | Leaf nodes are not stored as structural linked list. | Leaf nodes are stored as structural linked list. |
7. | No redundant search keys are present.. | Redundant search keys may be present.. |
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。