1 csc 211 data structures lecture 27 dr. iftikhar azim niaz [email protected] 1

85
1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz [email protected] 1

Upload: anna-roberts

Post on 03-Jan-2016

234 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

1

CSC 211Data Structures

Lecture 27

Dr. Iftikhar Azim [email protected]

1

Page 2: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

2

Last Lecture Summary Complete Binary Tree Heaps

Min-Heap and Max-Heap Heap Operations

Insertion and Deletion Applications of Heaps Priority Queue Heap Sort

Concept , Algorithm and Example Trace Complexity of Heap Sort Comparison with Quick and Merge Sort

2

Page 3: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

3

Objectives Overview Properties of Binary Trees Types of Binary trees

Expression Tree Threaded Binary Tree AVL Tree Red-Black Splay

Insertion and Deletion Operations Time Complexity B – trees

Page 4: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

4

Tree Terminology - Revisited Node is the main component of any tree structure. It

stores actual data and links to other nodes Parent of a node is the immediate predecessor of a

node. Also called the non terminal node or internal node Child. If the immediate predecessor of a node is the

parent of the node then all immediate successors of a node are called child. Child on the left is called the left child Child on the right is called the right child

Link is a pointer to a node in a tree. Also called branch or edge

Root is a specially designated node which has no parents

4

Page 5: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

5

Tree Terminology - Revisited Leaf is a node which is at the end and does not have any

child. Also called the terminal node or external node Level is the rank in hierarchy

Root node has level 0 If a node is at level l then its child is at level l + 1 and its parent is at

level l – 1. This is true for all nodes except root node Height. The maximum number of nodes that is possible in a

path starting from root node to a leaf node is called the height of the tree. Also called the depth h = lmax + 1, where h is the height and lmax is the maximum level of

the tree Degree is the maximum number of children that is possible

for a node Siblings are the nodes which have the same parent

5

Page 6: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

6

Binary Tree - Revisited A tree is a finite set of one or more modes such that

There is a specially designated node called the root The remaining nodes are partitioned into n (n > 0) disjoint

sets T1, T2……….. Tn, where each Ti ( i = 1, 2,…….., n) is a tree; T1, T2……….. Tn are called the sub-trees of the root

Binary tree is a special form of tree. It is more important and frequently used in various

applications of computer science It is defined as a finite set of nodes such that: T is empty (called the empty binary tree) or T contains a specially designated node called the root of T

and the remaining nodes of T form two disjoint binary tree T1 and T2 which are called left sub-tree and right sub-tree

Page 7: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

7

Binary Tree - Revisited A tree can never be empty but a binary tree may be

empty. In binary tree, a node may have at most two children

(i.e. tree having degree = 2) Full binary tree contains the maximum possible

number of nodes at all levels Complete binary tree

if all of its levels except possibly the last level have the maximum number of possible nodes and

all the nodes in the last level appear as far left as possible Skew Binary tree is a one where each level has only

one node and each parent has exactly one child

Page 8: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

8

Properties of Binary Tree Maximum number of nodes in any binary tree on level k

is n = 2k where k ≥ 0 Maximum number of nodes possible in a binary tree of

height h is n = 2h – 1 Minimum number of nodes possible in a binary tree of

height h is n = h (skew binary tree) For any non-empty binary tree, if n is the number of

nodes and e is the number of edges, then n = e - 1 For any non-empty binary tree T, if n0 is the number of

leaf nodes (degree = 0) and n2 is the number of internal nodes (degree = 2), then n0 = n2 + 1

The height of a complete binary tree with n number of nodes is log2 (n + 1)

Page 9: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

9

Type of Binary Tree Expression Tree Binary Search Tree Heap Tree Threaded Binary Tree Heighted Balance Tree (AVL Tree) Red Black Tree Splay Tree

Page 10: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

10

Expression Tree a specific application of a binary tree to

evaluate certain expressions Binary tree which stores an arithmetic

expression Leaves of expression tree are operands such

as constants or variables names and All internal nodes are the operators An expression tree is always a binary tree

