Dsa24 10
Dsa24 10
Tree implementations
template <class Type> 20 30 60 80 x
struct LinkedListNode {
Type info; 25 50 75
LinkedListNode<Type> *link;
};
template<class Type>
struct AVLTreeNode { -1
Type info;
AVLTreeNode *llink; 1 1
AVLTreeNode *rlink; -1 0
int bfactor; 0
}; 0
4
30 35 58 59 63 70 73 80 3 data items
63 70 73
52 54 61 62
4 pointers
57
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
B-tree insertion
22
22
22
B-tree insertion
Insert sequence:
11, 21, 14, 78, 97, 85, 74, 63, 45, 42,57, 20,16,19,52,30,22
https://www.cs.usfca.edu/~galles/visualization/BTree.html
log21,000,000,000 = 30
18
Complete
& nearly
complete
trees
20
Valid Heaps
Invalid Heaps
Conceptually
we still think it
as a tree
24
ReheapUp operation
O(log2 n)
26
ReheapDown operation
42
O(log2n)
27
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:9
sw:0 sw:9 sw:0 sw:9
T T T T F F T T T T T F T T T T T T
33
Reading