Algorithms. Algorithms 3.3 BALANCED SEARCH TREES. 2 3 search trees red black BSTs ROBERT SEDGEWICK KEVIN WAYNE.

Size: px
Start display at page:

Download "Algorithms. Algorithms 3.3 BALANCED SEARCH TREES. 2 3 search trees red black BSTs ROBERT SEDGEWICK KEVIN WAYNE."

Transcription

1 lgorithms OBT DGWICK KVIN WYN 3.3 BLNCD C T 2 3 search trees red black BTs lgorithms F O U T D I T I O N OBT DGWICK KVIN WYN Last updated on 10/11/16 9:22 M

2 BT: ordered symbol table operations summary sequential search binary search BT search N log N h insert N N h min / max N 1 h h = height of BT (proportional to log N if keys inserted in random order) floor / ceiling N log N h rank N log N h select N 1 h ordered iteration N log N N N order of growth of running time of ordered symbol table operations 2

3 Many BTs correspond to same set of keys. Number of compares for search/insert = 1 + depth of node. Bottom line. Tree shape depends on order of insertion. 3 Tree shape X C best case C X typical case X C worst case

4 ymbol table review implementation guarantee search insert delete search hit average case insert delete ordered ops? key interface sequential search (unordered list) binary search (ordered array) N N N N N N equals() log N N N log N N N compareto() BT N N N log N log N N compareto() goal log N log N log N log N log N log N compareto() Challenge. Guarantee performance. This lecture. 2 3 trees and left-leaning red black BTs. Textbook: B-trees. 4

5 3.3 BLNCD C T 2 3 search trees red black BTs lgorithms OBT DGWICK KVIN WYN

6 2 3 tree llow 1 or 2 keys per node. 2-node: one key, two children. 3-node: two keys, three children. ymmetric order. In order traversal yields keys in ascending order. Perfect balance. very path from root to null link has same length. M how to maintain? 3-node 2-node smaller than J larger than J C L P X between and J null link 6

7 2 3 tree demo earch. Compare search key against keys in node. Find interval containing search key. Follow associated link (recursively). search for M J C L P X 7

8 2 3 tree demo: search earch. Compare search key against keys in node. Find interval containing search key. Follow associated link (recursively). search for is less than M (go left) M J C L P X

9 2 3 tree demo: search earch. Compare search key against keys in node. Find interval containing search key. Follow associated link (recursively). search for is between and J (go middle) M J C L P X

10 2 3 tree demo: search earch. Compare search key against keys in node. Find interval containing search key. Follow associated link (recursively). search for M J C L P X found (search hit)

11 2 3 tree demo: search earch. Compare search key against keys in node. Find interval containing search key. Follow associated link (recursively). search for B B is less than M (go left) B M J C L P X

12 2 3 tree demo: search earch. Compare search key against keys in node. Find interval containing search key. Follow associated link (recursively). search for B B is less than (go left) M B J C L P X

13 2 3 tree demo: search earch. Compare search key against keys in node. Find interval containing search key. Follow associated link (recursively). search for B M J B is between and C (go middle) B C L P X

14 2 3 tree demo: search earch. Compare search key against keys in node. Find interval containing search key. Follow associated link (recursively). search for B M J C L P X B link is null (search miss)

15 2 3 tree demo: insertion Insert into a 2-node at bottom. earch for key, as usual. eplace 2-node with 3-node. insert K K is less than M (go left) K M J C L P X

16 2 3 tree demo: insertion Insert into a 2-node at bottom. earch for key, as usual. eplace 2-node with 3-node. insert K M K is greater than J (go right) K J C L P X

17 2 3 tree demo: insertion Insert into a 2-node at bottom. earch for key, as usual. eplace 2-node with 3-node. insert K M J C K L P X search ends here

18 2 3 tree demo: insertion Insert into a 2-node at bottom. earch for key, as usual. eplace 2-node with 3-node. insert K M J C K L P X replace 2-node with 3-node containing K

19 2 3 tree demo: insertion Insert into a 2-node at bottom. earch for key, as usual. eplace 2-node with 3-node. insert K M J C KLL P X

20 2 3 tree demo: insertion Insert into a 3-node at bottom. dd new key to 3-node to create temporary 4-node. Move middle key in 4-node into parent. insert Z Z is greater than M (go right) M Z J C K L P X

21 2 3 tree demo: insertion Insert into a 3-node at bottom. dd new key to 3-node to create temporary 4-node. Move middle key in 4-node into parent. insert Z M Z is greater than (go right) J Z C K L P X

22 2 3 tree demo: insertion Insert into a 3-node at bottom. dd new key to 3-node to create temporary 4-node. Move middle key in 4-node into parent. insert Z M J C K L P X Z search ends here

23 2 3 tree demo: insertion Insert into a 3-node at bottom. dd new key to 3-node to create temporary 4-node. Move middle key in 4-node into parent. insert Z M J C K L P X Z replace 3-node with temporary 4-node containing Z

24 2 3 tree demo: insertion Insert into a 3-node at bottom. dd new key to 3-node to create temporary 4-node. Move middle key in 4-node into parent. insert Z M J C K L P X Z

25 2 3 tree demo: insertion Insert into a 3-node at bottom. dd new key to 3-node to create temporary 4-node. Move middle key in 4-node into parent. insert Z M J C K L P X Z split 4-node into two 2-nodes (pass middle key to parent)

26 2 3 tree demo: insertion Insert into a 3-node at bottom. dd new key to 3-node to create temporary 4-node. Move middle key in 4-node into parent. insert Z M J X C K L P Z

27 2 3 tree demo: insertion Insert into a 3-node at bottom. dd new key to 3-node to create temporary 4-node. Move middle key in 4-node into parent. insert Z M J X C K L P Z

28 2 3 tree demo: insertion Insert into a 3-node at bottom. dd new key to 3-node to create temporary 4-node. Move middle key in 4-node into parent. epeat up the tree, as necessary. If you reach the root and it's a 4-node, split it into three 2-nodes. insert L C P L X convert 3-node into 4-node

29 2 3 tree demo: insertion Insert into a 3-node at bottom. dd new key to 3-node to create temporary 4-node. Move middle key in 4-node into parent. epeat up the tree, as necessary. If you reach the root and it's a 4-node, split it into three 2-nodes. insert L C L P X

30 2 3 tree demo: insertion Insert into a 3-node at bottom. dd new key to 3-node to create temporary 4-node. Move middle key in 4-node into parent. epeat up the tree, as necessary. If you reach the root and it's a 4-node, split it into three 2-nodes. insert L C L P X split 4-node (move L to parent)

31 2 3 tree demo: insertion Insert into a 3-node at bottom. dd new key to 3-node to create temporary 4-node. Move middle key in 4-node into parent. epeat up the tree, as necessary. If you reach the root and it's a 4-node, split it into three 2-nodes. insert L L C P X

32 2 3 tree demo: insertion Insert into a 3-node at bottom. dd new key to 3-node to create temporary 4-node. Move middle key in 4-node into parent. epeat up the tree, as necessary. If you reach the root and it's a 4-node, split it into three 2-nodes. insert L split 4-node (move L to parent) L C P X

33 2 3 tree demo: insertion Insert into a 3-node at bottom. dd new key to 3-node to create temporary 4-node. Move middle key in 4-node into parent. epeat up the tree, as necessary. If you reach the root and it's a 4-node, split it into three 2-nodes. height of tree increases by 1 insert L L C P X