because an arithmetic expression contains either binary operators or unary operators Hence an internal node has at most two children

Page 11: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

11

Expression Tree Two common types of expressions

Arithmetic and Boolean can represent expressions that contain both unary

and binary operators implemented as binary trees mainly because binary

trees allows you to quickly find what you are looking for.

Page 12: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

12

Expression Tree –Boolean Expression Binary boolean expression tree equivalent to ((true V false) Λ ¬false) V (true V false))

Page 13: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

13

Build Expression Tree - AlgorithmInput: an expression E with postfix notation appended with # as delimiter symbol

Output: expression tree corresponding to the given postfix notation

Data structure: linked structure of binary tree

Push() and Pop() operations are for a stack data structure

getToken() scans the postfix notation and returns the current token one at a time and then control automatically shifts to the next token

1. Token = getToken() // read first token from postfix expression

2. While (Token ≠ #) do // continue till the end of expression

3. If (token = operand) then // if the token is an expression

4. newNode = getNode(NODE) // get new node and initialize it

5. newNode→data = token

6. Push(newNode) // Push into the stack

7. Else

Page 14: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

14

Build Expression Tree - Algorithm8. If (Token = operator) then

9. ptr1 = POP()

10. ptr2 = POP()

11. newNode = getNode(NODE) // get new node

12. newNode→data = token

13. newNode→lchild = ptr2

14. newNode→rchild = ptr1

15. Push(newNode)

16. Endif

17. EndIf

18. Token = getToken()

19. EndWhile

20. Stop

Page 15: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

15

Expression Tree - Operations Two common operations

Traversing the expression tree and Evaluating the expression tree

Traversal operations are the same as the binary tree traversals

The evaluating the expression tree is also simple and easy to implement

Page 16: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

16

Binary Search Tree An application of Binary Trees Binary Search Tree (BST) or Ordered Binary

Tree has the property that All elements in the left subtree of a node N are less

than the contents of N and All elements in the right subtree of a node N are

greater than nor equal to the contents of N

Page 17: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

17

Binary Search Tree Common Operations

Searching Data Inserting Data Deleting Data Traversing the tree

Seen in detail in lecture 25

Page 18: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

18

Heap Trees All levels are full, except the last one, which is

left-filled A heap is a specialized tree-based data structure

that satisfies the heap property: If A is a parent node of B then key(A) is ordered

with respect to key(B) with the same ordering applying across the heap Either the keys of parent nodes are always greater

than or equal to those of the children and the highest key is in the root node (this kind of heap is called max heap) or

The keys of parent nodes are less than or equal to those of the children (min heap).

Page 19: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

19

Common Operations on Heap Trees create-heap: create an empty heap

(a variant) create-heap: create a heap out of given array of elements

find-max or find-min: find the maximum item of a max-heap or a minimum item of a min-heap, respectively

delete-max or delete-min: removing the root node of a max- or min-heap, respectively

increase-key or decrease-key: updating a key within a max- or min-heap, respectively

insert: adding a new key to the heap merge: joining two heaps to form a valid new heap

containing all the elements of both

Page 20: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

20

Threaded Binary Trees In a linked representation of binary trees with n

nodes (where n > 0), n + 1 link fields contain null entries

It highlights the fact that in a binary tree more than 50% of link fields are with null values, thereby wasting the memory space

A threaded binary tree defined as follows: "A binary tree is threaded by making all right child pointers that would normally be null

point to the inorder successor of the node, and all left child pointers that would normally be null

point to the inorder predecessor of the node

Page 21: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

21

Threaded Binary Tree Makes it possible to traverse the values in the

binary tree via a linear traversal that is more rapid than a recursive in-order traversal

It is also possible to discover the parent of a node from a threaded binary tree, without explicit use of parent pointers or a stack This can be useful where stack space is limited, or

where a stack of parent pointers is unavailable (for finding the parent pointer via Depth First Search)

Page 22: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

22

Threaded Binary Tree Consider a node k that has a right child r.

Then the left pointer of r must be either a child or a thread back to k. In the case that r has a left child, that left child must in turn have

either a left child of its own or a thread back to k, and so on for all successive left children.

So by following the chain of left pointers from r, we will eventually find a thread pointing back to k

The situation is symmetrically similar when q is the left child of p—we can follow q's right children to a thread pointing ahead to p

Page 23: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

23

Types of Threaded Binary Tree Single Threaded each node is

threaded towards either the inorder predecessor or successor

Double Threaded each node is

threaded towards both the inorder predecessor and successor

Page 24: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

24

Array of In-Order Traversal Threads are reference to the

predecessors and successors of the node according to an inorder traversal.

Inorder of the threaded tree is ABCDEFGHI, the predecessor of E is D, the successor of E is F.

Page 25: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

25

Double Threaded Binary Trees

Page 26: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

26

Advantages of Threaded Binary Trees The traversal operation is faster than that of its unthreaded version Because with threaded binary tree non-recursive implementation is possible

which can run faster and does not require the botheration of stock management.

We can efficiently determine the predecessor and successor nodes starting from any node A stack is required to provide upward pointing information in the tree whereas in

a threaded binary tree, without having to incur the overload of using a stack mechanism the same can be carried out with the threads.

Any node can be accessible from any other node Threads are usually more to upward whereas links are downward. Thus in a threaded tree, one can move in either direction and nodes are in fact

circularly linked. This is not possible in unthreaded counter part because there we can move only in downward direction starting form root.

Insertion into and deletions from a threaded tree are all although time consuming operations(since we have to manipulate both links and threads) but these are very easy to implement.

Page 27: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

27

Disadvantages of Threaded Binary Trees Slower tree creation, since threads need to be maintained. This can partly be alleviated by constructing the tree as an non-threaded tree, then threading it with a special library function

In theory, threaded trees need two extra bits per node to indicate whether each child pointer points to an ordinary node or the node's successor/predecessor node.

Page 28: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

28

Self- Balancing Binary Search Tree Also called Heighted Balance Trees

Binary search trees are useful for efficiently implementing dynamic set operations: Search, Successor, Predecessor, Minimum, Maximum, Insert, Delete in O(h) time, where h is the height of the tree

When the tree is balanced, that is, its height h = O(log n), the operations are indeed efficient.

However, the Insert and Delete alter the shape of the tree and can result in an unbalanced tree.

In the worst case, h = O(n) no better than a linked list

Page 29: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

29

Self- Balancing Binary Search Tree Find a method for keeping the tree always balanced

When an Insert or Delete operation causes an imbalance, we want to correct this in at most O(log n) time no complexity overhead.

Add a requirement on the height of sub-trees The most popular balanced tree data structures:

AVL trees: subtree height difference of at most 1 Red-Black trees: subtree height difference ≤ 2 (log n

+ 1) Splay trees: gg

Page 30: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

30

AVL Tree An AVL tree is a binary tree with one balance property: For any node in the tree, the height difference between

its left and right sub-trees is at most one; if at any time they differ by more than one, rebalancing is done to restore this property

Sh-1 Sh-2h-2h-1

hSh

x

Sh = Sh–1 + Sh–2 + 1 Size of tree:

AVL: Adelson-Velsky and Landis, 1962

Page 31: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

31

12

16

14

8

104

2 6

An AVL tree Not an AVL tree(nodes 8 and 12)

AVL Examples

12

8 16

144 10

2 6

1

Page 32: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

32

AVL Tree Properties What is the minimum and maximum height h

of an AVL tree with n nodes? Study the recursion: T(n) = T(n-1) + T(n-2) + 1 Minimum height: fill all levels except the last

oneroot

Tright Tleft

h-1

h = log n

h-1or h-2

Page 33: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

33

Maximal height of an AVL tree All levels have a difference of height of 1.

The smallest AVL tree of depth 1 has 1 node. The smallest AVL tree of depth 2 has 2 nodes. In general, Sh = Sh-1+ Sh-2+ 1 (S1 = 1; S2 = 2)

Sh-1Sh-2

h-2h-1

hSh

x

Page 34: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

34

Balancing AVL trees Before the operation, the tree is balanced.

After an insertion or deletion operation, the tree might become unbalanced. fix subtrees that became unbalanced.

The height of any subtree has changed by at most 1. Thus, if a node is not balanced, the difference between its children heights is 2.

Four possible cases with a height difference of 2

Page 35: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

35

Cases of Imbalance (1)

Case 1: The left subtree is higher than the right subtree because of the left subtree of the left child

k2

k1

BC

A

k1

k2

BA

C

Case 4: The right subtree is higher than the left subtree because of the right subtree of the right child

Page 36: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

36

Cases of Imbalance (2)

Case 2: The left subtree is higher than the right subtree because of the right subtree of the left child

Case 3: The right subtree is higher than the left subtree because of the left subtree of the right child

k2

k1

CA

B

k1

k2

CA

B

Page 37: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

37

Fixing Case 1: right rotation

The rotation takes O(1) time and restores the balance Insertion – The height of subtree A is increased. After the

rotation, the tree has the same height as before the insert. Deletion – The height of subtree C is decreased. After the

rotation, the tree has a lower height by 1 from before

k2

k1

AB

C

right rotation k2

k1

A B C

Page 38: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

38

Case 1: Insertion example

12

8 16

14104

62

1

k2

k1 C

A B

12

8

16

14

10

4

6

2

1

k2

k1

CA

B

Insert 1

Page 39: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

39

Fixing Case 4: left rotation

k1

k2

CB

A

left rotation

Analysis as in Case 1

k1

k2

CBA

Page 40: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

40

Case 4: Deletion example

5

7

9

4

delete 4left rotationk1

k2

7

95

k2

k1

5

7

9

k1

k2

Page 41: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

41

Fixing Case 2 – First try …

• A single rotation is not enough• A series of rotations on the subtree of k1 to

reduce Case 2 to Case 1!

k2

k1

BA

C

right rotationk2

k1

B

AC

Page 42: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

42

Fixing Case 2: right then left rotation

k3

k1

AC

k2

B1 B2

left rotation on k1

right rotation on k3

k3

k1

A

C

k2

B1

B2

k3k1

A C

k2

B1 B2

Page 43: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

43

Case 2: Insertion Example

12

8 16

14104

62

5

k3

k1

C

A B1

k2

left rotation on 4

k1

12

8 16

14106

4

5

k3

C

A B1

k2

2

right rotation on 8

12

6 16

1484

52 10

k3k1

CB1

k2

A

no B2

Page 44: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

44

Fixing Case 3: right then left rotation

k1

k3

CA

right rotation on k3

left rotation on k1

k2

B1 B2

Analysis as in Case 2

k3k1

A C

k2

B1 B2

Page 45: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

45

Insert and Delete operations (1) Insert/delete the element as in a regular binary search tree, and then re-balance.

Observation: only nodes on the path from the root to the node that was changed may become unbalanced.

Assume we go left: the right subtree was not changed, and stays balanced. This holds as we descend the tree.

Page 46: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

46

Insert and delete operations (2) After adding/deleting a leaf, go up, back to the root.

Re-balance every node on the way as necessary. The path is O(log n) long, and each node balance

takes O(1), thus the total time for every operation is O(log n).

For the insertion we can do better: when going up, after the first balance, the subtree that

was balanced has height as before, so all higher nodes are now balanced again.

We can find this node in the pass down to the leaf, so one pass is enough.

Page 47: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

47

AVL Trees – Time Complexity

Worst Case Average Case

Space O(n) O(n)

Search O(log2n) O(log2n)

Insert O(log2n) O(log2n)

Delete O(log2n) O(log2n)

Insertions and deletions may require the tree to be rebalanced by one or more tree rotations

Page 48: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

48

Red Black Trees Binary Search Trees should be balanced AVL Trees need 2 passes: top-down

insertion/deletion and bottom-up rebalancing Need recursive implementation

Red-Black Trees need 1 pass: top-down rebalancing and insertion/deletion Can be implemented iteratively, faster

Red-Black Trees have slightly weaker balance restrictions Less effort to maintain In practice, worst case is similar to AVL Trees

Page 49: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

49

Red Black Trees - Rules1. Every node is colored either red or black2. The root is black3. If a node is red, its children must be black

consecutive red nodes are disallowed4. Every path from a node to a null reference must

contain the same number of black nodes Convention: null nodes are black

Page 50: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

50

Red-Black Trees - Properties

• Each path must contain the same number of black nodes. (Rule #4)

• Consecutive red nodes are not allowed. (Rule #3)

• The longest path is at most twice the length of the shortest path

Page 51: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

51

Red-Black Trees - Properties

B = total black nodes from root to leaf

N = total all nodes H = height

)1log(2)1log(

)1log()1log(2

1

2)1log()1log(

22log2log

212

1212

2

2

2

NHN

NBN

BNNB

BB

N

N

BB

BB

BB

All operations guaranteed logarithmic!

Page 52: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

52

Red-Black Trees - Properties Height of a node: the number of edges in

the longest path to a leaf. Black-height bh(x) of a node x: the number

of black nodes (including NIL) on the path from x to a leaf, not counting x.

26

17 41

30 47

38 50

NIL NIL

NIL

NIL NIL NIL NIL

NIL

h = 4bh = 2

h = 3bh = 2

h = 2bh = 1

h = 1bh = 1

h = 1bh = 1

h = 2bh = 1 h = 1

bh = 1

Page 53: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

53

Red – Black Trees - Implementation For Insert and delete implementation code visit the following Website

https://en.wikipedia.org/wiki/Red-black_tree#Operations

Page 54: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

54

Red - Black Trees – Time Complexity

Worst Case Average Case

Space O(n) O(n)

Search O(log2n) O(log2n)

Insert O(log2n) O(log2n)

Delete O(log2n) O(log2n)

Insertions and deletions may require the tree to be rebalanced by one or more tree rotations

Page 55: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

55

Splay Trees A splay tree is a self-adjusting binary search

tree with the additional property that recently accessed elements are quick to access again

It performs basic operations such as insertion, look-up and removal in O(log n)

amortized time For many sequences of nonrandom operations,

splay trees perform better than other search trees, even when the specific pattern of the sequence is unknown

Page 56: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

56

Splaying Operation All normal operations on a binary search tree are

combined with one basic operation, called splaying Splaying the tree for a certain element rearranges

the tree so that the element is placed at the root of the tree

One way to do this is to first perform a standard binary tree search for the

element in question, and then use tree rotations in a specific fashion to bring

the element to the top Alternatively, a top-down algorithm can combine the

search and the tree reorganization into a single phase

Page 57: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

57

Splaying When a node x is accessed, a splay operation is performed on

x to move it to the root To perform a splay operation we carry out a sequence of splay

steps, each of which moves x closer to the root By performing a splay operation on the node of interest after

every access, the recently accessed nodes are kept near the root and the tree remains roughly balanced, so that we achieve the desired amortized time bounds.

Each particular step depends on three factors: Whether x is the left or right child of its parent node, p, whether p is the root or not, and if not whether p is the left or right child of its parent, g (the grandparent of x).

It is important to remember to set gg (the great-grandparent of x) to now point to x after any splay operation. If gg is null, then x obviously is now the root and must be updated as

such.

Page 58: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

58

Zig Step This step is done when p is the root The tree is rotated on the edge between x and p Zig steps exist to deal with the parity issue and

will be done only as the last step in a splay operation and only when x has odd depth at the beginning of the operation

Page 59: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

59

Zig-zig Step This step is done when p is not the root and x and p

are either both right children or are both left children Figure shows the case where x and p are both left

children The tree is rotated on the edge joining p with its parent

g, then rotated on the edge joining x with p.

Page 60: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

60

Zig-Zag Step This step is done when p is not the root and x

is a right child and p is a left child or vice versa The tree is rotated on the edge between x and

p, then rotated on the edge between x and its new parent g

Page 61: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

61

Splay tree: Insertion and Deletion Insertion: To insert a node x into a splay tree

First insert the node as with a normal BST Then splay the newly inserted node x to the top of the tree if there is a duplicate, the node holds the duplicate element is

splayed Deletion:

splay selected element to root disconnect left and right subtrees from root do one of:

splay max item in TL (then TL has no right child)

splay min item in TR (then TR has no left child) connect other subtree to empty child if the item to be deleted is not in the tree, the node last visited in the

search is splayed https://en.wikipedia.org/wiki/Splay_tree

Page 62: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

62

Splay Trees – Time Complexity

Worst Case Average Case

Space O(n) O(n)

SearchAmortized O(log2n) O(log2n)

InsertAmortized O(log2n) O(log2n)

DeleteAmortized O(log2n) O(log2n)

Insertions and deletions may require the tree to be rebalanced by one or more tree rotations

Page 63: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

63

B-Trees B-tree is a tree data structure that keeps data

sorted and allows searches, sequential access, insertions, and deletions in logarithmic time

B-tree is a generalization of a binary search tree in that a node can have more than two children As branching increases, depth decreases

Unlike self-balancing binary search trees, the B-tree is optimized for systems that read and write large blocks of data

It is commonly used in databases and file systems

Page 64: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

64

B - Trees In B-trees, internal (non-leaf) nodes can have a variable

number of child nodes within some pre-defined range When data are inserted or removed from a node, its

number of child nodes changes In order to maintain the pre-defined range, internal

nodes may be joined or split Because a range of child nodes is permitted

B-trees do not need re-balancing as frequently as other self-balancing search trees, but may waste some space, since nodes are not entirely full

The lower and upper bounds on the number of child nodes are typically fixed for a particular implementation

Page 65: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

65

B – Trees Definition A B-tree of order m is an m-way tree (i.e., a tree

where each node may have up to m children) in which:

1. the number of keys in each non-leaf node is one less than the number of its children and these keys partition the keys in the children in the fashion of a search tree

2. all leaves are on the same level

3. all non-leaf nodes except the root have at least m / 2 children

4. the root is either a leaf node, or it has from two to m children

5. a leaf node contains no more than m – 1 keys The number m should always be odd

Page 66: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

66

B – Tree Example

51 6242

6 12

26

55 60 7064 9045

1 2 4 7 8 13 15 18 25

27 29 46 48 53

A B-tree of order 5 containing 26 items

Note that all the leaves are at the same level

Page 67: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

67

Constructing a B – Tree Suppose we start with an empty B-tree and keys

arrive in the following order: 1 12 8 2 25 5 14 28 17 7 52 16 48 68 3 26 29 53 55 45

We want to construct a B-tree of order 5 The first four items go into the root:

To put the fifth item in the root would violate condition 5

Therefore, when 25 arrives, pick the middle key to make a new root

1 2 8 12

Page 68: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

68

Insertion in B – Tree Attempt to insert the new key into a leaf If this would result in that leaf becoming too big,

split the leaf into two, promoting the middle key to the leaf’s parent

If this would result in the parent becoming too big, split the parent into two, promoting the middle key

This strategy might have to be repeated all the way to the top

If necessary, the root is split in two and the middle key is promoted to a new root, making the tree one level higher

Page 69: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

69

Insert into B – Tree (Contd.)

1 2

8

12 25

6, 14, 28 get added to the leaf nodes:

1 2

8

12 146 25 28

Page 70: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

70

Insert into B – Tree (Contd.) Adding 17 to the right leaf node would over-fill it, so we take the middle key, promote it (to the root) and split the leaf

8 17

12 14 25 281 2 6

7, 52, 16, 48 get added to the leaf nodes

8 17

12 14 25 281 2 6 16 48 527

Page 71: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

71

Insert into B – Tree (Contd.) Adding 68 causes us to split the right most leaf, promoting 48 to the root, and adding 3 causes us to split the left most leaf, promoting 3 to the root; 26, 29, 53, 55 then go into the leaves

3 8 17 48

52 53 55 6825 26 28 291 2 6 7 12 14 16

Adding 45 causes a split of 25 26 28 29

and promoting 28 to the root then causes the root to split

Page 72: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

72

Insert into B – Tree (Contd.)

17

3 8 28 48

1 2 6 7 12 14 16 52 53 55 6825 26 29 45

Page 73: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

73

Removal from a B – Tree During insertion, the key always goes into a leaf. For deletion we wish to remove from a leaf There are three possible ways we can do this:

1 - If the key is already in a leaf node, and removing it doesn’t cause that leaf node to have too few keys, then simply remove the key to be deleted.

2 - If the key is not in a leaf then it is guaranteed (by the nature of a B-tree) that its predecessor or successor will be in a leaf -- in this case can we delete the key and promote the predecessor or successor key to the non-leaf deleted key’s position.

Page 74: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

74

Removal from a B – Tree (2) If (1) or (2) lead to a leaf node containing less than the

minimum number of keys then we have to look at the siblings immediately adjacent to the leaf in question: 3: if one of them has more than the min number of keys

then we can promote one of its keys to the parent and take the parent key into our lacking leaf

4: if neither of them has more than the min’ number of keys then the lacking leaf and one of its neighbours can be combined with their shared parent (the opposite of promoting a key) and the new leaf will have the correct number of keys; if this step leaves the parent with too few keys then we repeat the process up to the root itself, if required

Page 75: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

75

Type #1: Simple leaf deletion

12 29 52

2 7 9 15 22 56 69 7231 43

Delete 2: Since there are enoughkeys in the node, just delete it

Assuming a 5-wayB-Tree, as before...

Page 76: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

76

Type #2: Simple non-leaf deletion

12 29 52

7 9 15 22 56 69 7231 43

Delete 52

Borrow the predecessoror (in this case) successor

56

Page 77: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

77

Type #4: Too few keys in node and its siblings

12 29 56

7 9 15 22 69 7231 43

Delete 72Too few keys!

Join back together

Page 78: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

78

Type #4: Too few keys in node and its siblings

12 29

7 9 15 22 695631 43

Page 79: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

79

Type #3: Enough siblings

12 29

7 9 15 22 695631 43

Delete 22

Demote root key andpromote leaf key

Page 80: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

80

Type #3: Enough siblings

12

297 9 15

31

695643

Page 81: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

81

Analysis of B – Trees The maximum number of items in a B-tree of order

m and height h:root m – 1level 1 m(m – 1)level 2 m2(m – 1). . .level h mh(m – 1)

So, the total number of items is

(1 + m + m2 + m3 + … + mh)(m – 1) =

[(mh+1 – 1)/ (m – 1)] (m – 1) = mh+1 – 1 When m = 5 and h = 2 this gives 53 – 1 = 124

Page 82: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

82

Reasons for Using B – Trees When searching tables held on disc, the cost of each

disc transfer is high but doesn't depend much on the amount of data transferred, especially if consecutive items are transferred If we use a B-tree of order 101, say, we can transfer each

node in one disc read operation A B-tree of order 101 and height 3 can hold 1014 – 1 items

(approximately 100 million) and any item can be accessed with 3 disc reads (assuming we hold the root in memory)

If we take m = 3, we get a 2-3 tree, in which non-leaf nodes have two or three children (i.e., one or two keys) B-Trees are always balanced (since the leaves are all at the

same level), so 2-3 trees make a good type of balanced tree

Page 83: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

83

Comparing Trees Binary trees

Can become unbalanced and lose their good time complexity (big O)

AVL trees are strict binary trees that overcome the balance problem

Heaps remain balanced but only prioritise (not order) the keys

Multi-way trees B-Trees can be m-way, they can have any (odd) number

of children One B-Tree, the 2-3 (or 3-way) B-Tree, approximates a

permanently balanced binary tree, exchanging the AVL tree’s balancing operations for insertion and (more complex) deletion operations

Page 84: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

84

Last tree

The very last tree for this class

Page 85: 1 CSC 211 Data Structures Lecture 27 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

85

Summary Properties of Binary Trees Types of Binary trees

Expression Tree Threaded Binary Tree AVL Tree Red-Black Splay

Insertion and Deletion Operations Time Complexity B – trees