0% found this document useful (0 votes)
6 views

Dsa24 10

The document discusses B-trees and heaps. It provides an overview of B-trees, including their properties and complexity analysis. It also covers heaps, complete and nearly complete trees, and heap operations like heap-up and heap-down.

Uploaded by

parmarnupur44
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Dsa24 10

The document discusses B-trees and heaps. It provides an overview of B-trees, including their properties and complexity analysis. It also covers heaps, complete and nearly complete trees, and heap operations like heap-up and heap-down.

Uploaded by

parmarnupur44
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Lecture 10

B-Trees and Heaps


2

Review of tree concepts


§ Review of binary search trees
§ Tree: a graph (network) without circular paths
§ Binary tree: an extended linked list with two links in each node
§ Binary search tree: an ordered binary tree
§ AVL trees:
§ Balanced binary search tree
§ Balance factor
§ Causes of unbalance: left-of-left, left-of-right, right-of-left, right-
of-right
§ Rebalancing
3

Tree implementations
template <class Type> 20 30 60 80 x
struct LinkedListNode {
Type info; 25 50 75

LinkedListNode<Type> *link;
};

template <class Type>


struct BinaryTreeNode {
Type info;
BinaryTreeNode<Type> *llink;
BinaryTreeNode<Type> *rlink;
};

template<class Type>
struct AVLTreeNode { -1
Type info;
AVLTreeNode *llink; 1 1
AVLTreeNode *rlink; -1 0
int bfactor; 0
}; 0
4

Single Rotation (right) for left of left


• Left of left • Right of right
Node* rotateRight (Node* root) Node* rotateLeft (Node* root) {
{ tempPtr = root->right;
tempPtr = root->left; root->right = tempPtr-> left;
root->left = tempPtr->right; tempPtr->left = root;
tempPtr->right = root; return tempPtr;
return tempPtr; }
}

• Right of left • Left of right


Node* leftBalance (Node* root) { Node* rightBalance (Node* root) {
leftTree = root->left; rightTree = root->right;
root->left = rotateLeft(leftTree); root->right = rotateRight(rightTree);
return rotateRight(root); return rotateLeft(root);
} }
5

Complexity of AVL tree operations


§ If an AVL tree has n nodes, the search, insertion,
deletion take O(log n) time. Thus, an AVL tree provides
high efficiency for all the common data-storage
operations.

Operations Unordered Ordered Unordered AVL Tree


array array Linked list
Search O(n) O(log n) O(n) O(log n)
Insert O(1) O(n) O(1) O(log n)
Delete O(n) O(n) O(n) O(log n)

n 10 100 1000 10,000 100,000 1,000,000 1,000,000,000


log2n 3 7 10 13 17 20 30
6

Content of this lecture


§ B-Tree
§ M-way tree
§ B-Tree
§ Heap
§ Complete and nearly complete tree
§ Definition of heap tree
§ Array representation of a heap
§ Heap operations: heap-up and heap-down
§ Shortest path in a graph
M-Way Tree 63 Binary
§ An AVL tree reduces search space by half each pointers
time. Can we do even better?
§ m-way search tree (M-Way tree of order m): a 4-way search
tree node
§ Each node has at most m children and at most m-1
3 data items
items of data each with a unique key
§ The keys in each node are in ascending order 63 70 73
§ The keys in the first i children are smaller than the
ith key while the keys in the last m-i children are 4 pointers
larger than the ith key.
d1 d2 … di-1 … dm-1

§ All subtrees, if any, are m-way trees.

m >= 2 A BST is a 2-way


search tree.
8

A 4-way tree or M-way tree of order 4


50 60 80
Structure of a node

30 35 58 59 63 70 73 80 3 data items

63 70 73
52 54 61 62

4 pointers
57

55 56 Each node contains


at most 3 keys in a
4-way tree!
9

A node of 4-way tree

template <class Type>


struct BinaryTreeNode {
Type info;
BinaryTreeNode<Type> *llink;
BinaryTreeNode<Type> *rlink;
Binary tree
};
template <class Type, int BTreeOrder>
struct BTreeNode {
Type list[BTreeOrder - 1];
BTreeNode *children[BTreeOrder]; M-Way tree of
int recCount; //record count order BTreeOrder
};
10