34 2 3 tree demo: insertion Insert into a 3-node at bottom. dd new key to 3-node to create temporary 4-node. Move middle key in 4-node into parent. epeat up the tree, as necessary. If you reach the root and it's a 4-node, split it into three 2-nodes. insert L L C P X

35 2 3 tree construction demo insert 35

36 2 3 tree demo: construction 2 3 tree

37 2 3 tree demo: construction insert convert 2-node into 3-node

38 2 3 tree demo: construction insert

39 2 3 tree demo: construction 2 3 tree

40 2 3 tree demo: construction insert convert 3-node into 4-node

41 2 3 tree demo: construction insert

42 2 3 tree demo: construction insert split 4-node (move to parent)

43 2 3 tree demo: construction insert

44 2 3 tree demo: construction 2 3 tree

45 2 3 tree demo: construction insert convert 2-node into 3-node

46 2 3 tree demo: construction insert

47 2 3 tree demo: construction 2 3 tree

48 2 3 tree demo: construction insert C C convert 2-node into 3-node

49 2 3 tree demo: construction insert C C

50 2 3 tree demo: construction 2 3 tree C

51 2 3 tree demo: construction insert C convert 3-node into 4-node

52 2 3 tree demo: construction insert C

53 2 3 tree demo: construction insert C split 4-node (move to parent)

54 2 3 tree demo: construction insert C

55 2 3 tree demo: construction 2 3 tree C

56 2 3 tree demo: construction insert X C X convert 2-node into 3-node

57 2 3 tree demo: construction insert X C X

58 2 3 tree demo: construction 2 3 tree C X

59 2 3 tree demo: construction insert M C M X convert 2-node into 3-node

60 2 3 tree demo: construction insert M C M X

61 2 3 tree demo: construction 2 3 tree C M X

62 2 3 tree demo: construction insert P C M P X convert 3-node into 4-node

63 2 3 tree demo: construction insert P C M P X

64 2 3 tree demo: construction insert P C M P X split 4-node (move L to parent)

65 2 3 tree demo: construction insert P M C P X

66 2 3 tree demo: construction insert P split 4-node (move M to parent) M C P X

67 2 3 tree demo: construction insert P M C P X

68 2 3 tree demo: construction 2 3 tree M C P X

69 2 3 tree demo: construction insert L M C L P X convert 2-node into 3-node

70 2 3 tree demo: construction insert L M C L P X convert 2-node into 3-node

71 2 3 tree demo: construction 2-3 tree M C L P X

72 2 3 tree: global properties Invariants. Maintains symmetric order and perfect balance. Pf. ach transformation maintains symmetric order and perfect balance. root a b c a b c parent is a 3-node left d e b d e a b c a c parent is a 2-node left a b c d a b d c middle a e b c d a c e b d right a a c right a b a b d b c d b d c d e c e 72

73 2 3 tree: performance plitting a 4-node is a local transformation: constant number of operations. a e b c d less than a between a and b between b and c between c and d between d and e greater than e a c e b d less than a between a and b between b and c between c and d between d and e greater than e 73

74 2 3 tree: performance Perfect balance. very path from root to null link has same length. Tree height. Worst case: lg N. Best case: log [all 2-nodes] 3 N.631 lg N. [all 3-nodes] The tree will have a height of between 12 and 20 for a million nodes. The tree will have a height of between 18 and 30 for a billion nodes. Bottom line. Guaranteed logarithmic performance for search and insert. 74

75 T implementations: summary implementation guarantee search insert delete search hit average case insert delete ordered ops? key interface sequential search (unordered list) N N N N N N equals() binary search (ordered array) log N N N log N N N compareto() BT N N N log N log N N compareto() 2 3 tree log N log N log N log N log N log N compareto() but hidden constant c is large (depends upon implementation) 75

76 2 3 tree: implementation? Direct implementation is complicated, because: Maintaining multiple node types is cumbersome. Need multiple compares to move down tree. Need to move back up the tree to split 4-nodes. Large number of cases for splitting. fantasy code public void put(key key, Value val) { Node x = root; Beautiful while (x.getthecorrectchild(key) algorithms are not always!= null) the most useful. } { x = x.getthecorrectchildkey(); Donald Knuth if (x.is4node()) x.split(); } if (x.is2node()) x.make3node(key, val); else if (x.is3node()) x.make4node(key, val); Bottom line. Could do it, but there's a better way. 76

77 3.3 BLNCD C T 2 3 search trees red black BTs lgorithms OBT DGWICK KVIN WYN

78 ow to implement 2 3 trees with binary trees? Challenge. ow to represent a 3 node? pproach 1. egular BT. No way to tell a 3-node from a 2-node. Cannot map from BT back to 2 3 tree. pproach 2. egular BT with red "glue" nodes. Wastes space, wasted link. Code probably messy. pproach 3. egular BT with red "glue" links. Widely used in practice. rbitrary restriction: red links lean left. 78

79 Left-leaning red black BTs (Guibas-edgewick 1979 and edgewick 2007) 3-node lack tree 2. Use "internal"mleft-leaning links as "glue" 3 nodes. greater less for between J C than a 3-node L a b less than a P b between a and b a M J C less than a between a and b a less than a M X C L P between a and b black links connect 2-nodes and 3-nodes red links "glue" nodes within a 3-node red black tree M J J greater than b ncoding a 3-node with two 2-nodes connected by a left-leaning red link greater than b ncoding a 3-node with two 2-nodes connected by a left-leaning red link ee larger key is root greater than b P L than b a and b X b ontal red links a b 1. epresent 2 3 tree as a BT. C X P L X 2 3 tree corresponding red black BT correspondence between red-black and 2-3 trees horizontal red links M 79

80 Left-leaning red black BTs: 1 1 correspondence with 2 3 trees Key property. 1 1 correspondence between 2 3 and LLB. red black tree J M C L P X horizontal red links C J L M P X 2-3 tree M J C L P X 80

81 n equivalent definition BT such that: No node has two red links connected to it. very path from root to null link has the same number of black links. ed links lean left. "perfect black balance" ck tree M J C L P X 81

82 earch implementation for red black BTs Observation. earch is the same as for elementary BT (ignore color). public Value get(key key) { Node x = root; while (x!= null) { int cmp = key.compareto(x.key); if (cmp < 0) x = x.left; else if (cmp > 0) x = x.right; else if (cmp == 0) return x.val; } return null; } but runs faster because of better balance ck tree emark. Most other ops (e.g., floor, iteration, selection) are also identical. C J L M P X 82

