1 / 39

22c: 21 Data Structures

22c: 21 Data Structures. Lecture on 03/13/2009. Outline. Infix to Postfix Conversion with Stack B-Trees. Infix to Postfix. Why do we need the stack?. a + b ab +. a + b. a. b. +. +. Example. Infix String : a+b*c-d. Postfix String : abc*+d-. b*c -> bc*. a+bc* -> abc*+.

Download Presentation

22c: 21 Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 22c: 21 Data Structures Lecture on 03/13/2009

  2. Outline • Infix to Postfix Conversion with Stack • B-Trees

  3. Infix to Postfix

  4. Why do we need the stack? a + b ab + a + b a b + +

  5. Example Infix String : a+b*c-d Postfix String : abc*+d- b*c -> bc* a+bc* -> abc*+ abc*+-d -> abc*+d-

  6. Algorithm • Initially the Stack is empty and our Postfix string has no characters. • Now, the first character scanned is 'a'. 'a' is added to the Postfix string. • The next character scanned is '+'. It being an operator, it is pushed to the stack.

  7. a +

  8. Algorithm • Next character scanned is 'b' which will be placed in the Postfix string. • Next character is '*' which is an operator. • Now, the top element of the stack is '+' which has lower precedence than '*', so '*' will be pushed to the stack.

  9. a b * +

  10. Algorithm • The next character is 'c' which is placed in the Postfix string. • Next character scanned is '-'. The topmost character in the stack is '*' which has a higher precedence than '-'.

  11. Algorithm • Thus '*' will be popped out from the stack and added to the Postfix string. • Even now the stack is not empty. • Now the topmost element of the stack is '+' which has equal priority to '-'. So pop the '+' from the stack and add it to the Postfix string. • The '-' will be pushed to the stack.

  12. a b c + * -

  13. Algorithm • Next character is 'd' which is added to Postfix string. • Now all characters have been scanned so we must pop the remaining elements from the stack and add it to the Postfix string.

  14. - d a a b c + *

  15. Algorithm • scan the Infix string from left to right. • Initialize an empty stack. • If the scanned character is an operand, add it to the Postfix string. • If the scanned character is an operator and if the stack is empty push the character to stack.

  16. Algorithm • If the scanned character is an operator and the stack is not empty, compare the precedence of the character with the element on top of the stack (topStack). • If topStack has higher precedence over the scanned character Pop the stack else Push the scanned character to stack.

  17. Algorithm • Repeat this step as long as stack is not empty and topStack has precedence over the character. • Repeat this step till all the characters are scanned.

  18. Algorithm • (After all characters are scanned, we have to add any character that the stack may have to the Postfix string.) • If stack is not empty add topStack to Postfix string and Pop the stack. • Repeat this step as long as stack is not empty. • Return the Postfix string.

  19. B-Trees • In computer science, a B-tree is a tree data structure that keeps data sorted and allows searches, insertions, and deletions in logarithmic time.

  20. B -Tree • Unlike self-balancing binary search trees, it is optimized for systems that read and write large blocks of data. It is most commonly used in databases and file systems. • Example?

  21. Secondary Storage Access • Binary Search Trees • AVL Trees • M-ary Search Trees • B- Trees Lesser the height of the tree, quicker is an element access

  22. 72 8 48 92 97 78 18 51 83 26 54 35 B-tree of order 5 41 66 87 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 31 32 35 36 37 38 39 66 68 69 70 72 73 74 76 78 79 81 83 84 85 41 42 44 46 48 49 50 51 52 53 54 56 58 59 87 89 90 92 93 95 97 98 99

  23. Properties • Data items are stored at the leaves • The non leaf nodes store up to M-1 keys • The root is either a leaf or has between two and M children

  24. Properties • All non leaf nodes (except the root) have at least M/2 up to M children. • All leaves are at the same depth and have at least L/2 up to L data items, for some L.

  25. Properties • Each node represents a disk block • So we choose M and L on the basis of the size of the items that are being stored

  26. Inserting an Element • Search if it already exists (no duplicates allowed)

  27. 72 8 48 92 97 78 18 51 83 26 54 35 Insert 57 41 66 87 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 31 32 35 36 37 38 39 66 68 69 70 72 73 74 76 78 79 81 83 84 85 41 42 44 46 48 49 50 51 52 53 54 56 57 58 59 87 89 90 92 93 95 97 98 99

  28. Insert 57 • We had to rearrange the data in the leaf. • Cost of doing this is negligible compared to a disk access.

  29. Insert 55 • Leaf is already full • Since we now have L+1 items we split them into two leaves • Distribute data evenly between leaves • Update parent

  30. 72 92 8 78 18 97 83 26 35 Insert 55 41 66 87 48 51 54 57 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 31 32 35 36 37 38 39 66 68 69 70 72 73 74 76 78 79 81 83 84 85 41 42 44 46 48 49 50 51 52 53 54 55 56 57 58 59 87 89 90 92 93 95 97 98 99

  31. Splitting • Splitting is time consuming, but it is a rare occurrence. • For every split, there are roughly L/2 non splits.

  32. Insert 40 • The leaf is full, so we need to split. • But there is no place to add an extra key. • So, we need to add an extra child under root. • But, root cannot have more than M=5 children!

  33. Insert 40 • Hence, the solution is to split parent. • Then, update all the values

  34. Insert 40 26 41 66 87 8 18 35 38 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 31 32 35 36 37 38 39 40

  35. Increasing height • When a non leaf node is split, its parent gains a child. • What if the parent is already full? • We continue splitting nodes up the tree till no splitting is required. • If we split the root, we have two roots, so add another single root at the top.

  36. Deletion • Find the item, and then remove it • What if the leaf already had minimum number of elements? • Adopt a neighbor item if the neighbor is not itself at its minimum • Otherwise, combine neighbors.

  37. Delete 99 26 41 66 87 8 18 35 38 72 78 83 92 97 87 89 90 92 93 95 97 98 99 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 31 32 35 36 37 38 39 40 66 68 69 70 72 73 74 76 78 79 81 83 84 85

  38. Delete 99 26 41 66 87 8 18 35 38 72 78 87 92 83 84 85 87 89 90 92 93 95 97 98 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 31 32 35 36 37 38 39 40 66 68 69 70 72 73 74 76 78 79 81

  39. Questions?

More Related