B-Trees
§ M-Way trees are not necessarily balanced thus not necessarily
computationally efficient.
§ B-Tree is going to be a balanced M-Way tree.
§ Floor function, ⌊x⌋, is the greatest integer less than or equal to x.
§ Ceiling function, ⌈x⌉, is the smallest integer greater than or equal to
x.
§ ⌊x⌋ ≥ ⌈x⌉-1
For example,
⌊2.4⌋ = 2
⌈2.4⌉ = 3
while ⌊2⌋ = ⌈2⌉ = 2
11

B-Trees
§ B-tree of order m is a m-way search tree with the following properties:
§ All leaves are on the same level; By no means similar to AVL tree
§ The root has at least 2 children if it is not a leaf;
§ All internal nodes except the root have at least ⌈m/2⌉ children
(⌈m/2⌉-1 data items).

The root
The idea is to fill
Internal nodes a node as full as
possible.
Leaves

A B-tree of order 4
Developed in 1971 at Boeing Research Labs by Bayer & McCreight
12

B-Trees

For order 5 B-
tree, maximum
children of a
node is 5 and
maximum data
entries is 4,
always.

If not a leaf
B-tree of order 5: ⌈5/2⌉ = 3
• Root: minimum children 2 & minimum data entry 1
• Internal: minimum children 3 & minimum entries 2
• Leaves: no minimum requirement
13

B-tree insertion
§ Compare the new data key with keys on the tree until
reach a leaf and temporarily insert the new data into the
leaf
§ If the leaf node has been overflowed (data items = m),
push the median entry to its parent node and split the
current node into 2 nodes, each containing half the data
(one node contains ⌊m/2⌋ and the other at least ⌊m/2⌋)
§ If the parent node is also overflowed, further push up until
reach the root. If the root node is also full, the median
entry becomes the new root (tree grows higher)
Median entry means its value is in the middle
among all other items.
14
B-tree insertion
Insertion sort: 11, 21, 14, 78, 97

B-Tree of
Order 5

Insert a sequence of data with keys into a B-Tree of Order 5:


11, 21, 14, 78, 97, 85, 74, 63, 45, 42,57, 20,16,19,52,30,22
15

B-tree insertion

22

22

22

Insert a sequence of data with keys:


11, 21, 14, 78, 97, 85, 74, 63, 45, 42,57, 20,16,19,52,30,22
16

B-tree insertion

Insert sequence:
11, 21, 14, 78, 97, 85, 74, 63, 45, 42,57, 20,16,19,52,30,22

into a B-Tree of order 4

https://www.cs.usfca.edu/~galles/visualization/BTree.html

B-tree of order 4: ⌈4/2⌉ = 2


• Root: minimum children 2 & maximum entries 3
• Internal: minimum children 2 & minimum entries 1
• Leave: maximum entries 3
17

Complexity of B-tree operations


§ If a B-tree has n nodes, the search, insertion, deletion
take O(log n) time, which in theory the same as AVL
tree in big-O notation but in practice can be more
efficient.

Operations Unordered Ordered Linked AVL Tree B-Tree


array array list
Search O(n) O(log n) O(n) O(log n) O(log n)
Insert O(1) O(n) O(1) O(log n) O(log n)
Delete O(n) O(n) O(n) O(log n) O(log n)

log21,000,000,000 = 30
18

Content of this lecture


§ B-Tree
§ M-way tree
§ B-Tree
§ Heap
§ Complete and nearly complete tree
§ Definition of heap tree
§ Array representation of a heap
§ Heap operations: heap-up and heap-down
19

Complete and nearly complete tree


n Complete binary tree: a tree has the maximum of
entries (nodes) for its height;
n Nearly complete tree: has the minimum height and all
nodes in the last level are placed on the left

Complete
& nearly
complete
trees
20

Heap definition Min heap is similar

§ A heap (max heap) is a binary tree with the following


properties:
§ The tree is complete or nearly complete (shape property)
§ The key value of root node of the tree is greater than or
equal to the key value in each of its descendants (order
property)
§ All the subtrees are also heaps

Differences between BST and heap:


BST: Ordered but less balanced
Heap: More balanced but less ordered
£ £
21