83 ed black BT representation ach node is pointed to by precisely one link (from its parent) can encode color of links in nodes. private static final boolean D = true; private static final boolean BLCK = false; private class Node { Key key; Value val; Node left, right; boolean color; // color of parent link } h.left.color is D C D h G J h.right.color is BLCK private boolean ised(node x) { if (x == null) return false; return x.color == D; } null links are black 83

84 Insertion into a LLB tree: overview Basic strategy. Maintain 1 1 correspondence with 2 3 trees. During internal operations, maintain: ymmetric order. Perfect black balance. [ but not necessarily color invariants ] right-leaning red link two red children (a temporary 4-node) ow? pply elementary red black BT operations: rotation and color flip. left-left red (a temporary 4-node) left-right red (a temporary 4-node) 84

85 lementary red black BT operations Left rotation. Orient a (temporarily) right-leaning red link to lean left. rotate left (before) less than h between and x greater than private Node rotateleft(node h) { assert ised(h.right); Node x = h.right; h.right = x.left; x.left = h; x.color = h.color; h.color = D; return x; } Invariants. Maintains symmetric order and perfect black balance. 85

86 lementary red black BT operations Left rotation. Orient a (temporarily) right-leaning red link to lean left. rotate left (after) less than h between and x greater than private Node rotateleft(node h) { assert ised(h.right); Node x = h.right; h.right = x.left; x.left = h; x.color = h.color; h.color = D; return x; } Invariants. Maintains symmetric order and perfect black balance. 86

87 lementary red black BT operations ight rotation. Orient a left-leaning red link to (temporarily) lean right. rotate right (before) x less than between and h greater than private Node rotateight(node h) { assert ised(h.left); Node x = h.left; h.left = x.right; x.right = h; x.color = h.color; h.color = D; return x; } Invariants. Maintains symmetric order and perfect black balance. 87

88 lementary red black BT operations ight rotation. Orient a left-leaning red link to (temporarily) lean right. rotate right (after) less than x between and h greater than private Node rotateight(node h) { assert ised(h.left); Node x = h.left; h.left = x.right; x.right = h; x.color = h.color; h.color = D; return x; } Invariants. Maintains symmetric order and perfect black balance. 88

89 lementary red black BT operations Color flip. ecolor to split a (temporary) 4-node. flip colors less (before) between h between greater private void flipcolors(node h) { assert!ised(h); assert ised(h.left); assert ised(h.right); h.color = D; h.left.color = BLCK; h.right.color = BLCK; } than and and than Invariants. Maintains symmetric order and perfect black balance. 89

90 lementary red black BT operations Color flip. ecolor to split a (temporary) 4-node. flip colors (after) less between h between greater private void flipcolors(node h) { assert!ised(h); assert ised(h.left); assert ised(h.right); h.color = D; h.left.color = BLCK; h.right.color = BLCK; } than and and than Invariants. Maintains symmetric order and perfect black balance. 90

91 Insertion into a LLB tree Warmup 1. Insert into a tree with exactly 1 node. left a b b root search ends at this null link root red link to new node containing a converts 2-node to 3-node right a a a search ends at this null link attached new node with red link b b root root rotated left to make a legal 3-node 91

92 Insertion into a LLB tree Case 1. Insert into a 2-node at the bottom. Do standard BT insert; color new link red. If new red link is a right link, rotate left. to maintain symmetric order and perfect black balance to fix color invariants insert C add new node here right link red so rotate left C C C Insert into a 2-node 92

93 Insertion into a LLB tree Warmup 2. Insert into a tree with exactly 2 nodes. larger a a a b b b c search ends at this null link attached new node with red link colors flipped to black c smaller a a a b b search ends at this null link b b c c attached new node with red link c rotated right b colors flipped to black c ed between a a a a a b b b b c search ends at this null link c c attached new node with red link rotated left c rotated right colors flipped to black c 93

94 Insertion into a LLB tree Case 2. Insert into a 3-node at the bottom. Do standard BT insert; color new link red. otate to balance the 4-node (if needed). Flip colors to pass red link up one level. otate to make lean left (if needed). to maintain symmetric order and perfect black balance to fix color invariants inserting C add new node here two lefts C both children red so flip colors node here C two lefts in a row so rotate right right link red so rotate left C C Insert into a 3-node right link red 94

95 Insertion into a LLB tree: passing red links up the tree Case 2. Insert into a 3-node at the bottom. inserting P Do standard BT insert; color new link red. otate to balance the 4-node (if needed). Flip colors to pass red link up one Clevel. otate to make lean left (if needed). inserting P epeat case 1 or case 2 up the tree (if needed). C M add new node here C right link red so rotate left C M M M P two lefts in a row so rotate right C M P inserting P C right link red both so rotate children left red so flip colors C C M M M M P add new node here P two lefts in a row so rotate right M P both children red so flip colors both children red so flip colors C M add new node here both children red so flip colors right link red C M both children so rotate left P red so to maintain symmetric order flip colors and perfect black balance C Mright link red so P rotate left two lefts in a rowto so rotate right C fix color M invariants P C C C both children red so flip colors C M M two lefts in a row so rotate right M C P Passing a red link up C the tree P P P M Passing a red link up the tree P P both children red so flip colors M M P 95

96 ed-black BT construction demo insert

97 ed-black BT construction demo insert

98 ed-black BT construction demo insert

99 ed-black BT construction demo insert two left reds in a row (rotate right)

100 ed-black BT construction demo both children red (flip colors)

101 ed-black BT construction demo both children red (flip colors)

102 ed-black BT construction demo red-black BT

103 ed-black BT construction demo red-black BT

104 ed-black BT construction demo insert

105 ed-black BT construction demo red-black BT

106 ed-black BT construction demo red-black BT

107 ed-black BT construction demo insert C C

108 ed-black BT construction demo right link red (rotate left) C

109 ed-black BT construction demo red-black BT C

110 ed-black BT construction demo red-black BT C

111 ed-black BT construction demo red-black BT C

112 ed-black BT construction demo insert C

113 ed-black BT construction demo two left reds in a row (rotate right) C

114 ed-black BT construction demo both children red (flip colors) C

115 ed-black BT construction demo right link red (rotate left) C

116 ed-black BT construction demo red-black BT C

117 ed-black BT construction demo red-black BT C

118 ed-black BT construction demo red-black BT C

119 ed-black BT construction demo insert X C X

120 ed-black BT construction demo insert X right link red (rotate left) C X

121 ed-black BT construction demo red-black BT X C

122 ed-black BT construction demo red-black BT X C

123 ed-black BT construction demo red-black BT X C

124 ed-black BT construction demo insert M X C M

125 ed-black BT construction demo insert M right link red (rotate left) X C M

126 ed-black BT construction demo red-black BT X C M

127 ed-black BT construction demo insert P two red children (flip colors) X C M P

128 ed-black BT construction demo right link red (rotate left) X C M P

129 ed-black BT construction demo two left reds in a row (rotate right) M X P C

130 ed-black BT construction demo two red children (flip colors) M C P X

131 ed-black BT construction demo two red children (flip colors) M C P X

132 ed-black BT construction demo red-black BT M C P X

133 ed-black BT construction demo red-black BT M C P X

134 ed-black BT construction demo red-black BT M C P X

135 Insertion into a LLB tree: Java implementation ame code for all cases. ight child red, left child black: rotate left. Left child, left-left grandchild red: rotate right. Both children red: flip colors. h h left rotate h right rotate flip colors private Node put(node h, Key key, Value val) { if (h == null) return new Node(key, val, D); int cmp = key.compareto(h.key); if (cmp < 0) h.left = put(h.left, key, val); else if (cmp > 0) h.right = put(h.right, key, val); else if (cmp == 0) h.val = val; Passing a red link up a red-black tree insert at bottom (and color it red) if (ised(h.right) &&!ised(h.left)) h = rotateleft(h); if (ised(h.left) && ised(h.left.left)) h = rotateight(h); if (ised(h.left) && ised(h.right)) flipcolors(h); lean left balance 4-node split 4-node } return h; only a few extra lines of code provides near-perfect balance 135

136 Insertion into a LLB tree: visualization 255 insertions in ascending order 136

137 Insertion into a LLB tree: visualization 255 insertions in descending order 137

138 Insertion into a LLB tree: visualization 255 random insertions 138

139 Balance in LLB trees Proposition. eight of tree is 2 lg N in the worst case. Pf. Black height = height of corresponding 2 3 tree lg N. Never two red links in-a-row. Property. eight of tree is ~ 1.0 lg N in typical applications. 139

140 T implementations: summary implementation guarantee search insert delete search hit average case insert delete ordered ops? key interface sequential search (unordered list) N N N N N N equals() binary search (ordered array) log N N N log N N N compareto() BT N N N log N log N N compareto() 2 3 tree log N log N log N log N log N log N compareto() red black BT log N log N log N log N log N log N compareto() hidden constant c is small (at most 2 lg N compares) 140

141 ed black BTs in the wild Common sense. ixth sense. Together they're the FBI's newest team. 141

142 ed black BTs in the wild 142

143 War story: why red black? Xerox PC innovations. [1970s] lto. GUI. thernet. malltalk. InterPress. Laser printing. Bitmapped display. WYIWYG text editor.... Xerox lto DIClIOlV1TIC FUl\l\V()K Fon BLNCD T Leo J. Guibas.Xerox Palo lto esearch Center, Palo lto, California, and Carnegie-fellon University and obert edgewick* Program in Computer cience Brown University Providence,. I. BTUCT I() this paper we present a uniform framework for the implementation and study of halanced tree algorithms. \Ve show how to imhcd in this the way down towards a leaf. s we will see, this has a number of significant advantages ovcr the older methods. We shall cxamine a numhcr of variations on a common theme and exhibit full implementations which are notable for their brcvity. One imp1cn1entation is exatnined carefully, and some properties about its 143

144 War story: red black BTs Telephone company contracted with database provider to build real-time database to store customer information. Database implementation. ed Black BT. xceeding height limit of 80 triggered error-recovery process. xtended telephone service outage. show allow for for up to 2 40 keys Main cause = height bound exceeded! Telephone company sues database provider. Legal testimony: did not rebalance BT during delete If implemented properly, the height of a red black BT with N keys is at most 2 lg N. expert witness 144

145 File system model Page. Contiguous block of data (e.g., a 4,096-byte chunk). Probe. First access to a page (e.g., from disk to memory). slow fast Property. Time required for a probe is much larger than time to access data within a page. Cost model. Number of probes. Goal. ccess data using minimum number of probes. 145

146 B-trees (Bayer-McCreight, 1972) B-tree. Generalize 2 3 trees by allowing up to M keys per node. t least M / 2 keys in all nodes (except root). very path from root to leaf has same number of links. choose M as large as possible so that M keys fit in a page (M = 1,024 is typical) G P U C D F I J K L O Q T V W X Y Z a B-tree (M = 6) 146

147 earch in a B-tree tart at root. Check if node contains key. Otherwise, find interval for search key and take corresponding link. could use binary search (but all ops are considered free) G P U C D F I J K L O Q T V W X Y Z a B-tree (M = 6) 147

148 Insertion in a B-tree earch for new key. Insert at bottom. plit nodes with M + 1 keys on the way back up the B-tree (moving middle key to parent). G P U C D F I J K L O Q T V W X Y Z a B-tree (M = 6) 148

149 Balance in B-tree Proposition. search or an insertion in a B-tree of order M with N keys requires between ~ log M N and ~ log M/2 N probes. Pf. ll nodes (except possibly root) have between M / 2 and M keys. In practice. Number of probes is at most 4. M = 1024; N = 62 billion log M/2 N 4 149

150 Balanced search trees: quiz 3 What of the following does the B in B-tree not mean?. Bayer B. Balanced C. Binary D. Boeing. I don't know. the more you think about what the B in B-trees could mean, the more you learn about B-trees and that is good. udolph Bayer 150

151 Balanced trees in the wild ed Black trees are widely used as system symbol tables. Java: java.util.treemap, java.util.treeet. C++ TL: map, multimap, multiset. Linux kernel: completely fair scheduler, linux/rbtree.h. macs: conservative stack scanning. B-tree cousins. B+ tree, B*tree, B# tree, B-trees (and cousins) are widely used for file systems and databases. Windows: NTF. Mac: F, F+. Linux: eiserf, XF, xt3f, JF, BTF. Databases: OCL, DB2, ING, QL, PostgreQL. 151

Algorithms. Algorithms 3.3 BALANCED SEARCH TREES. 2 3 search trees red black BSTs B-trees (see book or videos) ROBERT SEDGEWICK KEVIN WAYNE

Algorithms. Algorithms 3.3 BALANCED SEARCH TREES. 2 3 search trees red black BSTs B-trees (see book or videos) ROBERT SEDGEWICK KEVIN WAYNE lgorithms OBT DGWICK KVIN WYN 3.3 BLNCD C T lgorithms F O U T D I T I O N 2 3 search trees red black BTs B-trees (see book or videos) OBT DGWICK KVIN WYN http://algs4.cs.princeton.edu Last updated on 10/17/17

More information

CS2012 Programming Techniques II

CS2012 Programming Techniques II 14/02/2014 C2012 Programming Techniques II Vasileios Koutavas Lecture 14 1 BTs ordered operations deletion 27 T implementations: summary implementation guarantee average case search insert delete search

More information

Algorithms. Algorithms 3.2 BINARY SEARCH TREES. BSTs ordered operations iteration deletion (see book or videos) ROBERT SEDGEWICK KEVIN WAYNE

Algorithms. Algorithms 3.2 BINARY SEARCH TREES. BSTs ordered operations iteration deletion (see book or videos) ROBERT SEDGEWICK KEVIN WAYNE lgorithms OBT DGWIK KVIN WYN 3.2 BINY T lgorithms F O U T D I T I O N BTs ordered operations iteration deletion (see book or videos) OBT DGWIK KVIN WYN https://algs4.cs.princeton.edu Last updated on 10/9/18

More information

3.2 BINARY SEARCH TREES. BSTs ordered operations iteration deletion. Algorithms ROBERT SEDGEWICK KEVIN WAYNE.

3.2 BINARY SEARCH TREES. BSTs ordered operations iteration deletion. Algorithms ROBERT SEDGEWICK KEVIN WAYNE. 3.2 BINY T lgorithms BTs ordered operations iteration deletion OBT DGWIK KVIN WYN http://algs4.cs.princeton.edu Binary search trees Definition. BT is a binary tree in symmetric order. binary tree is either:

More information

Algorithms. Algorithms 3.2 BINARY SEARCH TREES. BSTs ordered operations deletion ROBERT SEDGEWICK KEVIN WAYNE.

Algorithms. Algorithms 3.2 BINARY SEARCH TREES. BSTs ordered operations deletion ROBERT SEDGEWICK KEVIN WAYNE. lgorithms OBT DGWIK KVIN WYN 3.2 BINY T lgorithms F O U T D I T I O N BTs ordered operations deletion OBT DGWIK KVIN WYN http://algs4.cs.princeton.edu 3.2 BINY T lgorithms BTs ordered operations deletion

More information

BINARY SEARCH TREES TODAY BBM ALGORITHMS DEPT. OF COMPUTER ENGINEERING. Binary Search Tree (BST) Binary search trees

BINARY SEARCH TREES TODAY BBM ALGORITHMS DEPT. OF COMPUTER ENGINEERING. Binary Search Tree (BST) Binary search trees BB 202 - LOIT TODY DPT. OF OPUT NININ BTs Ordered operations Deletion BINY T cknowledgement: The course slides are adapted from the slides prepared by. edgewick and K. Wayne of Princeton University. Binary

More information

BINARY SEARCH TREES BBM ALGORITHMS DEPT. OF COMPUTER ENGINEERING

BINARY SEARCH TREES BBM ALGORITHMS DEPT. OF COMPUTER ENGINEERING cknowledgement: The course slides are adapted from the slides prepared by. edgewick and K. Wayne of Princeton University. BB 202 - LGOIT DPT. OF OPUT NGINING BINY T BTs Ordered operations Deletion TODY

More information

Lecture 14: Binary Search Trees (2)

Lecture 14: Binary Search Trees (2) cs2010: algorithms and data structures Lecture 14: Binary earch Trees (2) Vasileios Koutavas chool of omputer cience and tatistics Trinity ollege Dublin lgorithms OBT DGWIK KVIN WYN 3.2 BINY T lgorithms

More information

Lecture 13: Binary Search Trees

Lecture 13: Binary Search Trees cs2010: algorithms and data structures Lecture 13: Binary Search Trees Vasileios Koutavas School of Computer Science and Statistics Trinity College Dublin Algorithms ROBERT SEDGEWICK KEVIN WAYNE 3.2 BINARY

More information

2-3 search trees red-black BSTs B-trees

2-3 search trees red-black BSTs B-trees 2-3 serch trees red-lck BTs B-trees 3 2-3 tree llow 1 or 2 keys per node. 2-node: one key, two children. 3-node: two keys, three children. ymmetric order. Inorder trversl yields keys in scending order.

More information

Binary Search Trees. Analysis of Algorithms

Binary Search Trees. Analysis of Algorithms Binary Search Trees Analysis of Algorithms Binary Search Trees A BST is a binary tree in symmetric order 31 Each node has a key and every node s key is: 19 23 25 35 38 40 larger than all keys in its left

More information

CS2012 Programming Techniques II

CS2012 Programming Techniques II 10/02/2014 CS2012 Programming Techniques II Vasileios Koutavas Lecture 12 1 10/02/2014 Lecture 12 2 10/02/2014 Lecture 12 3 Answer until Thursday. Results Friday 10/02/2014 Lecture 12 4 Last Week Review:

More information

Left-Leaning Red-Black Trees

Left-Leaning Red-Black Trees Left-Leaning Red-Black Trees Robert Sedgewick Princeton University Original version: Data structures seminar at Dagstuhl (Feb 2008) red-black trees made simpler (!) full delete() implementation Next version:

More information

4.2 Binary Search Trees

4.2 Binary Search Trees Binary trees 4. Binary earc Trees Definition. BT is a binary tree in symmetric order. root a left link a subtree binary tree is eiter: mpty. rigt cild of root Two disjoint binary trees (left and rigt).

More information

Symbol Table. IP address

Symbol Table. IP address 4.4 Symbol Tables Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright 2002 2010 4/2/11 10:40 AM Symbol Table Symbol table. Key-value pair abstraction.

More information

Balanced Binary Search Trees

Balanced Binary Search Trees Balanced Binary Search Trees Pedro Ribeiro DCC/FCUP 2017/2018 Pedro Ribeiro (DCC/FCUP) Balanced Binary Search Trees 2017/2018 1 / 48 Motivation Let S be a set of comparable objects/items: Let a and b be

More information

CSE 530A. B+ Trees. Washington University Fall 2013

CSE 530A. B+ Trees. Washington University Fall 2013 CSE 530A B+ Trees Washington University Fall 2013 B Trees A B tree is an ordered (non-binary) tree where the internal nodes can have a varying number of child nodes (within some range) B Trees When a key

More information

4.4 Symbol Tables. Symbol Table. Symbol Table Applications. Symbol Table API

4.4 Symbol Tables. Symbol Table. Symbol Table Applications. Symbol Table API Symbol Table 4.4 Symbol Tables Symbol table. Keyvalue pair abstraction. Insert a key with specified value. Given a key, search for the corresponding value. Ex. [DS lookup] Insert URL with specified IP

More information

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree. The Lecture Contains: Index structure Binary search tree (BST) B-tree B+-tree Order file:///c /Documents%20and%20Settings/iitkrana1/My%20Documents/Google%20Talk%20Received%20Files/ist_data/lecture13/13_1.htm[6/14/2012

More information

! Insert a key with specified value. ! Given a key, search for the corresponding value. ! Insert URL with specified IP address.

! Insert a key with specified value. ! Given a key, search for the corresponding value. ! Insert URL with specified IP address. Symbol Table 4.4 Symbol Tables Symbol table. Key-value pair abstraction.! Insert a key with specied value.! Given a key, search for the corresponding value. Ex. [DS lookup]! Insert URL with specied IP

More information

Balanced Search Trees

Balanced Search Trees Balanced Search Trees Computer Science E-22 Harvard Extension School David G. Sullivan, Ph.D. Review: Balanced Trees A tree is balanced if, for each node, the node s subtrees have the same height or have

More information

Algorithms. Algorithms 3.1 SYMBOL TABLES. API elementary implementations ordered operations ROBERT SEDGEWICK KEVIN WAYNE

Algorithms. Algorithms 3.1 SYMBOL TABLES. API elementary implementations ordered operations ROBERT SEDGEWICK KEVIN WAYNE Algorithms ROBERT SEDGEWICK KEVIN WAYNE 3.1 SYMBOL TABLES Algorithms F O U R T H E D I T I O N API elementary implementations ordered operations ROBERT SEDGEWICK KEVIN WAYNE https://algs4.cs.princeton.edu

More information

Algorithms. Algorithms. Algorithms 3.1 SYMBOL TABLES. API elementary implementations ordered operations

Algorithms. Algorithms. Algorithms 3.1 SYMBOL TABLES. API elementary implementations ordered operations OBT DGWIK KVIN WYN lgorithms OBT DGWIK KVIN WYN Data structures 3.1 YBOL TBL lgorithms F O U T D I T I O N PI elementary implementations mart data structures and dumb code works a lot better than the other

More information

Algorithms. AVL Tree

Algorithms. AVL Tree Algorithms AVL Tree Balanced binary tree The disadvantage of a binary search tree is that its height can be as large as N-1 This means that the time needed to perform insertion and deletion and many other

More information

4.1 Symbol Tables. API sequential search binary search ordered operations. Symbol tables

4.1 Symbol Tables. API sequential search binary search ordered operations. Symbol tables . ymbol Tables ymbol tables Key-value pair abstraction. Insert a value with specified key. Given a key, for the corresponding value. I sequential binary ordered operations x. D lookup. Insert U with specified

More information

Multi-way Search Trees! M-Way Search! M-Way Search Trees Representation!

Multi-way Search Trees! M-Way Search! M-Way Search Trees Representation! Lecture 10: Multi-way Search Trees: intro to B-trees 2-3 trees 2-3-4 trees Multi-way Search Trees A node on an M-way search tree with M 1 distinct and ordered keys: k 1 < k 2 < k 3

More information

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Advanced Java Concepts Unit 5: Trees. Notes and Exercises dvanced Java Concepts Unit 5: Trees. Notes and Exercises Tree is a data structure like the figure shown below. We don t usually care about unordered trees but that s where we ll start. Later we will focus

More information

ECE 242 Data Structures and Algorithms. Trees IV. Lecture 21. Prof.

ECE 242 Data Structures and Algorithms.  Trees IV. Lecture 21. Prof. ECE 22 Data Structures and Algorithms http://www.ecs.umass.edu/~polizzi/teaching/ece22/ Trees IV Lecture 2 Prof. Eric Polizzi Summary previous lectures Implementations BST 5 5 7 null 8 null null 7 null

More information

Advanced Tree Data Structures

Advanced Tree Data Structures Advanced Tree Data Structures Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park Binary trees Traversal order Balance Rotation Multi-way trees Search Insert Overview

More information

Trees can be used to store entire records from a database, serving as an in-memory representation of the collection of records in a file.

Trees can be used to store entire records from a database, serving as an in-memory representation of the collection of records in a file. Large Trees 1 Trees can be used to store entire records from a database, serving as an in-memory representation of the collection of records in a file. Trees can also be used to store indices of the collection

More information

CS350: Data Structures B-Trees

CS350: Data Structures B-Trees B-Trees James Moscola Department of Engineering & Computer Science York College of Pennsylvania James Moscola Introduction All of the data structures that we ve looked at thus far have been memory-based

More information

CS 171: Introduction to Computer Science II. Binary Search Trees

CS 171: Introduction to Computer Science II. Binary Search Trees CS 171: Introduction to Computer Science II Binary Search Trees Binary Search Trees Symbol table applications BST definitions and terminologies Search and insert Traversal Ordered operations Delete Symbol

More information

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010 Uses for About Binary January 31, 2010 Uses for About Binary Uses for Uses for About Basic Idea Implementing Binary Example: Expression Binary Search Uses for Uses for About Binary Uses for Storage Binary

More information

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min Binary Search Trees FRIDAY ALGORITHMS Sorted Arrays Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min 6 10 11 17 2 0 6 Running Time O(1) O(lg n) O(1) O(1)

More information

School of Computing National University of Singapore CS2010 Data Structures and Algorithms 2 Semester 2, AY 2015/16. Tutorial 2 (Answers)

School of Computing National University of Singapore CS2010 Data Structures and Algorithms 2 Semester 2, AY 2015/16. Tutorial 2 (Answers) chool of Computing National University of ingapore C10 Data tructures and lgorithms emester, Y /1 Tutorial (nswers) Feb, 1 (Week ) BT and Priority Queue/eaps Q1) Trace the delete() code for a BT for the

