0% found this document useful (0 votes)
42 views133 pages

DSA Chapter 06 (Trees)

Download as pptx, pdf, or txt
0% found this document useful (0 votes)
42 views133 pages

DSA Chapter 06 (Trees)

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 133

Data Structures and Algorithms

Lecture 10

Manish Aryal
Data Structures and Algorithms
Key Learning Points
 Recursion
 Divide and conquer
 Phenomena in which a procedure P calls itself directly
or indirectly
 Examples:
▪ Factorial
▪ Fibonacci Series
▪ Towers of Hanoi
 Recursion Tree
▪ Evaluation of Expressions
2
Trees
Contents
 Concepts
 Operations in Binary Tree
 Tree Traversal: pre-order, in-order and post-order
 Tree Search, insertion/deletion
 Height, level and depth of a tree
 AVL balanced trees and balancing algorithm
 The Huffman Algorithm
 B-tree
 Red Black Tree
Trees: Some Examples

 A tree represents a hierarchy, for example:


 Organizational structure of a company
Trees: Some Examples

 Table of contents of a book


Trees: Some Examples
Tree:Basic Concepts
 A tree consists of:
– nodes: finite set of elements
– branches: directed lines connecting the nodes
 For a node:
– degree: number of branches associated with the node
– indegree: number of branches towards the node
– outdegree: number of branches away from the node
 For a tree:
– root: node with indegree 0
– nodes different from the root must have indegree 1
7
Trees:Terminology
 Leaf: node with outdegree 0
 Internal node: not a root or a leaf
 Parent: node with outdegree greater than 0
 Child: node with indegree greater than 0
 Siblings: nodes with the same parent
 Path: sequence of adjacent nodes
 Ancestor: node in the path from the root to the node
 Descendent: node in a path from the node to a leaf
 Level: the node's distance from the root (at level 0)
 Height (Depth): the level of the leaf in the longest path
from the root plus 1
 Sub-tree: connected structure below the root
8
Trees: Terminology

 A is the root node


 B is the parent of D and E
 C is the sibling of B
 D and E are the children of B
 D, E, F, G, I are external nodes, or
leaves
 A, B, C, H are internal nodes
 The depth, level, or path length of E
is 2
 The height of the tree is 3
Trees: Representation
General tree
Computer

Case CPU 3.5" Disk CD-ROM

Controller ALU ROM

10
Trees: Representation {Contd..}
Indented list
Computer
Case
CPU
Controller
ALU
ROM
...
3.5" Disk
CD-ROM