Valid Heaps

A heap is not ordered


22

Invalid Heaps

(e) An AVL tree but not a heap (rules 1&2)


23

Heap Data Structure Bread-first traversal

§ The shape property of a


heap makes it possible to
use an array to
implement a heap
§ The data in the array
corresponds to the
breadth-first traversal.

Conceptually
we still think it
as a tree
24

Heap operations: insertion and deletion


§ Insert a node to a heap
§ Add the node to the end of the heap
§ Perform a reheapUP operation: re-establishes heap by moving
that node up to its correct location
§ Delete the biggest node from a heap
§ Remove the node from the top of the heap and move the last
element of the heap to the top temporally
§ Perform a reheapDown operation: re-establishes heap by
moving data in the root down to its correct location in the heap

Note that we are only interested in


deleting the biggest node from a
heap.
25

ReheapUp operation

To insert 25 into the


heap, add it at the
end first

O(log2 n)
26

ReheapDown operation
42

The last element was


moved to the root after
removed the top element.
10

Compare to these two

O(log2n)
27

Heap Applications: priority queue


A heap is ready to
implement a priority
queue: the node with
highest priority is always
on the top of the heap and
is the first node to be
removed from the heap
n
§ enqueue: insert the
node into the heap
§ dequeue: remove n
the top node for the
heap
28

Summary
§ Binary tree: each node has at most two children
§ Binary search tree: ordered binary tree
§ AVL tree: balanced binary search tree
§ M-way tree: generated BST.
§ B-tree: balanced m-way tree
§ Heap: binary tree that is complete or nearly
complete with the highest key on the root and
subtrees are also heaps.
Shortest Path: Dijkstra’s algorithm
§ Find the shortest paths from a given vertex to all
other vertices by building a spanning tree:
§ Start with tree containing only the starting vertex
§ For each vertex that is not in the tree but adjacent to a
vertex of the tree, mark the total path weight (i.e.,
distance) from the tree root to this vertex on each edge
that link the vertex. Select the vertex and edge with the
minimum distance and insert them into the tree.
§ Repeat the above step until all vertices are in the tree
Shortest Path Algorithm

Determine
shortest paths
Shortest Path Algorithm

Determine
shortest path
Shortest Path Algorithm - implementation
sw:6 sw:max sw:5 sw:6 sw:5 sw:6
sw:max sw:max

sw:0 sw:0 sw:0 sw:max

sw:3 sw:max sw:3 sw:7 sw:3 sw:7


v=C minWeight = 3 v=B minWeight = 5
T F F F F F T F T F F F T T T F F F
sw:5 sw:6 sw:5 sw:6 sw:5 sw:6

sw:0
sw:9
sw:0 sw:9 sw:0 sw:9

sw:3 sw:7 sw:3 sw:7 sw:3 sw:7

v=D minWeight = 6 v=E minWeight = 7 v=F minWeight = 9

T T T T F F T T T T T F T T T T T T
33

Dijkastra’s Shortest Path Algorithm


void shortestPath(int source) {
for (int j = 0; j < gSize; j++)
smallestWeight[j] = weights[source][j];//DBL_MAX for not adjacent
bool *weightFound = new bool[gSize];//Record generated spanning tree
for (int j = 0; j < gSize; j++) weightFound[j] = false;
weightFound[source] = true;
smallestWeight[source] = 0;

for (int i = 0; i < gSize - 1; i++) {


double minWeight = DBL_MAX;//The new shortest distance to source
int v; //Record new added node
for (int j = 0; j < gSize; j++)
if (!weightFound[j])
if (smallestWeight[j] < minWeight) {
v = j;
minWeight = smallestWeight[v];
}
weightFound[v] = true; //Add the vertex into the spanning tree
for (int j = 0; j < gSize; j++)//update shortest weight
if (!weightFound[j])
if (minWeight + weights[v][j] < smallestWeight[j])
smallestWeight[j] = minWeight + weights[v][j];
}} //end shortestPath
34

Reading

§ Read textbook Chapter 11


§ Read the code for heap implementation
§ Check online AVL-tree, B-Tree and Heap
visualization to fully understand how they work
§ If you haven’t start to work on assignment 2, do it
right now.

You might also like