More information

Lecture 15 Binary Search Trees

Lecture 15 Binary Search Trees Lecture 15 Binary Search Trees 15-122: Principles of Imperative Computation (Fall 2017) Frank Pfenning, André Platzer, Rob Simmons, Iliano Cervesato In this lecture, we will continue considering ways to

More information

Trees. Eric McCreath

Trees. Eric McCreath Trees Eric McCreath 2 Overview In this lecture we will explore: general trees, binary trees, binary search trees, and AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for:

More information

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap DATA STRUCTURES AND ALGORITHMS Hierarchical data structures: AVL tree, Bayer tree, Heap Summary of the previous lecture TREE is hierarchical (non linear) data structure Binary trees Definitions Full tree,

More information

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am Announcements Midterm exam 2, Thursday, May 18 Closed book/notes but one sheet of paper allowed Covers up to stacks and queues Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps

More information

4.4 Symbol Tables. Symbol Table. Symbol Table Applications. Symbol Table API

4.4 Symbol Tables. Symbol Table. Symbol Table Applications. Symbol Table API Symbol Table 4.4 Symbol Tables Symbol table. Keyvalue pair abstraction. Insert a key with specified value. Given a key, search for the corresponding value. Ex. [DS lookup] Insert URL with specified IP

More information

Design and Analysis of Algorithms Lecture- 9: B- Trees