11
Trees: Representation {Contd..}
Parenthetical listing
Computer (Case CPU (Controller ALU ROM ...) 3.5" Disk CD-ROM)

12
Trees: Viewed Recursively

A sub-tree is also a tree!!


Binary Trees
 Binary tree: tree with all internal nodes of degree 2
 Recursive View: a binary tree is either
 empty
 an internal node (the root) and two binary trees (left subtree
and right subtree)
Binary Trees: An Example
 Arithmetic expression
Binary Trees: Properties
 Height of binary trees:
Hmax = N
Hmin = log2N + 1
Nmin = H
Nmax = 2H - 1

16
Binary Trees: Properties {Contd..}

 Balance:
– Balance factor: B = HL - HR
– Balanced tree: balance factor is 0, -1, or 1
sub-trees are balanced

17
Binary Trees: Properties {Contd..}

 Completeness:
A
– Complete tree:
B C
Nmax = 2H - 1
D E F G
(last level is full)
A
– Nearly complete tree:
B C
Hmin = log2N + 1
D E
nodes in the last level are
on the left
18
Binary Trees: Structure
Node
leftSubTree <nodePointer>
data <dataType>
rightSubTree <nodePointer>
End Node

19
Binary Tree Traversal

 Each node is processed once and only once in


a predetermined sequence.
 Depth-First Traversal
 Breadth-First Traversal

20
Depth-First Traversal

1 2 3

2 3 1 3 1 2

PreOrder InOrder PostOrder

21
Depth-First Traversal:PreOrder

B E

C D F

A B C D E F

Processing order
22
Depth-First Traversal:PreOrder {Contd..}

A
A

B E
B E
C D F
C D F

A B C D E F Walking order

Processing order
23
Depth-First Traversal: PreOrder {Contd..}

Algorithm preOrder (val root <nodePointer>)


Traverses a binary tree in node-left-right sequence
Pre root is the entry node of a tree/subtree
Post each node has been processed in PreOrder
1 if (root is not null)
1 process (root)
2 preOrder (root -> leftSubTree)
3 preOrder (root -> rightSubTree)
4 return
End preOrder

24
Depth-First Traversal: InOrder

B E

C D F

C B D A E F

Processing order
25
Depth-First Traversal: InOrder {Contd..}

B E

B E
C D F

C D F

C BD A E F
Walking order

Processing order
26
Depth-First Traversal: InOrder {Contd..}
Algorithm inOrder (val root <nodePointer>)
Traverses a binary tree in left-node-right sequence
Pre root is the entry node of a tree/subtree
Post each node has been processed in InOrder
1 if (root is not null)
1 inOrder (root -> leftSubTree)
2 process (root)
3 inOrder (root -> rightSubTree)
4 return
End inOrder

27
Depth-First Traversal: InOrder {Contd..}

* d
a*b+c+d
a +

b c

28
Depth-First Traversal: InOrder {Contd..}

( + )

( * ) d
((a * (b + c)) +
( + ) d)
a

b c

29
Depth-First Traversal: PostOrder

B E

C D F

C D B F E A

Processing order
30
Depth-First Traversal: PostOrder {Contd..}

B E

B E
C D F

C D F

C D B F E A
Walking order

Processing order
31
Depth-First Traversal: PostOrder {Contd..}

Algorithm postOrder (val root <nodePointer>)


Traverses a binary tree in left-node-right sequence
Pre root is the entry node of a tree/subtree
Post each node has been processed in order
1 if (root is not null)
1 postOrder (root -> leftSubTree)
2 postOrder (root -> rightSubTree)
3 process (root)
4 return
End preOrder

32
Breadth-First Traversal: Level Order

A A

B E B E

C D F C D F

A Walking order
B E
C D F

Processing order
33
Data Structures and Algorithms

Lecture 11

Manish Aryal
Trees
Key learning Points
 Tree, Binary Tree
 In order to process the elements of a tree, we consider accessing the elements
in certain order
 Tree traversal is a tree operation that involves "visiting” (or" processing") all
the nodes in a tree.
 Depth First Traversal:
 Pre-order: Visit node first, pre-order all its subtrees from leftmost to
rightmost.
 Inorder: Inorder the node in left subtree and then visit the root following by
inorder traversal of all its right subtrees.
 Post-order: Post-order the node in left subtree and then post-order the
right subtrees followed by visit to the node.
 Breadth First traversal:
 Level-order: Visit root followed by its children from left to right and
followed by their children. So we go down the tree level by level.
Trees

 Operations in a Tree
 Binary search trees
 AVL trees

36
Trees: Operations in a Tree
Operations

 Basic Operations
 create()
 insert()
 delete()
 search()
 Primitive Operations
 maketree()
 setleft(), setright()
 isleft(p), isright(p)
 left(p), right(p), father(p) and brother(p)
37
Trees: Operations in a Tree {Contd..}
Creating a tree
 Initialization
struct Node
{
<datatype> data;
struct Node * left, *right;
};
 Create
Node * create()
{
Node *p=(Node *)malloc(sizeof(Node));
printf(“\nEnter Data:”);
scanf(“%d”, &p->data);
p->left=p->right=NULL;
}
38
Trees: Operations in a Tree {Contd..}
Insertion
 Nodes can be inserted into binary trees in between
two other nodes or added after an external node
 In binary trees, a node that is inserted is specified as
child to the node after which it is added
 Insertion can be made at
▪ External nodes
▪ Internal Nodes

39
Trees: Operations in a Tree {Contd..}
Insertion
 External nodes
 Say that the external node being added on to is node
A
 To add a new node after node A, A assigns the new
node as one of its children and the new node assigns
node A as its parent.

40
Trees: Operations in a Tree {Contd..}
Insert
 Internal nodes
 Insertion on internal nodes is slightly more complex than on
external nodes
 Say that the internal node is node A and that node B is the child
of A
 If the insertion is to insert a right child, then B is the right child
of A, and similarly with a left child insertion
▪ A assigns its child to the new node and the new node assigns its parent to
A
▪ Then the new node assigns its child to B and B assigns its parent as the
new node.

41
Trees: Operations in a Tree {Contd..}
Insert

42
Trees: Operations in a Tree {Contd..}
Delete
 Deletion is the process whereby a node is removed from
the tree.
 Only certain nodes in a binary tree can be removed
unambiguously
 Node with zero or one children
 Node with two children

43
Trees: Operations in a Tree {Contd..}
Delete
 Node with zero or one children
 Say that the node to delete is node A.
 If a node has no children, deletion is accomplished by
setting the child of A's parent to NULL and A's parent to
null
 If it has one child, set the parent of A's child to A's parent
and set the child of A's parent to A's child

44
Trees: Operations in a Tree {Contd..}
Delete
 Node with two children
 In a binary tree, a node with two children cannot be
deleted unambiguously
 However, in certain binary trees these nodes can be
deleted, including binary search trees

45
Trees: Operations in a Tree {Contd..}
Delete

46
Trees: Operations in a Tree {Contd..}
Search
 Using binary Search Tree

47
Trees: Operations in a Tree {Contd..}
Primitive Operations
 If p is pointer to node nd then,
 isleft(p) returns true if nd is a left son else returns false
 isright(p) returns true if nd is a right son else returns false
 left(p) returns pointer to left son of P
 right(p) returns pointer to right son of P
 father(p) returns pointer to father of P
 brother(p) returns pointer to sibling of P

48
Trees: Operations in a Tree {Contd..}
Primitive Operations
 The maketree() function allocates a node and sets it as the root
of a single node binary tree.
NODEPTR maketree(int x)
{
NODEPTR p;
p = create();
p->info = x;
p->left = NULL;
p->right = NULL;
return p;
}

49
Trees: Operations in a Tree {Contd..}
Primitive Operations
 The setleft() and setright() functions sets a node with content x as the
left son and right son of the node p respectively

void setleft(NODEPTR p, int x) void setright(NODEPTR p, int x)


{ {
if(p == NULL){ if(p == NULL){
printf(“void insertion\n”); printf(“void insertion\n”);
else if (p->left != NULL) else if (p->right != NULL)
printf(“invalid insertion\n”); printf(“invalid insertion\n”);
else else
p->left = maketree(x); p->right = maketree(x);
} }

50
Binary Search Trees

 All items in the left subtree < the root.


 All items in the right subtree >= the root.
 Each subtree is itself a binary search tree.

51
Binary Search Tree Traversals

preorder
23 18 12 20 44
23
35 52
18 44 postorder
12 20 18 35 52
12 20 35 52
44 23

inorder
12 18 20 23 35
44 52

52
Find Smallest Node

Algorithm findSmallestBST (val root <pointer>)


Finds the smallest node in a BST
Pre root is a pointer to a non-empty BST
Return address of smallest node 23
1 if (root -> left = null)
18 44
1 return (root)
2 return findSmallestBST (root -> left)
12 20 35 52
End findSmallestBST

53
Find Largest Node

Algorithm findLargestBST (val root <pointer>)


Finds the largest node in a BST
Pre root is a pointer to a non-empty BST
Return address of largest node 23
1 if (root -> right = null)
18 44
1 return (root)
2 return findLargestBST (root -> right)
12 20 35 52
End findLargestBST

54
BST Search
Algorithm searchBST (val root <pointer>, val arg <key>)
Searches a binary search tree for a given value
Pre root is a pointer to a non-empty BST
arg is the key value requested
Return the node address if the value is found; null otherwise
1 if (root = null)
1 return null
2 else if (arg = root -> key)
1 return root
3 else if (arg < root -> key)
23
1 searchBST (root -> left, arg)
4 else if (arg > root -> key)
1 searchBST (root -> right, arg) 18 44
5 else
1 return null
End searchBST 12 20 35 52

55
BST Insertion

Taking place at a 23

node having a
18 44
null branch
12 20 35 52

19
23 23

18 44 22 18 44

12 20 35 52 12 20 35 52

19 19 22

56
Iterative BST Insertion Algorithm

Algorithm insertBST (ref root <pointer>, val new <pointer>)


Inserts a new node into BST using iteration
Pre root is address of the root
new is address of the new node
Post new node inserted into the tree

57
Iterative BST Insertion Algorithm
1 if (root = null) 23
1 root = new
2 else
18 44
1 pWalk = root
2 loop (pWalk not null)
1 parent = pWalk 12 20 35 52
2 if (new -> key < pWalk -> key)
1 pWalk = pWalk -> left
3 else 19
1 pWalk = pWalk -> right
Location found for the new node
3 if (new -> key < parent -> key)
1 parent -> left = new
4 else
1 parent -> right = new
3 return
End insertBST

58
Recursive BST Insertion Algorithm

Algorithm addBST (ref root <pointer>, val new <pointer>)


Inserts a new node into BST using recursion
Pre root is address of the root
new is address of the new node
Post new node inserted into the tree

59
Recursive BST Insertion Algorithm
23
1 if (root = null)
1 root = new 18 44
2 else
1 if (new -> key < root -> key) 12 20 35 52
1 addBST (root -> left, new)
2 else 19 22
1 addBST (root -> right, new)
3 return
End addBST

60
BST Deletion

 Leaf node: set the deleted node's parent link


to null.
 Node having only right subtree: attach the
right subtree to the deleted node's parent.
 Node having only left subtree: attach the left
subtree to the deleted node's parent.

61
BST Deletion
Node having both subtrees

23 23

18 44 ? 44

12 20 35 52 12 20 35 52

19 22 19 22

62
BST Deletion
Node having both subtrees

23 23

18 44 12 44

12 20 35 52 12 20 35 52

19 22 19 22

Using largest node in the left


subtree
63
BST Deletion
Node having both subtrees

23 23

18 44 19 44

12 20 35 52 12 20 35 52

19 22 19 22

Using smallest node in the right


subtree
64
BST Deletion Algorithm
Algorithm deleteBST (ref root <pointer>, val dltKey <key>)
Deletes a node from a BST
Pre root is a pointer to a non-empty BST
dltKey is the key of the node to be deleted
Post node deleted & memory recycled
if dltKey not found, root unchanged
Return true if node deleted; false otherwise
1 if (root = null)
1 return false
2 if (dltKey < root -> data.key)
1 deleteBST (root -> left, dltKey)
3 else if (dltKey > root -> data.key)
1 deleteBST (root -> right, dltKey)
4 else
Deleted node found

65
BST Deletion Algorithm
4 else
Deleted node found
1 if (root -> left = null)
1 dltPtr = root
2 root = root -> right
3 recycle (dltPtr)
4 return true
2 else if (root -> right = null)
1 dltPtr = root
2 root = root -> left
3 recycle (dltPtr)
4 return true
3 else
1 dltPtr = root -> left
2 loop (dltPtr -> right not null)
1 dltPtr = dltPtr -> right
3 root -> data = dltPtr -> data
4 return deleteBST (root -> left, dltPtr -> data.key)
End deleteBST

66
AVL Trees

 The heights of the left subtree and the right


subtree differ by no more than one.
 The left and right subtrees are AVL trees
themselves.
(G.M. Adelson-Velskii and E.M. Landis, 1962 )

67
AVL Trees

23

18 44

12 20 35 52

8 14

68
Getting Unbalanced

18 18
insert 4
12 20 12 20

8 14 8 14

Left of Left

69
Getting Unbalanced

14 14
insert 44
12 20 12 20

18 23 18 23

44

Right of Right

70
Getting Unbalanced

18 18
insert 13
12 20 12 20

8 14 8 14

13

Right of Left

71
Getting Unbalanced

14 14
insert 19
12 20 12 20

18 44 18 44

19

Left of Right

72
Balancing Trees

20 20 18
insert rotate
18
12 18
right
12 20

12

Left of Left

73
Balancing Trees

18 18 rotate 12
insert right
12 20
4 12 20 8 18

8 14 8 14 4 14 20

Left of Left

74
Balancing Trees

12 12 18
insert rotate left
18
20 18 12 20

20

Right of Right

75
Balancing Trees

14 14 rotate 20
insert left
12 20
44 12 20 14 23

18 23 18 23 12 18 44

44

Right of Right

76
Balancing Trees

12 12 8
rotate rotate
4
left 8
right
4 12

8 4

Right of Left

77
Balancing Trees

18 rotate 18 rotate 14
left right
12 20 14 20 12 18

4 14 12 16 4 16 20

16 4

Right of Left

78
Balancing Trees

12 12 18
rotate rotate left
44
right
18 12 44

18 44

Left of Right

79
Balancing Trees

18 rotate 18 rotate 23
right left
12 44 12 23 18 44

23 52 20 44 12 20 52

20 52

Left of Right

80
AVL Node Structure
Node
key <key type>
18
data <data type>
leftSubTree <pointer> 12 44
rightSubTree <pointer>
bal <Bf> 23 52
End Node
20

81
AVL Insertion Algorithm

Algorithm insertAVL (ref root <pointer>, val newPtr <pointer>,


ref taller <boolean>)
Inserts a new node into an AVL tree using recursion
Pre root is address of the root
newPtr is address of the new node
Post taller = true indicating the tree's height has increased;
false ortherwise

82
AVL Insertion Algorithm
1 if (root = null)
1 taller = true
2 root = newPtr
2 else
1 if (newPtr -> key < root -> key)
1 insertAVL (root -> left, newPtr, taller)
2 if (taller)
1 if (root left-high)
1 leftBalance (root, taller)
2 else if (root right-high)
1 taller = false
2 root -> bal = EH
3 else
1 root -> bal = LH
2 else if (newPtr -> key > root -> key)

83
AVL Insertion Algorithm
2 else if (newPtr -> key > root -> key)
1 insertAVL (root -> right, newPtr, taller)
2 if (taller)
1 if (root right-high)
1 rightBalance (root, taller)
2 else if (root left-high)
1 taller = false
2 root -> bal = EH
3 else
1 root -> bal = RH
3 else
1 error ("Dupe data")
2 recycle (newPtr)
3 taller = false
3 return
End insertAVL

84
AVL Left Balance Algorithm

Algorithm leftBalance (ref root <pointer>, ref taller <pointer>)


Balances an AVL tree that is left heavy
Pre root is address of the root
taller = true
Post root has been updated (if necessary)
taller has been updated

85
AVL Left Balance Algorithm
1 leftTree = root -> left
2 if (leftTree left-high)
Case 1: Left of left. Single rotation required.
1 adjust balance factors
2 rotateRight (root)
3 taller = false

18 12

12 20 8 18

8 14 4 14 20

86
AVL Left Balance Algorithm
3 else
Case 2: Right of left. Double rotation required.
1 rightTree = leftTree -> right
2 adjust balance factors
3 rotateLeft (leftTree)
4 rotateRight (root)
5 taller = false
4 return
End leftBalance
18 18 14

12 20 14 20 12 18

4 14 12 16 4 16 20

16 4
87
Rotate Algorithms

Algorithm rotateRight (ref root <pointer>)


Exchanges pointers to rotate the tree right
Pre root is address of the root
Post Node rotated and root updated

88
Rotate Algorithms
1 tempPtr = root -> left root
2 root -> left = tempPtr -> right 18
3 tempPtr -> right = root tempPtr
4 root = tempPtr
14 20
5 return
End rotateRight
12 16

4
root root root
18 14 14
tempPtr
tempPtr
14 20 12 18 12 18

12 16 4 16 20 4 16 20

4 89
Rotate Algorithms
Algorithm rotateLeft (ref root <pointer>)
Exchanges pointers to rotate the tree left
Pre root is address of the root
Post Node rotated and root updated
1 tempPtr = root -> right
2 root -> right = tempPtr -> left
3 tempPtr -> left = root
4 root = tempPtr
5 return
End rotateLeft

90
AVL Deletion

12 12 Rotate left 18
delete 9
9 18 18 12 20

15 20 15 20 15

Right subtree is even-balanced

91
AVL Deletion

12 12 Rotate left 18
delete 9
9 18 18 12 20

20 20

Right subtree is not even-balanced

92
AVL Deletion Algorithm

Algorithm deleteAVL (ref root <pointer>, val deleteKey <key>,


ref shorter <boolean>)
Deletes a node from an AVL tree
Pre root is address of the root
deleteKey is the key of the node to be deleted
Post node deleted if found
shorter = true if the tree is shorter
Return success true if node deleted; false otherwise

93
AVL Deletion Algorithm
1 if (root null)
1 shorter = false
2 return success = false
2 if (deleteKey < root -> key)
1 success = deleteAVL (root -> left, deleteKey, shorter)
2 if (shorter)
1 deleteRightBalance (root, shorter)
3 return success
3 else if (deleteKey > root -> key)
1 success = deleteAVL (root -> right, deleteKey, shorter)
2 if (shorter)
1 deleteLeftBalance (root, shorter)
3 return success
4 else
Delete node found - Test for leaf node

94
AVL Deletion Algorithm
4 else
Delete node found - Test for node having a null subtree
1 deleteNode = root
2 if (no left subtree)
1 root = root -> right
2 shorter = true
3 recycle (deleteNode)
4 return success = true
3 else if (no right subtree)
1 root = root -> left
2 shorter = true
3 recycle (deleteNode)
4 return success = true
4 else
Delete node has two subtrees

95
AVL Deletion Algorithm
4 else
Delete node has two subtrees
1 exchPtr = root -> left
2 loop (exchPtr -> right not null)
1 exchPtr = exchPtr -> right
3 root -> data = exchPtr -> data
4 deleteAVL (root -> left, exchPtr -> data.key, shorter)
5 if (shorter)
1 deleteRightBalance (root, shorter)
6 return success = true
5 return
End deleteAVL

96
Delete Right Balance Algorithm

Algorithm deleteRightBalance (ref root <pointer>, ref shorter <pointer>)


Adjusts the balance factors, and balance the tree by rotating left if necessary
Pre tree is shorter
Post balance factors updated and balance restored
root updated
shorter updated
1 if (root left-high)
1 root -> bal = EH
2 else if (root even-high)
1 root -> bal = RH
2 shorter = false
3 else
Tree was right high already. Rotate left

97
Delete Right Balance Algorithm
3 else
Tree was right high already. Rotate left
1 rightTree = root -> right
2 if (rightTree left-high)
Double rotation required
1 leftTree = rightTree -> left
2 leftTree -> bal = EH
3 rotateRight (rightTree)
4 rotateLeft (root)
3 else
Single rotation required

98
Delete Right Balance Algorithm
Single rotation required
1 if (rightTree even-high)
1 root -> bal = RH
2 rightTree -> bal = LH
3 shorter = false
2 else
1 root -> bal = EH
2 rightTree -> bal = EH
3 rotateLeft (root)
4 return
End deleteRightBalance

99
Multiway Trees

 Tree whose outdegree is not restricted to 2


while retaining the general properties of
binary search trees.

100
M-Way Search Trees
 Each node has m - 1 data entries and m subtree
pointers.
 The key values in a subtree

– >= the key of the left data entry


– < the key of the right data entry.
K1 K2 K3

keys < K1 K1<= keys < K2 K2<= keys < K3 K3<= keys
101
M-Way Search Trees

50 100 150

35 45 85 95 125 135 175

60 70 90 110 120

75

102
M-Way Node Structure

entry
key <key type>
key data data <data type>
rightPtr <pointer>
end entry

node
num firstPtr <pointer>
entries ...
numEntries <integer>
entries <array[1 .. m-1] of entry>
end node

103
B-Trees
 M-way trees are unbalanced.
 Bayer, R. & McCreight, E. (1970) created
B-Trees.

104
B-Trees
 A B-tree is an m-way tree with the following
additional properties:
 The root is either a leaf or has at least 2 and at
most m subtrees.
 All internal nodes have at least [m/2] and at most
m subtrees.
 A leaf node has at least [m/2] - 1 and at most m -
1 entries.
 All leaf nodes are at the same level.
105
B-Trees

42

16 20 58 76 81 93

11 14 17 18 19 21 22 23 24 45 52 63 65 74 78 79 85 87 94 97

m=5

106
B-Tree Insertion

 Insert the new entry into a leaf node.


 If the leaf node is overflow, then split it and
insert its median entry into its parent.
 If the internal node is overflow, do the same
thing
 If the root is overflow, then create a new root
containing the median entry.

107
B-Tree Insertion
Insert 78 78

Insert 21 21 78

Insert 14, 11 11 14 21 78

21
Insert 97
11 14 21 78 97 11 14 78 97
overflow
Insert 85, 74, 63
21 21 78

11 14 63 74 78 85 97 11 14 63 74 85 97
overflow
108
B-Tree Insertion
Insert 45, 42, 57
21 78 21 57 78

11 14 42 45 57 63 74 85 97 11 14 42 45 63 74 85 97
overflow
Insert 20, 16, 19
21 57 78 16 21 57 78

11 14 16 19 20 63 74 85 97 11 14 19 20 63 74 85 97
overflow 42 45 42 45

Insert 52, 30, 21 42

16 21 57 78 16 21 57 78

11 14 19 20 63 74 85 97 11 14 21 30 45 52 85 97

21 30 42 45 52 19 20 63 74

overflow 109
B-Tree Insertion
Algorithm BTreeInsert (val root <pointer>, val data <record>)
Inserts data into B-tree. Equal keys placed on right branch.
Pre root is a pointer to the B-tree. May be null.
Post data inserted.
Return pointer to B-tree root.
1 higher = insertNode(root, data, upEntry)
2 if (higher true)
Tree has grown. Create new root.
1 allocate (newPtr)
2 newPtr -> entries[1] = upEntry
3 newPtr -> firstPtr = root
4 newPtr -> numEntries = 1
5 root = newPtr
3 return root
End BTreeInsert
110
B-Tree Insertion
Algorithm insertNode (val root <pointer>, val data <record>,
ref upEntry <entry>)
Recursively searches tree to locate leaf for data. If node overflow, inserts median
key's data into parent.
Pre root is a pointer to tree or subtree. May be null.
Post data inserted.
upEntry is overflow entry to be inserted into parent.
Return tree taller <boolean>.
1 if (root null)
1 upEntry.data = data
2 upEntry.rightPtr = null
3 taller = true
2 else

111
B-Tree Insertion
2 else
1 entryNdx = searchNode (root, data.key)
2 if (entryNdx > 0)
1 subTree = root -> entries[entryNdx].rightPtr
3 else
1 subTree = root -> firstPtr
4 taller = insertNode(subTree, data, upEntry)
5 if (taller)
1 if (node full)
1 splitNode(root, entryNdx, upEntry)
2 taller = true
2 else
1 insertEntry (root, entryNdx, upEntry)
2 taller = false
3 root -> numEntries = root -> numEntries + 1
3 return taller
End insertNode
112
B-Tree Insertion
Algorithm searchNode (val nodePtr <pointer>, val target <key>)
Search B-tree node for data entry containing key <= target.
Pre nodePtr is pointer to non-null node.
target is key to be located.
Return index to entry with key <= target.
0 if key < first entry in node
1 if (target < nodePtr -> entry[1].data.key)
1 walker = 0
2 else
1 walker = nodePtr -> numEntries
2 loop (target < nodePtr -> entries[walker].data.key)
1 walker = walker - 1
3 return walker
End searchNode

113
B-Tree Insertion
Algorithm splitNode (val node <pointer>, val entryNdx <index>,
ref upEntry <entry>)
Node has overflowed. Split node.
Pre node is pointer to node that overflowed.
entryNdx contains index location of parent.
upEntry contains entry being inserted into split
node.
Post upEntry now contains entry to be inserted into parent.
1 minEntries = minimum number of entries
2 allocate (rightPtr)
Build right subtree node
3 if (entryNdx <= minEntries)
1 fromNdx = minEntries + 1
4 else

114
B-Tree Insertion
4 else
1 fromNdx = minEntries + 2
5 toNdx = 1
6 rightPtr -> numEntries = node -> numEntries - minEntries
7 medianNdx = minEntries + 1
8 loop (fromNdx <= node -> numEntries)
1 rightPtr -> entries[toNdx] = node -> entries[fromNdx]
2 fromNdx = fromNdx + 1
3 toNdx = toNdx + 1
9 node -> numEntries = node -> numEntries - rightPtr -> numEntries
10 if (entryNdx < minEntries)
1 insertEntry (node, entryNdx, upEntry)
11 else

115
B-Tree Insertion
11 else
1 insertEntry (rightPtr, entryNdx - minEntries, upEntry)
2 node -> numEntries = node -> numEntries - 1
3 rightPtr -> numEntries = rightPtr -> numEntries + 1
Build entry for parent
12 upEntry.data = node -> entries[medianNdx].data
13 upEntry.rightPtr = rightPtr
14 rightPtr -> firstPtr = node -> entries[minEntries + 1]. rightPtr
15 return
End splitNode

116
B-Tree Insertion
Algorithm insertEntry (val node <pointer>, val entryNdx <index>,
val newEntry <entry>)
Inserts one entry into a node by shifting nodes to make room.
Pre node is pointer to node to contain data.
newEntry contains data to be inserted.
entryNdx is index to location for new data.
Post data have been inserted in sequence.
1 shifter = node -> numEntries + 1
2 loop (shifter > entryNdx + 1)
1 node -> entries[shifter] = node -> entries[shifter - 1]
2 shifter = shifter - 1
3 node -> entries[shifter] = newEntry
4 node -> numEntries = node -> numEntries + 1
5 return
End insertEntry
117
B-Tree Deletion

 It must take place at a leaf node.


 If the data to be deleted are not in a leaf
node, then replace that entry by the largest
entry on its left subtree.

118
B-Tree Deletion
Delete
78
63 63

11 14 21 74 78 85 11 14 21 74 85

Delete
63
63 21

11 14 21 74 85 11 14 74 85

119
B-Tree Deletion
Delete
85
21 21

11 14 74 85 11 14 74

underflow
(node has fewer than
the min num of
Delete entries)
21
21 14

11 14 74 85 11 74 85

120
Balance
Borrow from right ... 21 ...
when the right
Original 14 42 45 63
sibling of the
node underflow node
has more than
21 min num of
Rotate entries
parent data 14 21 42 45 63
down
42
Rotate data
to parent
14 21 42 45 63

42
Shift
entries left 14 21 45 63

121
Balance
Borrow from left ... 78 ...
when the left
sibling of the
Original 45 63 74 85
underflow node
node
has more than
... 78 ...
min num of
Shift
entries
entries 45 63 74 85
right 12 42

... 78 ...
Rotate 8 9 14 45 63
parent data 45 63 74 78 85
down

... 74 ...
Rotate data
up 45 63 78 85

122
Combine
 when both left and right sibling nodes of the underflow nodes have min
num of entries
12 42 12

8 9 14 45 63 8 9 14 42 45 63
 choose one of its sibling
 move the separator down to the underflow node
12

8 9 14 42 45 63

 combine the underflow node with the chosen sibling


 if the parent node is underflow, repeat this combination until the root.

123
Combine (cont’d)
7

1 4 12

   8 9 14 42 45 63

1 4 7 12

   8 9 14 42 45 63

124
B-Tree Traversal

21 58

11 14 19 20 42 45 63 74 87

125
B-Tree Traversal
Algorithm BTreeTraversal (val root <pointer>)
Processes tree using inorder traversal
Pre root is a pointer to B-tree
Post Every entry has been processed in order
1 scanCount = 0
2 ptr = root -> firstPtr
3 loop (scanCount <= root -> numEntries)
1 if (ptr not null)
1 BTreeTraversal (ptr)
2 scanCount = scanCount + 1
3 if (scanCount <= root -> numEntries)
1 process (root -> entries[scanCount].data)
2 ptr = root -> entries[scanCount].rightPtr
4 return
End BTreeTraversal
126
B-Tree Search
Algorithm BTreeSearch (val root <pointer>, val target <key>,
ref node <pointer>, ref entryNo
<index>)
Recursively searches a B-tree for the target key
Pre root is a pointer to a tree or subtree
target is the data to be located
Post if found --
node is pointer to located node
entryNo is entry within node
if not found --
node is null and entryNo is zero
Return found <boolean>

127
B-Tree Search
1 if (empty tree)
1 node = null
2 entryNo = 0
3 found = false
2 else
1 if (target < first entry)
1 return BTreeSearch (root -> firstPtr, target, node, entryNo)
2 else
1 entryNo = root -> numEntries
2 loop (target < root -> entries[entryNo].data.key)
1 entryNo = entryNo - 1
3 if (target = root -> entries[entryNo].data.key)
1 found = true
2 node = root
4 else
1 return BTreeSearch (root -> entries[entryNo].rightPtr, target,
node, entryNo)
4 return found
End BTreeTraversal
128
Red Black Tree
A red-black tree is a binary search tree with one extra attribute
for each node: the colour, which is either red or black
 We also need to keep track of the parent of each node, so that a
red-black tree's node structure would be
Struct red_black_node
{
enum color { red, black };
void *item;
struct red_black_node *left, *right, *parent;
}
 The NULL nodes which terminate the tree are considered to be
the leaves and are coloured black
A red–black tree is a type of self-balancing binary search tree

129
Red Black Tree {Contd..}
 A red-black tree is a binary search tree which has the following red-
black properties:
 Every node is either red or black
 Every leaf (NULL) is black
 If a node is red, then both its children can black
 Every simple path from a node to a descendant leaf contains the same number
of black nodes

130
Huffman Algorithm
 Finds the minimum length bit string which
can be used to encode a string of symbols
 One application is text compression
 What's the smallest number of bits (hence
the minimum size of file) we can use to store
an arbitrary piece of text?
 Huffman's scheme uses a table of frequency
of occurrence for each symbol (or character)
in the input

131
Huffman Algorithm {Contd..}
 An encoding for each
character is found by
following the tree from the
route to the character in
the leaf
 the encoding is the string of
symbols on each branch For example:
String Encoding
followed TEA 10 00 010
SEA 011 00 010
TEN 10 00 110
132
Huffman Algorithm {Contd..}
 Initial data sorted by frequency
 A B C D E F
Combine the two lowest frequencies, F and E, to form a
45 13 12 16 9 5
sub-tree of weight 14
 Move it into its correct place F E C B D A
 Again combine the two lowest frequencies, C and B, to 5 9 12 13 16 45
form a sub-tree of weight 25
 Move it into its correct place
 Now the sub-tree with weight, 14, and D are combined
to make a tree of weight, 30
 Move it to its correct place
 Now the two lowest weights are held by the "25" and
"30" sub-trees, so combine them to make one of
weight, 55
 Move it after the A
 Combine the A and the "55" sub-tree to produce the
final tree and assign o to left paths and 1 to right paths
133

You might also like