Design and Analysis of Algorithms Lecture- 9: B- Trees Design and Analysis of Algorithms Lecture- 9: B- Trees Dr. Chung- Wen Albert Tsao atsao@svuca.edu www.408codingschool.com/cs502_algorithm 1/12/16 Slide Source: http://www.slideshare.net/anujmodi555/b-trees-in-data-structure

More information

4.4 Symbol Tables and BSTs

4.4 Symbol Tables and BSTs 4.4 Symbol Tables and BSTs Symbol Table Symbol Table Applications Symbol table. Keyvalue pair abstraction. Insert a key with specified value. Given a key, search for the corresponding value. Ex. [DS lookup]

More information

Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

Trees. Courtesy to Goodrich, Tamassia and Olga Veksler Lecture 12: BT Trees Courtesy to Goodrich, Tamassia and Olga Veksler Instructor: Yuzhen Xie Outline B-tree Special case of multiway search trees used when data must be stored on the disk, i.e. too large

More information

BINARY SEARCH TREES TODAY BBM ALGORITHMS DEPT. OF COMPUTER ENGINEERING. Binary Search Tree (BST) Binary search trees

BINARY SEARCH TREES TODAY BBM ALGORITHMS DEPT. OF COMPUTER ENGINEERING. Binary Search Tree (BST) Binary search trees BB 202 - LOIT TODY DPT. OF OPUT NININ BTs Ordered operations Deletion BINY T cknowledgement: The course slides are adapted from the slides prepared by. edgewick and K. Wayne of Princeton University. Binary

More information

B-Trees. Version of October 2, B-Trees Version of October 2, / 22

B-Trees. Version of October 2, B-Trees Version of October 2, / 22 B-Trees Version of October 2, 2014 B-Trees Version of October 2, 2014 1 / 22 Motivation An AVL tree can be an excellent data structure for implementing dictionary search, insertion and deletion Each operation

More information

CS 350 : Data Structures B-Trees

CS 350 : Data Structures B-Trees CS 350 : Data Structures B-Trees David Babcock (courtesy of James Moscola) Department of Physical Sciences York College of Pennsylvania James Moscola Introduction All of the data structures that we ve

More information

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Advanced Java Concepts Unit 5: Trees. Notes and Exercises Advanced Java Concepts Unit 5: Trees. Notes and Exercises A Tree is a data structure like the figure shown below. We don t usually care about unordered trees but that s where we ll start. Later we will

More information

lgorithms OBT DGWIK KVIN WYN 3.1 YMBOL TBL lgorithms F O U T D I T I O N PI elementary implementations ordered operations OBT DGWIK KVIN WYN http://algs4.cs.princeton.edu 3.1 YMBOL TBL lgorithms PI elementary

More information

Hierarchical data structures. Announcements. Motivation for trees. Tree overview

Hierarchical data structures. Announcements. Motivation for trees. Tree overview Announcements Midterm exam 2, Thursday, May 18 Closed book/notes but one sheet of paper allowed Covers up to stacks and queues Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps

More information

Transform & Conquer. Presorting

Transform & Conquer. Presorting Transform & Conquer Definition Transform & Conquer is a general algorithm design technique which works in two stages. STAGE : (Transformation stage): The problem s instance is modified, more amenable to

More information

Algorithms. Algorithms 3.1 SYMBOL TABLES. API elementary implementations ordered operations ROBERT SEDGEWICK KEVIN WAYNE

Algorithms. Algorithms 3.1 SYMBOL TABLES. API elementary implementations ordered operations ROBERT SEDGEWICK KEVIN WAYNE Algorithms ROBERT SEDGEWICK KEVIN WAYNE 3.1 SYMBOL TABLES Algorithms F O U R T H E D I T I O N API elementary implementations ordered operations ROBERT SEDGEWICK KEVIN WAYNE https://algs4.cs.princeton.edu

More information

Trees can be used to store entire records from a database, serving as an in-memory representation of the collection of records in a file.

Trees can be used to store entire records from a database, serving as an in-memory representation of the collection of records in a file. Large Trees 1 Trees can be used to store entire records from a database, serving as an in-memory representation of the collection of records in a file. Trees can also be used to store indices of the collection

More information

Lecture 15 Notes Binary Search Trees

Lecture 15 Notes Binary Search Trees Lecture 15 Notes Binary Search Trees 15-122: Principles of Imperative Computation (Spring 2016) Frank Pfenning, André Platzer, Rob Simmons 1 Introduction In this lecture, we will continue considering ways

More information

Chapter 12 Advanced Data Structures

Chapter 12 Advanced Data Structures Chapter 12 Advanced Data Structures 2 Red-Black Trees add the attribute of (red or black) to links/nodes red-black trees used in C++ Standard Template Library (STL) Java to implement maps (or, as in Python)

More information

Binary search trees (chapters )

Binary search trees (chapters ) Binary search trees (chapters 18.1 18.3) Binary search trees In a binary search tree (BST), every node is greater than all its left descendants, and less than all its right descendants (recall that this

More information

Augmenting Data Structures

Augmenting Data Structures Augmenting Data Structures [Not in G &T Text. In CLRS chapter 14.] An AVL tree by itself is not very useful. To support more useful queries we need more structure. General Definition: An augmented data

More information

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology Introduction Chapter 4 Trees for large input, even linear access time may be prohibitive we need data structures that exhibit average running times closer to O(log N) binary search tree 2 Terminology recursive

More information

COS 226 Algorithms and Data Structures Fall Midterm

COS 226 Algorithms and Data Structures Fall Midterm COS 226 Algorithms and Data Structures Fall 2017 Midterm This exam has 10 questions (including question 0) worth a total of 55 points. You have 0 minutes. This exam is preprocessed by a computer, so please

More information

CS 380 ALGORITHM DESIGN AND ANALYSIS

CS 380 ALGORITHM DESIGN AND ANALYSIS CS 380 ALGORITHM DESIGN AND ANALYSIS Lecture 12: Red-Black Trees Text Reference: Chapters 12, 13 Binary Search Trees (BST): Review Each node in tree T is a object x Contains attributes: Data Pointers to

More information

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 The data of the problem is of 2GB and the hard disk is of 1GB capacity, to solve this problem we should Use better data structures

More information

CS 270 Algorithms. Oliver Kullmann. Binary search. Lists. Background: Pointers. Trees. Implementing rooted trees. Tutorial

CS 270 Algorithms. Oliver Kullmann. Binary search. Lists. Background: Pointers. Trees. Implementing rooted trees. Tutorial Week 7 General remarks Arrays, lists, pointers and 1 2 3 We conclude elementary data structures by discussing and implementing arrays, lists, and trees. Background information on pointers is provided (for

More information

CS 310: Memory Hierarchy and B-Trees

CS 310: Memory Hierarchy and B-Trees CS 310: Memory Hierarchy and B-Trees Chris Kauffman Week 14-1 Matrix Sum Given an M by N matrix X, sum its elements M rows, N columns Sum R given X, M, N sum = 0 for i=0 to M-1{ for j=0 to N-1 { sum +=

More information

Lecture Notes on Binary Search Trees

Lecture Notes on Binary Search Trees Lecture Notes on Binary Search Trees 15-122: Principles of Imperative Computation Frank Pfenning André Platzer Lecture 16 March 17, 2015 1 Introduction In this lecture, we will continue considering ways

More information

3.1 Symbol Tables. API sequential search binary search ordered operations

3.1 Symbol Tables. API sequential search binary search ordered operations 3.1 Symbol Tables API sequential search binary search ordered operations Algorithms in Java, 4 th Edition Robert Sedgewick and Kevin Wayne Copyright 2009 February 23, 2010 8:21:03 AM Symbol tables Key-value

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Binary search tree (part I) Version of March 24, 2013 Abstract These lecture notes are meant

More information

Main Memory and the CPU Cache

Main Memory and the CPU Cache Main Memory and the CPU Cache CPU cache Unrolled linked lists B Trees Our model of main memory and the cost of CPU operations has been intentionally simplistic The major focus has been on determining

More information

Extra: B+ Trees. Motivations. Differences between BST and B+ 10/27/2017. CS1: Java Programming Colorado State University

Extra: B+ Trees. Motivations. Differences between BST and B+ 10/27/2017. CS1: Java Programming Colorado State University Extra: B+ Trees CS1: Java Programming Colorado State University Slides by Wim Bohm and Russ Wakefield 1 Motivations Many times you want to minimize the disk accesses while doing a search. A binary search

More information

B-Trees. Based on materials by D. Frey and T. Anastasio

B-Trees. Based on materials by D. Frey and T. Anastasio B-Trees Based on materials by D. Frey and T. Anastasio 1 Large Trees n Tailored toward applications where tree doesn t fit in memory q operations much faster than disk accesses q want to limit levels of

More information

input sort left half sort right half merge results Mergesort overview

input sort left half sort right half merge results Mergesort overview Algorithms OBT S DGWICK K VIN W AYN Two classic sorting algorithms: mergesort and quicksort Critical components in the world s computational infrastructure. Full scientific understanding of their properties

More information

ELEMENTARY SEARCH ALGORITHMS

ELEMENTARY SEARCH ALGORITHMS BB 22 - GOIT TODY DT. OF OUT GIIG ymbol Tables I lementary implementations Ordered operations TY GOIT cknowledgement: The course slides are adapted from the slides prepared by. edgewick and K. Wayne of

More information

Balanced search trees. DS 2017/2018

Balanced search trees. DS 2017/2018 Balanced search trees. DS 2017/2018 Red-black trees Symmetric binary B-tree, Rudolf Bayer, 1972. The balancing is maintained by using a coloring of the nodes. The red-black trees are binary search trees

More information

Question Bank Subject: Advanced Data Structures Class: SE Computer

Question Bank Subject: Advanced Data Structures Class: SE Computer Question Bank Subject: Advanced Data Structures Class: SE Computer Question1: Write a non recursive pseudo code for post order traversal of binary tree Answer: Pseudo Code: 1. Push root into Stack_One.

More information

Search Trees. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Binary Search Trees

Search Trees. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Binary Search Trees Unit 9, Part 2 Search Trees Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Binary Search Trees Search-tree property: for each node k: all nodes in k s left subtree are < k all nodes

More information

COMPUTER SCIENCE. 15. Symbol Tables. Section 4.4.

COMPUTER SCIENCE. 15. Symbol Tables. Section 4.4. COMPUTER SCIENCE S E D G E W I C K / W A Y N E 15. Symbol Tables Section 4.4 http://introcs.cs.princeton.edu COMPUTER SCIENCE S E D G E W I C K / W A Y N E 15.Symbol Tables APIs and clients A design challenge

More information

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g) Introduction to Algorithms March 11, 2009 Massachusetts Institute of Technology 6.006 Spring 2009 Professors Sivan Toledo and Alan Edelman Quiz 1 Solutions Problem 1. Quiz 1 Solutions Asymptotic orders

More information

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015 BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015 1 administrivia 2 -assignment 7 due tonight at midnight -asking for regrades through assignment 5 and midterm must

More information

ELEMENTARY SEARCH ALGORITHMS

ELEMENTARY SEARCH ALGORITHMS BBM 202 - ALGORITHMS DEPT. OF COMPUTER ENGINEERING ELEMENTARY SEARCH ALGORITHMS Acknowledgement: The course slides are adapted from the slides prepared by R. Sedgewick and K. Wayne of Princeton University.

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Binary search tree (part I) Version of March 24, 2013 Abstract These lecture notes are meant

More information

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs Computational Optimization ISE 407 Lecture 16 Dr. Ted Ralphs ISE 407 Lecture 16 1 References for Today s Lecture Required reading Sections 6.5-6.7 References CLRS Chapter 22 R. Sedgewick, Algorithms in

More information

CSE332: Data Abstractions Lecture 7: B Trees. James Fogarty Winter 2012

CSE332: Data Abstractions Lecture 7: B Trees. James Fogarty Winter 2012 CSE2: Data Abstractions Lecture 7: B Trees James Fogarty Winter 20 The Dictionary (a.k.a. Map) ADT Data: Set of (key, value) pairs keys must be comparable insert(jfogarty,.) Operations: insert(key,value)

More information

Lecture 3: B-Trees. October Lecture 3: B-Trees

Lecture 3: B-Trees. October Lecture 3: B-Trees October 2017 Remarks Search trees The dynamic set operations search, minimum, maximum, successor, predecessor, insert and del can be performed efficiently (in O(log n) time) if the search tree is balanced.

More information

ELEMENTARY SEARCH ALGORITHMS

ELEMENTARY SEARCH ALGORITHMS BB - GOIT TODY DT. OF OUT GIIG KUT D ymbol Tables I lementary implementations Ordered operations TY GOIT ar., cknowledgement:.the$course$slides$are$adapted$from$the$slides$prepared$by$.$edgewick$ and$k.$wayne$of$rinceton$university.

More information

Multi-way Search Trees

Multi-way Search Trees Multi-way Search Trees Kuan-Yu Chen ( 陳冠宇 ) 2018/10/24 @ TR-212, NTUST Review Red-Black Trees Splay Trees Huffman Trees 2 Multi-way Search Trees. Every node in a binary search tree contains one value and

More information

Data Structures in Java

Data Structures in Java Data Structures in Java Lecture 9: Binary Search Trees. 10/7/015 Daniel Bauer 1 Contents 1. Binary Search Trees. Implementing Maps with BSTs Map ADT A map is collection of (key, value) pairs. Keys are

More information

Chapter 22 Splay Trees

Chapter 22 Splay Trees Chapter 22 Splay Trees Introduction Splay trees support all the operations of binary trees. But they do not guarantee Ο(log N) worst-case performance. Instead, its bounds are amortized, meaning that although

More information

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree Chapter 4 Trees 2 Introduction for large input, even access time may be prohibitive we need data structures that exhibit running times closer to O(log N) binary search tree 3 Terminology recursive definition

More information

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values)

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values) 9// References and Homework Text: Chapters, and ABSTRACT DATA TYPES; LISTS & TREES Homework: Learn these List methods, from http://docs.oracle.com/javase/7/docs/api/java/util/list.html add, addall, contains,

More information

Laboratory Module X B TREES

Laboratory Module X B TREES Purpose: Purpose 1... Purpose 2 Purpose 3. Laboratory Module X B TREES 1. Preparation Before Lab When working with large sets of data, it is often not possible or desirable to maintain the entire structure

More information

ELEMENTARY SEARCH ALGORITHMS

ELEMENTARY SEARCH ALGORITHMS BB - GOIT TODY DT. OF OUT GIIG KUT D ymbol Tables I lementary implementations Ordered operations TY GOIT ar., cknowledgement: The course slides are adapted from the slides prepared by. edgewick and K.

More information

CS 310 B-trees, Page 1. Motives. Large-scale databases are stored in disks/hard drives.

CS 310 B-trees, Page 1. Motives. Large-scale databases are stored in disks/hard drives. CS 310 B-trees, Page 1 Motives Large-scale databases are stored in disks/hard drives. Disks are quite different from main memory. Data in a disk are accessed through a read-write head. To read a piece

More information

CS200: Balanced Search Trees

CS200: Balanced Search Trees Value Oriented Data Structures CS200: Balanced Search Trees Walls & Mirrors Chapters 12,13 Homework 4 extension Next week: Programming quiz during recit Midterm 2 April 8 th (in class) New partners and

More information

CIS265/ Trees Red-Black Trees. Some of the following material is from:

CIS265/ Trees Red-Black Trees. Some of the following material is from: CIS265/506 2-3-4 Trees Red-Black Trees Some of the following material is from: Data Structures for Java William H. Ford William R. Topp ISBN 0-13-047724-9 Chapter 27 Balanced Search Trees Bret Ford 2005,

More information

CISC 235: Topic 4. Balanced Binary Search Trees

CISC 235: Topic 4. Balanced Binary Search Trees CISC 235: Topic 4 Balanced Binary Search Trees Outline Rationale and definitions Rotations AVL Trees, Red-Black, and AA-Trees Algorithms for searching, insertion, and deletion Analysis of complexity CISC

More information

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1 CS61B Lecture #20: Trees Last modified: Mon Oct 8 21:21:22 2018 CS61B: Lecture #20 1 A Recursive Structure Trees naturally represent recursively defined, hierarchical objects with more than one recursive

More information

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux CS 231 Data Structures and Algorithms Fall 2018 Recursion and Binary Trees Lecture 21 October 24, 2018 Prof. Zadia Codabux 1 Agenda ArrayQueue.java Recursion Binary Tree Terminologies Traversal 2 Administrative

More information

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct. MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct. In linked list the elements are necessarily to be contiguous In linked list the elements may locate at far positions

More information

CSE373 Fall 2013, Midterm Examination October 18, 2013

CSE373 Fall 2013, Midterm Examination October 18, 2013 CSE373 Fall 2013, Midterm Examination October 18, 2013 Please do not turn the page until the bell rings. Rules: The exam is closed-book, closed-note, closed calculator, closed electronics. Please stop

More information

Tree traversals and binary trees

Tree traversals and binary trees Tree traversals and binary trees Comp Sci 1575 Data Structures Valgrind Execute valgrind followed by any flags you might want, and then your typical way to launch at the command line in Linux. Assuming

More information

COS 226 Algorithms and Data Structures Fall Midterm

COS 226 Algorithms and Data Structures Fall Midterm COS 226 Algorithms and Data Structures Fall 2014 Midterm This test has 9 questions worth a total of 55 points. You have 80 minutes. The exam is closed book, except that you are allowed to use a one page

More information

Self-Balancing Search Trees. Chapter 11

Self-Balancing Search Trees. Chapter 11 Self-Balancing Search Trees Chapter 11 Chapter Objectives To understand the impact that balance has on the performance of binary search trees To learn about the AVL tree for storing and maintaining a binary

More information