Balanced Trees Part One

Size: px
Start display at page:

Download "Balanced Trees Part One"

Transcription

1 Balanced Trees Part One

2 Balanced Trees Balanced search trees are among the most useful and versatile data structures. Many programming languages ship with a balanced tree library. C++: std::map / std::set Java: TreeMap / TreeSet Python: OrderedDict Many advanced data structures are layered on top of balanced trees. We'll see them used to build y-fast Tries later in the quarter. (They re really cool, trust me!)

3 Where We're Going B-Trees A simple type of balanced tree developed for block storage. Red/Black Trees The canonical balanced binary search tree. Augmented Search Trees Adding extra information to balanced trees to supercharge the data structure. Two Advanced Operations The split and join operations.

4 Outline for Today BST Review Refresher on basic BST concepts and runtimes. Overview of Red/Black Trees What we're building toward. B-Trees and Trees A simple balanced tree in depth. Intuiting Red/Black Trees A much better feel for red/black trees.

5 A Quick BST Review

6 Binary Search Trees A binary search tree is a binary tree with the following properties: Each node in the BST stores a key, and optionally, some auxiliary information. The key of every node in a BST is strictly greater than all keys to its left and strictly smaller than all keys to its right. The height of a binary search tree is the length of the longest path from the root to a leaf, measured in the number of edges. A tree with one node has height 0. A tree with no nodes has height -1, by convention.

7 Searching a BST

8 Searching a BST

9 Searching a BST

10 Searching a BST

11 Searching a BST

12 Searching a BST

13 Searching a BST

14 Searching a BST

15 Searching a BST

16 Searching a BST

17 Inserting into a BST

18 Inserting into a BST

19 Inserting into a BST

20 Inserting into a BST

21 Inserting into a BST

22 Inserting into a BST

23 Inserting into a BST

24 Inserting into a BST

25 Deleting from a BST

26 Deleting from a BST

27 Deleting from a BST

28 Deleting from a BST

29 Deleting from a BST

30 Deleting from a BST

31 Deleting from a BST

32 Deleting from a BST Case Case 1: 1: If If the the node node has has just just no no children, children, just just remove remove it. it. 166

33 Deleting from a BST

34 Deleting from a BST

35 Deleting from a BST

36 Deleting from a BST

37 Deleting from a BST

38 Deleting from a BST Case Case 2: 2: If If the the node node has has just just one one child, child, remove remove it it and and replace replace it it with with its its child. child

39 Deleting from a BST

40 Deleting from a BST

41 Deleting from a BST

42 Deleting from a BST

43 Deleting from a BST

44 Deleting from a BST

45 Deleting from a BST

46 Deleting from a BST

47 Deleting from a BST

48 Deleting from a BST

49 Deleting from a BST

50 Deleting from a BST Case Case 3: 3: If If the the node node has has two two children, children, fnd fnd its its inorder inorder successor successor (which (which has has zero zero or or one one child), child), replace replace the the node's node's key key with with its its successor's successor's key, key, then then delete delete its its successor. successor

51 Runtime Analysis The time complexity of all these operations is O(h), where h is the height of the tree. Represents the longest path we can take. In the best case, h = O(log n) and all operations take time O(log n). In the worst case, h = Θ(n) and some operations will take time Θ(n). Challenge: How do you eficiently keep the height of a tree low?

52 A Glimpse of Red/Black Trees

53 Red/Black Trees A red/black tree is a BST with the following properties: 110 Every node is either red or black. The root is black No red node has a red child. Every root-null path in the tree passes through the same number of black nodes

54 Red/Black Trees A red/black tree is a BST with the following properties: Every node is either red or black. The root is black No red node has a red child. Every root-null path in the tree passes through the same number of black nodes

55 Red/Black Trees A red/black tree is a BST with the following properties: Every node is either red or black. The root is black No red node has a red child. Every root-null path in the tree passes through the same number of black nodes

56 Red/Black Trees A red/black tree is a BST with the following properties: Every node is either red or black. The root is black No red node has a red child. Every root-null path in the tree passes through the same number of black nodes

57 Red/Black Trees A red/black tree is a BST with the following properties: Every node is either red or black. The root is black No red node has a red child. Every root-null path in the tree passes through the same number of black nodes

58 Red/Black Trees A red/black tree is a BST with the following properties: Every node is either red or black. The root is black No red node has a red child. Every root-null path in the tree passes through the same number of black nodes

59 Red/Black Trees Theorem: Any red/black tree with n nodes has height O(log n). We could prove this now, but there's a much simpler proof of this we'll see later on. Given a fxed red/black tree, lookups can be done in time O(log n).

60 Mutating Red/Black Trees

61 Mutating Red/Black Trees

62 Mutating Red/Black Trees

63 Mutating Red/Black Trees

64 Mutating Red/Black Trees

65 Mutating Red/Black Trees

66 Mutating Red/Black Trees

67 Mutating Red/Black Trees

68 Mutating Red/Black Trees What What are are we we supposed supposed to to do do with with this this new new node? node?

69 Mutating Red/Black Trees

70 Mutating Red/Black Trees

71 Mutating Red/Black Trees

72 Mutating Red/Black Trees

73 Mutating Red/Black Trees

74 Mutating Red/Black Trees

75 Mutating Red/Black Trees

76 Mutating Red/Black Trees How How do do we we fx fx up up the the black-height property? property?

77 Fixing Up Red/Black Trees The Good News: After doing an insertion or deletion, can locally modify a red/black tree in time O(log n) to fx up the red/black properties. The Bad News: There are a lot of cases to consider and they're not trivial. Some questions: How do you memorize / remember all the diferent types of rotations? How on earth did anyone come up with red/black trees in the frst place?

78 B-Trees

79 Generalizing BSTs In a binary search tree, each node stores a single key. That key splits the key space into two pieces, and each subtree stores the keys in those halves Values less than two Values greater than two

80 Generalizing BSTs In a multiway search tree, each node stores an arbitrary number of keys in sorted order In a node with k keys splits the key space into k + 1 pieces, and each subtree stores the keys in those pieces.

81 One Solution: B-Trees A B-tree of order b is a multiway search tree with the following properties: All leaf nodes are stored at the same depth. All non-root nodes have between b 1 and 2b 1 keys. The root node has been 1 and 2b 1 keys. All root-null paths through the tree pass through the same number of nodes. 43 B-tree B-tree of of order order

82 One Solution: B-Trees A B-tree of order b is a multiway search tree with the following properties: All leaf nodes are stored at the same depth. All non-root nodes have between b 1 and 2b 1 keys. The root node has been 1 and 2b 1 keys. All root-null paths through the tree pass through the same number of nodes B-tree B-tree of of order order

83 2-3-4 Trees A tree is a B-tree of order 2. The rules for trees are really simple: All leaf nodes are stored at the same depth. All nodes have between 1 and 3 keys (between 2 and 4 children). All root-null paths through the tree pass through the same number of nodes. These fellas will make a number of appearances later on. Stay tuned!

84 The Tradeof Because B-tree nodes can have multiple keys, when performing a search, insertion, or deletion, we have to spend more work inside each node. Insertion and deletion can be expensive for large b, we might have to shufe thousands or millions of keys over! Why would you use a B-tree?

85 Memory Tradeofs There is an enormous tradeof between speed and size in memory. SRAM (the stuf registers are made of) is fast but very expensive: Can keep up with processor speeds in the GHz. As of 2010, cost is $5/MB. (Anyone know a good source for a more recent price?) Good luck buying 1TB of the stuf! Hard disks are cheap but very slow: As of 2018, you can buy a 4TB hard drive for about $100. As of 2018, good disk seek times for magnetic drives are measured in ms (about two to four million times slower than a processor cycle!)

86 The Memory Hierarchy Idea: Try to get the best of all worlds by using multiple types of memory.

87 The Memory Hierarchy Idea: Try to get the best of all worlds by using multiple types of memory. Registers L1 Cache L2 Cache Main Memory Hard Disk Network (The Cloud) 256B - 8KB 16KB 64KB 1MB - 4MB 4GB 256GB 1TB+ Lots ns 1ns 5ns 5ns 25ns 25ns 100ns 3 10ms ms

88 Why B-Trees? Because B-trees have a huge branching factor, they're great for on-disk storage. Disk block reads/writes are glacially slow. The high branching factor minimizes the number of blocks to read during a lookup. Extra work scanning inside a block ofset by these savings. Major use cases for B-trees and their variants (B +-trees, H-trees, etc.) include databases (huge amount of data stored on disk); file systems (ext4, N4T,S, Re,S); and, recently, in-memory data structures (due to cache efects).

89 The Height of a B-Tree What is the maximum possible height of a B-tree of order b? 1 1 b 1 b 1 2(b - 1) b 1 b 1 b 1 b 1 2b(b - 1) b 1 b 1 b 1 b 1 2b 2 (b - 1) b 1 b 1 b 1 2b h-1 (b - 1)

90 The Height of a B-Tree Theorem: The maximum height of a B-tree of order b containing n nodes is log b ((n + 1) / 2). Proof: N4umber of nodes n in a B-tree of height h is guaranteed to be at least = 1 + 2(b 1) + 2b(b 1) + 2b 2 (b 1) + + 2b h-1 (b 1) = 1 + 2(b 1)(1 + b + b b h-1 ) = 1 + 2(b 1)((b h 1) / (b 1)) = 1 + 2(b h 1) = 2b h 1. Solving n = 2b h 1 yields h = log b ((n + 1) / 2). Corollary: B-trees of order b have height Θ(logb n).

91 Searching in a B-Tree Doing a search in a B-tree involves searching the root node for the key, and if it's not found, recursively exploring the correct child. Using binary search within a given node, can find the key or the correct child in time O(log number-of-keys). Repeat this process O(tree-height) times. Time complexity is = O(log number-of-keys tree-height) = O(log b log b n) = O(log b (log n / log b)) = O(log n) Requires reading O(logb n) blocks; this more directly accounts for the total runtime.

92 Searching in a B-Tree Doing a search in a B-tree involves searching the root node for the key, and if it's not found, recursively exploring the correct child. Using binary search within a given node, can find the key or the correct child in time O(log number-of-keys). Repeat this process O(tree-height) times. Time complexity is = O(log number-of-keys tree-height) = O(log b log b n) = O(log b (log n / log b)) = O(log n) Requires reading O(logb n) blocks; this more directly accounts for the total runtime.

93 B-Trees are Simple Because nodes in a B-tree can store multiple keys, most insertions or deletions are straightforward. Here's a B-tree with b = 3 (nodes have between 2 and 5 keys):

94 B-Trees are Simple Because nodes in a B-tree can store multiple keys, most insertions or deletions are straightforward. Here's a B-tree with b = 3 (nodes have between 2 and 5 keys):

95 B-Trees are Simple Because nodes in a B-tree can store multiple keys, most insertions or deletions are straightforward. Here's a B-tree with b = 3 (nodes have between 2 and 5 keys):

96 B-Trees are Simple Because nodes in a B-tree can store multiple keys, most insertions or deletions are straightforward. Here's a B-tree with b = 3 (nodes have between 2 and 5 keys):

97 B-Trees are Simple Because nodes in a B-tree can store multiple keys, most insertions or deletions are straightforward. Here's a B-tree with b = 3 (nodes have between 2 and 5 keys):

98 B-Trees are Simple Because nodes in a B-tree can store multiple keys, most insertions or deletions are straightforward. Here's a B-tree with b = 3 (nodes have between 2 and 5 keys):

99 B-Trees are Simple Because nodes in a B-tree can store multiple keys, most insertions or deletions are straightforward. Here's a B-tree with b = 3 (nodes have between 2 and 5 keys):

100 B-Trees are Simple Because nodes in a B-tree can store multiple keys, most insertions or deletions are straightforward. Here's a B-tree with b = 3 (nodes have between 2 and 5 keys):

101 B-Trees are Simple Because nodes in a B-tree can store multiple keys, most insertions or deletions are straightforward. Here's a B-tree with b = 3 (nodes have between 2 and 5 keys):

102 B-Trees are Simple Because nodes in a B-tree can store multiple keys, most insertions or deletions are straightforward. Here's a B-tree with b = 3 (nodes have between 2 and 5 keys):

103 B-Trees are Simple Because nodes in a B-tree can store multiple keys, most insertions or deletions are straightforward. Here's a B-tree with b = 3 (nodes have between 2 and 5 keys):

104 B-Trees are Simple Because nodes in a B-tree can store multiple keys, most insertions or deletions are straightforward. Here's a B-tree with b = 3 (nodes have between 2 and 5 keys):

105 B-Trees are Simple Because nodes in a B-tree can store multiple keys, most insertions or deletions are straightforward. Here's a B-tree with b = 3 (nodes have between 2 and 5 keys):

106 B-Trees are Simple Because nodes in a B-tree can store multiple keys, most insertions or deletions are straightforward. Here's a B-tree with b = 3 (nodes have between 2 and 5 keys):

107 B-Trees are Simple Because nodes in a B-tree can store multiple keys, most insertions or deletions are straightforward. Here's a B-tree with b = 3 (nodes have between 2 and 5 keys):

108 B-Trees are Simple Because nodes in a B-tree can store multiple keys, most insertions or deletions are straightforward. Here's a B-tree with b = 3 (nodes have between 2 and 5 keys):

109 The Trickier Cases What happens if you insert a key into a node that's too full? Idea: Split the node in two and propagate upward. Here's a tree (each node has 1 to 3 keys)

110 The Trickier Cases What happens if you insert a key into a node that's too full? Idea: Split the node in two and propagate upward. Here's a tree (each node has 1 to 3 keys)

111 The Trickier Cases What happens if you insert a key into a node that's too full? Idea: Split the node in two and propagate upward. Here's a tree (each node has 1 to 3 keys)

112 The Trickier Cases What happens if you insert a key into a node that's too full? Idea: Split the node in two and propagate upward. Here's a tree (each node has 1 to 3 keys)

113 The Trickier Cases What happens if you insert a key into a node that's too full? Idea: Split the node in two and propagate upward. Here's a tree (each node has 1 to 3 keys)

114 The Trickier Cases What happens if you insert a key into a node that's too full? Idea: Split the node in two and propagate upward. Here's a tree (each node has 1 to 3 keys)

115 The Trickier Cases What happens if you insert a key into a node that's too full? Idea: Split the node in two and propagate upward. Here's a tree (each node has 1 to 3 keys)

116 The Trickier Cases What happens if you insert a key into a node that's too full? Idea: Split the node in two and propagate upward. Here's a tree (each node has 1 to 3 keys)

117 The Trickier Cases What happens if you insert a key into a node that's too full? Idea: Split the node in two and propagate upward. Here's a tree (each node has 1 to 3 keys)

118 The Trickier Cases What happens if you insert a key into a node that's too full? Idea: Split the node in two and propagate upward. Here's a tree (each node has 1 to 3 keys)

119 The Trickier Cases What happens if you insert a key into a node that's too full? Idea: Split the node in two and propagate upward. Here's a tree (each node has 1 to 3 keys)

120 The Trickier Cases What happens if you insert a key into a node that's too full? Idea: Split the node in two and propagate upward. Here's a tree (each node has 1 to 3 keys)

121 The Trickier Cases What happens if you insert a key into a node that's too full? Idea: Split the node in two and propagate upward. Here's a tree (each node has 1 to 3 keys). 41 Note: Note: B-trees B-trees grow grow upward, upward, not not downward. downward

122 Inserting into a B-Tree To insert a key into a B-tree: Search for the key, insert at the last-visited leaf node. If the leaf is too big (contains 2b keys): Split the node into two nodes of size b each. Remove the largest key of the first block and make it the parent of both blocks. Recursively add that node to the parent, possibly triggering more upward splitting. Time complexity: O(b) work per level and O(logb n) levels. Total work: O(b logb n) In terms of blocks read: O(logb n)

123 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree:

124 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree:

125 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree:

126 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree:

127 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree:

128 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree:

129 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree: 46?

130 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree:

131 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree:

132 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree:

133 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree: 46?

134 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree:

135 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree:

136 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree:

137 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree:

138 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree:

139 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree:

140 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree:

141 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree:

142 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree: 46?

143 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree:

144 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree:

145 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree:

146 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree:

147 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree:

148 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree:

149 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree:

150 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree: 46?

151 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree: 46?

152 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree: 46?

153 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree:?

154 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree:?

155 The Trickier Cases How do you delete from a leaf that has only b 1 keys? Idea: Steal keys from an adjacent nodes, or merge the nodes if both are empty. Again, a tree:

156 Deleting from a B-Tree If not in a leaf, replace the key with its successor from a leaf and delete out of a leaf. To delete a key from a node: If the node has more than b 1 keys, or if the node is the root, just remove the key. Otherwise, find a sibling node whose shared parent is p. If that sibling has more than b 1 keys, move the max/min key from that sibling into p's place and p down into the current node, then remove the key. Otherwise, fuse the node and its sibling into a single node by adding p into the block, then recursively remove p from the parent node. Work done is O(b log b n): O(b) work per level times O(log b n) total levels. Requires O(log b n) block reads/writes.

157 Time-Out for Announcements!

158 Problem Sets Problem Set One solutions are now available up on the course website. We re working on getting them graded stay tuned! Problem Set Two is due next Tuesday. Have questions? Ask them on Piazza or stop by our ofice hours!

159 Back to CS166!

160 So... red/black trees?

161 Red/Black Trees A red/black tree is a BST with the following properties: 110 Every node is either red or black. The root is black No red node has a red child. Every root-null path in the tree passes through the same number of black nodes

162 Red/Black Trees A red/black tree is a BST with the following properties: Every node is either red or black. The root is black No red node has a red child. Every root-null path in the tree passes through the same number of black nodes.

163 Red/Black Trees A red/black tree is a BST with the following properties: Every node is either red or black. The root is black. No red node has a red child. Every root-null path in the tree passes through the same number of black nodes

164 Red/Black Trees A red/black tree is a BST with the following properties: Every node is either red or black. The root is black. No red node has a red child. Every root-null path in the tree passes through the same number of black nodes

165 Data Structure Isometries Red/black trees are an isometry of trees; they represent the structure of trees in a diferent way. Many data structures can be designed and analyzed in the same way. Huge advantage: Rather than memorizing a complex list of red/black tree rules, just think about what the equivalent operation on the corresponding tree would be and simulate it with color fips and rotations.

166 The Height of a Red/Black Tree Theorem: Any red/black tree with n nodes has height O(log n). Proof: Contract all red nodes into their parent nodes to convert the red/black tree into a tree. This decreases the height of the tree by at most a factor of two. The resulting tree has height O(log n), so the original red/black tree has height 2 O(log n) = O(log n).

167 Exploring the Isometry Nodes in a tree are classifed into types based on the number of children they can have. 2-nodes have one key (two children). 3-nodes have two keys (three children). 4-nodes have three keys (four children). How might these nodes be represented?

168 Exploring the Isometry k₁ k₁ k₁ k₂ k₁ k₂ k₂ k₁ k₂ k₁ k₂ k₃ k₁ k₃

169 Using the Isometry

170 Using the Isometry

171 Using the Isometry

172 Using the Isometry

173 Using the Isometry

174 Using the Isometry

175 Using the Isometry

176 Using the Isometry

177 Using the Isometry

178 Using the Isometry

179 Using the Isometry

180 Using the Isometry

181 Using the Isometry

182 Using the Isometry

183 Using the Isometry

184 Using the Isometry

185 Using the Isometry

186 Using the Isometry

187 Using the Isometry

188 Using the Isometry

189 Using the Isometry

190 Red/Black Tree Insertion Rule #1: When inserting a node, if its parent is black, make the node red and stop. Justifcation: This simulates inserting a key into an existing 2-node or 3-node.

191 Using the Isometry

192 Using the Isometry

193 Using the Isometry

194 Using the Isometry

195 Using the Isometry

196 Using the Isometry

197 Using the Isometry

198 Using the Isometry

199 Using the Isometry

200 Using the Isometry

201 Using the Isometry

202 Using the Isometry We We need need to to Change Change the the colors colors of of the the nodes, nodes, and and Move Move the the nodes nodes around around in in the the tree. tree.

203 Tree Rotations B Rotate Right A Rotate Left A B >B <A <A >A <B >A <B >B

204 3 apply rotation This This applies applies any any time time we're we're inserting inserting a new new node node into into the the middle middle of of a 3-node. 3-node. 5 4 By By making making observations observations like like these, these, we we can can determine determine how how to to update update a red/black red/black tree tree after after an an insertion. insertion. 4 apply rotation 3 5 change colors 3 5

205 Using the Isometry

206 Using the Isometry

207 Using the Isometry

208 Using the Isometry

209 Using the Isometry

210 Using the Isometry

211 Using the Isometry

212 Using the Isometry

213 Using the Isometry

214 Using the Isometry

215 Using the Isometry

216 Using the Isometry

217 Using the Isometry

218 Using the Isometry

219 Using the Isometry

220 Using the Isometry

221 Using the Isometry

222 Using the Isometry

223 Using the Isometry

224 Using the Isometry Two Two steps: steps: Split Split the the 5-node 5-node into into a 2-node 2-node and and a 3-node. 3-node Insert Insert the the new new parent parent of of the the two two nodes nodes into into the the parent parent node. node.

225 change colors

226 Using the Isometry

227 Using the Isometry

228 Using the Isometry

229 Using the Isometry

230 Using the Isometry

231 Using the Isometry

232 Using the Isometry

233 Using the Isometry

234 Building Up Rules All of the crazy insertion rules on red/black trees make perfect sense if you connect it back to trees. There are lots of cases to consider because there are many diferent ways you can insert into a red/black tree. Main point: Simulating the insertion of a key into a node takes time O(1) in all cases. Therefore, since trees support O(log n) insertions, red/black trees support O(log n) insertions. The same is true of deletions.

235 My Advice Do know how to do B-tree insertions and deletions. You can derive these easily if you remember to split and join nodes. Do remember the rules for red/black trees and B-trees. These are useful for proving bounds and deriving results. Do remember the isometry between red/black trees and trees. Gives immediate intuition for all the red/black tree operations. Don't memorize the red/black rotations and color fips. This is rarely useful. If you're coding up a red/black tree, just fip open CLRS and translate the pseudocode.

236 Next Time Augmented Trees Building data structures on top of balanced BSTs. Splitting and Joining Trees Two powerful operations on balanced trees.

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

2-3 Tree. Outline B-TREE. catch(...){ printf( Assignment::SolveProblem() AAAA!); } ADD SLIDES ON DISJOINT SETS Outline catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } Balanced Search Trees 2-3 Trees 2-3-4 Trees Slide 4 Why care about advanced implementations? Same entries, different insertion sequence:

More information

Balanced Trees Part Two

Balanced Trees Part Two Balanced Trees Part Two Outline for Today Recap from Last Time Review of B-trees, 2-3-4 trees, and red/black trees. Order Statistic Trees BSTs with indexing. Augmented Binary Search Trees Building new

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

(2,4) Trees. 2/22/2006 (2,4) Trees 1

(2,4) Trees. 2/22/2006 (2,4) Trees 1 (2,4) Trees 9 2 5 7 10 14 2/22/2006 (2,4) Trees 1 Outline and Reading Multi-way search tree ( 10.4.1) Definition Search (2,4) tree ( 10.4.2) Definition Search Insertion Deletion Comparison of dictionary

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms CS245-2008S-19 B-Trees David Galles Department of Computer Science University of San Francisco 19-0: Indexing Operations: Add an element Remove an element Find an element,

More information

x-fast and y-fast Tries

x-fast and y-fast Tries x-fast and y-fast Tries Problem Problem Set Set 7 due due in in the the box box up up front. front. That's That's the the last last problem problem set set of of the the quarter! quarter! Outline for Today

More information

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25 Multi-way Search Trees (Multi-way Search Trees) Data Structures and Programming Spring 2017 1 / 25 Multi-way Search Trees Each internal node of a multi-way search tree T: has at least two children contains

More information

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1 (2,4) Trees 9 2 5 7 10 14 2004 Goodrich, Tamassia (2,4) Trees 1 Multi-Way Search Tree A multi-way search tree is an ordered tree such that Each internal node has at least two children and stores d -1 key-element

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

x-fast and y-fast Tries

x-fast and y-fast Tries x-fast and y-fast Tries Outline for Today Bitwise Tries A simple ordered dictionary for integers. x-fast Tries Tries + Hashing y-fast Tries Tries + Hashing + Subdivision + Balanced Trees + Amortization

More information

Outline for Today. Analyzing data structures over the long term. Why could we construct them in time O(n)? A simple and elegant queue implementation.

Outline for Today. Analyzing data structures over the long term. Why could we construct them in time O(n)? A simple and elegant queue implementation. Amortized Analysis Outline for Today Amortized Analysis Analyzing data structures over the long term. Cartesian Trees Revisited Why could we construct them in time O(n)? The Two-Stack Queue A simple and

More information

Outline for Today. Why could we construct them in time O(n)? Analyzing data structures over the long term.

Outline for Today. Why could we construct them in time O(n)? Analyzing data structures over the long term. Amortized Analysis Outline for Today Euler Tour Trees A quick bug fix from last time. Cartesian Trees Revisited Why could we construct them in time O(n)? Amortized Analysis Analyzing data structures over

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

B-trees. It also makes sense to have data structures that use the minimum addressable unit as their base node size.

B-trees. It also makes sense to have data structures that use the minimum addressable unit as their base node size. B-trees Balanced BSTs such as RBTs are great for data structures that can fit into the main memory of the computer. But what happens when we need to use external storage? Here are some approximate speeds

More information

CSE 326: Data Structures B-Trees and B+ Trees

CSE 326: Data Structures B-Trees and B+ Trees Announcements (2/4/09) CSE 26: Data Structures B-Trees and B+ Trees Midterm on Friday Special office hour: 4:00-5:00 Thursday in Jaech Gallery (6 th floor of CSE building) This is in addition to my usual

More information

Friday Four Square! 4:15PM, Outside Gates

Friday Four Square! 4:15PM, Outside Gates Binary Search Trees Friday Four Square! 4:15PM, Outside Gates Implementing Set On Monday and Wednesday, we saw how to implement the Map and Lexicon, respectively. Let's now turn our attention to the Set.

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. 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

TREES. Trees - Introduction

TREES. Trees - Introduction TREES Chapter 6 Trees - Introduction All previous data organizations we've studied are linear each element can have only one predecessor and successor Accessing all elements in a linear sequence is O(n)

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

B-Trees and External Memory

B-Trees and External Memory Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 and External Memory 1 1 (2, 4) Trees: Generalization of BSTs Each internal node

More information

CS F-11 B-Trees 1

CS F-11 B-Trees 1 CS673-2016F-11 B-Trees 1 11-0: Binary Search Trees Binary Tree data structure All values in left subtree< value stored in root All values in the right subtree>value stored in root 11-1: Generalizing BSTs

More information

Red-black trees (19.5), B-trees (19.8), trees

Red-black trees (19.5), B-trees (19.8), trees Red-black trees (19.5), B-trees (19.8), 2-3-4 trees Red-black trees A red-black tree is a balanced BST It has a more complicated invariant than an AVL tree: Each node is coloured red or black A red node

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

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

Range Minimum Queries Part Two

Range Minimum Queries Part Two Range Minimum Queries Part Two Recap from Last Time The RMQ Problem The Range Minimum Query (RMQ) problem is the following: Given a fied array A and two indices i j, what is the smallest element out of

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

B-Trees and External Memory

B-Trees and External Memory Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 B-Trees and External Memory 1 (2, 4) Trees: Generalization of BSTs Each internal

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

Search Trees. COMPSCI 355 Fall 2016

Search Trees. COMPSCI 355 Fall 2016 Search Trees COMPSCI 355 Fall 2016 2-4 Trees Search Trees AVL trees Red-Black trees Splay trees Multiway Search Trees (2, 4) Trees External Search Trees (optimized for reading and writing large blocks)

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

B-Trees. Disk Storage. What is a multiway tree? What is a B-tree? Why B-trees? Insertion in a B-tree. Deletion in a B-tree

B-Trees. Disk Storage. What is a multiway tree? What is a B-tree? Why B-trees? Insertion in a B-tree. Deletion in a B-tree B-Trees Disk Storage What is a multiway tree? What is a B-tree? Why B-trees? Insertion in a B-tree Deletion in a B-tree Disk Storage Data is stored on disk (i.e., secondary memory) in blocks. A block is

More information

Motivation for B-Trees

Motivation for B-Trees 1 Motivation for Assume that we use an AVL tree to store about 20 million records We end up with a very deep binary tree with lots of different disk accesses; log2 20,000,000 is about 24, so this takes

More information

C SCI 335 Software Analysis & Design III Lecture Notes Prof. Stewart Weiss Chapter 4: B Trees

C SCI 335 Software Analysis & Design III Lecture Notes Prof. Stewart Weiss Chapter 4: B Trees B-Trees AVL trees and other binary search trees are suitable for organizing data that is entirely contained within computer memory. When the amount of data is too large to fit entirely in memory, i.e.,

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

Balanced Binary Search Trees. Victor Gao

Balanced Binary Search Trees. Victor Gao Balanced Binary Search Trees Victor Gao OUTLINE Binary Heap Revisited BST Revisited Balanced Binary Search Trees Rotation Treap Splay Tree BINARY HEAP: REVIEW A binary heap is a complete binary tree such

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

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 11: Binary Search Trees MOUNA KACEM mouna@cs.wisc.edu Fall 2018 General Overview of Data Structures 2 Introduction to trees 3 Tree: Important non-linear data structure

More information

M-ary Search Tree. B-Trees. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. Maximum branching factor of M Complete tree has height =

M-ary Search Tree. B-Trees. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. Maximum branching factor of M Complete tree has height = M-ary Search Tree B-Trees Section 4.7 in Weiss Maximum branching factor of M Complete tree has height = # disk accesses for find: Runtime of find: 2 Solution: B-Trees specialized M-ary search trees Each

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

Ch04 Balanced Search Trees

Ch04 Balanced Search Trees Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 05 Ch0 Balanced Search Trees v 3 8 z Why care about advanced implementations? Same entries,

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

Lecture 11: Multiway and (2,4) Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

Lecture 11: Multiway and (2,4) Trees. Courtesy to Goodrich, Tamassia and Olga Veksler Lecture 11: Multiway and (2,4) Trees 9 2 5 7 10 14 Courtesy to Goodrich, Tamassia and Olga Veksler Instructor: Yuzhen Xie Outline Multiway Seach Tree: a new type of search trees: for ordered d dictionary

More information

Outline for Today. How can we speed up operations that work on integer data? A simple data structure for ordered dictionaries.

Outline for Today. How can we speed up operations that work on integer data? A simple data structure for ordered dictionaries. van Emde Boas Trees Outline for Today Data Structures on Integers How can we speed up operations that work on integer data? Tiered Bitvectors A simple data structure for ordered dictionaries. van Emde

More information

CMPS 2200 Fall 2017 Red-black trees Carola Wenk

CMPS 2200 Fall 2017 Red-black trees Carola Wenk CMPS 2200 Fall 2017 Red-black trees Carola Wenk Slides courtesy of Charles Leiserson with changes by Carola Wenk 9/13/17 CMPS 2200 Intro. to Algorithms 1 Dynamic Set A dynamic set, or dictionary, is a

More information

Multiway Search Trees. Multiway-Search Trees (cont d)

Multiway Search Trees. Multiway-Search Trees (cont d) Multiway Search Trees Each internal node v of a multi-way search tree T has at least two children contains d-1 items, where d is the number of children of v an item is of the form (k i,x i ) for 1 i d-1,

More information

Search Trees - 1 Venkatanatha Sarma Y

Search Trees - 1 Venkatanatha Sarma Y Search Trees - 1 Lecture delivered by: Venkatanatha Sarma Y Assistant Professor MSRSAS-Bangalore 11 Objectives To introduce, discuss and analyse the different ways to realise balanced Binary Search Trees

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

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

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

Module 4: Dictionaries and Balanced Search Trees

Module 4: Dictionaries and Balanced Search Trees Module 4: Dictionaries and Balanced Search Trees CS 24 - Data Structures and Data Management Jason Hinek and Arne Storjohann Based on lecture notes by R. Dorrigiv and D. Roche David R. Cheriton School

More information

Advanced Set Representation Methods

Advanced Set Representation Methods Advanced Set Representation Methods AVL trees. 2-3(-4) Trees. Union-Find Set ADT DSA - lecture 4 - T.U.Cluj-Napoca - M. Joldos 1 Advanced Set Representation. AVL Trees Problem with BSTs: worst case operation

More information

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya CS62: Foundations of Algorithm Design and Machine Learning Sourangshu Bhattacharya Balanced search trees Balanced search tree: A search-tree data structure for which a height of O(lg n) is guaranteed when

More information

M-ary Search Tree. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. B-Trees (4.7 in Weiss)

M-ary Search Tree. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. B-Trees (4.7 in Weiss) M-ary Search Tree B-Trees (4.7 in Weiss) Maximum branching factor of M Tree with N values has height = # disk accesses for find: Runtime of find: 1/21/2011 1 1/21/2011 2 Solution: B-Trees specialized M-ary

More information

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya CS62: Foundations of Algorithm Design and Machine Learning Sourangshu Bhattacharya Binary Search Tree - Best Time All BST operations are O(d), where d is tree depth minimum d is d = ëlog for a binary tree

More information

TREES AND ORDERS OF GROWTH 7

TREES AND ORDERS OF GROWTH 7 TREES AND ORDERS OF GROWTH 7 COMPUTER SCIENCE 61A October 17, 2013 1 Trees In computer science, trees are recursive data structures that are widely used in various settings. This is a diagram of a simple

More information

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures COMP 3170 - Analysis of Algorithms & Data Structures Shahin Kamali Lecture 9 - Jan. 22, 2018 CLRS 12.2, 12.3, 13.2, read problem 13-3 University of Manitoba COMP 3170 - Analysis of Algorithms & Data Structures

More information

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

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

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

B-Trees. Introduction. Definitions

B-Trees. Introduction. Definitions 1 of 10 B-Trees Introduction A B-tree is a specialized multiway tree designed especially for use on disk. In a B-tree each node may contain a large number of keys. The number of subtrees of each node,

More information

CS Fall 2010 B-trees Carola Wenk

CS Fall 2010 B-trees Carola Wenk CS 3343 -- Fall 2010 B-trees Carola Wenk 10/19/10 CS 3343 Analysis of Algorithms 1 External memory dictionary Task: Given a large amount of data that does not fit into main memory, process it into a dictionary

More information

If you took your exam home last time, I will still regrade it if you want.

If you took your exam home last time, I will still regrade it if you want. Some Comments about HW2: 1. You should have used a generic node in your structure one that expected an Object, and not some other type. 2. Main is still too long for some people 3. braces in wrong place,

More information

Maps; Binary Search Trees

Maps; Binary Search Trees Maps; Binary Search Trees PIC 10B Friday, May 20, 2016 PIC 10B Maps; Binary Search Trees Friday, May 20, 2016 1 / 24 Overview of Lecture 1 Maps 2 Binary Search Trees 3 Questions PIC 10B Maps; Binary Search

More information

Binary Trees, Binary Search Trees

Binary Trees, Binary Search Trees Binary Trees, Binary Search Trees Trees Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search, insert, delete)

More information

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge. Binary Trees 1 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the root, which are disjoint from

More information

Outline for Today. Euler Tour Trees Revisited. The Key Idea. Dynamic Graphs. Implementation Details. Dynamic connectivity in forests.

Outline for Today. Euler Tour Trees Revisited. The Key Idea. Dynamic Graphs. Implementation Details. Dynamic connectivity in forests. Dynamic Graphs Outline for Today Euler Tour Trees Revisited Dynamic connectivity in forests. The Key Idea Maintaining dynamic connectivity in general graphs Dynamic Graphs A data structure for dynamic

More information

2-3 and Trees. COL 106 Shweta Agrawal, Amit Kumar, Dr. Ilyas Cicekli

2-3 and Trees. COL 106 Shweta Agrawal, Amit Kumar, Dr. Ilyas Cicekli 2-3 and 2-3-4 Trees COL 106 Shweta Agrawal, Amit Kumar, Dr. Ilyas Cicekli Multi-Way Trees A binary search tree: One value in each node At most 2 children An M-way search tree: Between 1 to (M-1) values

More information

An AVL tree with N nodes is an excellent data. The Big-Oh analysis shows that most operations finish within O(log N) time

An AVL tree with N nodes is an excellent data. The Big-Oh analysis shows that most operations finish within O(log N) time B + -TREES MOTIVATION An AVL tree with N nodes is an excellent data structure for searching, indexing, etc. The Big-Oh analysis shows that most operations finish within O(log N) time The theoretical conclusion

More information

Trees. Reading: Weiss, Chapter 4. Cpt S 223, Fall 2007 Copyright: Washington State University

Trees. Reading: Weiss, Chapter 4. Cpt S 223, Fall 2007 Copyright: Washington State University Trees Reading: Weiss, Chapter 4 1 Generic Rooted Trees 2 Terms Node, Edge Internal node Root Leaf Child Sibling Descendant Ancestor 3 Tree Representations n-ary trees Each internal node can have at most

More information

What is a Multi-way tree?

What is a Multi-way tree? B-Tree Motivation for studying Multi-way and B-trees A disk access is very expensive compared to a typical computer instruction (mechanical limitations) -One disk access is worth about 200,000 instructions.

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

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures COMP 3170 - Analysis of Algorithms & Data Structures Shahin Kamali Lecture 9 - Jan. 22, 2018 CLRS 12.2, 12.3, 13.2, read problem 13-3 University of Manitoba 1 / 12 Binary Search Trees (review) Structure

More information

V Advanced Data Structures

V Advanced Data Structures V Advanced Data Structures B-Trees Fibonacci Heaps 18 B-Trees B-trees are similar to RBTs, but they are better at minimizing disk I/O operations Many database systems use B-trees, or variants of them,

More information

Multiway searching. In the worst case of searching a complete binary search tree, we can make log(n) page faults Everyone knows what a page fault is?

Multiway searching. In the worst case of searching a complete binary search tree, we can make log(n) page faults Everyone knows what a page fault is? Multiway searching What do we do if the volume of data to be searched is too large to fit into main memory Search tree is stored on disk pages, and the pages required as comparisons proceed may not be

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

Note that this is a rep invariant! The type system doesn t enforce this but you need it to be true. Should use repok to check in debug version.

Note that this is a rep invariant! The type system doesn t enforce this but you need it to be true. Should use repok to check in debug version. Announcements: Prelim tonight! 7:30-9:00 in Thurston 203/205 o Handed back in section tomorrow o If you have a conflict you can take the exam at 5:45 but can t leave early. Please email me so we have a

More information

Multiway Search Trees

Multiway Search Trees Multiway Search Trees Intuitive Definition A multiway search tree is one with nodes that have two or more children. Within each node is stored a given key, which is associated to an item we wish to access

More information

CMPS 2200 Fall 2017 B-trees Carola Wenk

CMPS 2200 Fall 2017 B-trees Carola Wenk CMPS 2200 Fall 2017 B-trees Carola Wenk 9/18/17 CMPS 2200 Intro. to Algorithms 1 External memory dictionary Task: Given a large amount of data that does not fit into main memory, process it into a dictionary

More information

CSE 373 OCTOBER 25 TH B-TREES

CSE 373 OCTOBER 25 TH B-TREES CSE 373 OCTOBER 25 TH S ASSORTED MINUTIAE Project 2 is due tonight Make canvas group submissions Load factor: total number of elements / current table size Can select any load factor (but since we don

More information

Lecture 6: Analysis of Algorithms (CS )

Lecture 6: Analysis of Algorithms (CS ) Lecture 6: Analysis of Algorithms (CS583-002) Amarda Shehu October 08, 2014 1 Outline of Today s Class 2 Traversals Querying Insertion and Deletion Sorting with BSTs 3 Red-black Trees Height of a Red-black

More information

CS 3343 Fall 2007 Red-black trees Carola Wenk

CS 3343 Fall 2007 Red-black trees Carola Wenk CS 3343 Fall 2007 Red-black trees Carola Wenk Slides courtesy of Charles Leiserson with small changes by Carola Wenk CS 334 Analysis of Algorithms 1 Search Trees A binary search tree is a binary tree.

More information

(2,4) Trees Goodrich, Tamassia. (2,4) Trees 1

(2,4) Trees Goodrich, Tamassia. (2,4) Trees 1 (2,4) Trees 9 2 5 7 10 14 (2,4) Trees 1 Multi-Way Search Tree ( 9.4.1) A multi-way search tree is an ordered tree such that Each internal node has at least two children and stores d 1 key-element items

More information

Algorithms for Memory Hierarchies Lecture 2

Algorithms for Memory Hierarchies Lecture 2 Algorithms for emory Hierarchies Lecture Lecturer: odari Sitchianva Scribes: Robin Rehrmann, ichael Kirsten Last Time External memory (E) model Scan(): O( ) I/Os Stacks / queues: O( 1 ) I/Os / elt ergesort:

More information

V Advanced Data Structures

V Advanced Data Structures V Advanced Data Structures B-Trees Fibonacci Heaps 18 B-Trees B-trees are similar to RBTs, but they are better at minimizing disk I/O operations Many database systems use B-trees, or variants of them,

More information

CS127: B-Trees. B-Trees

CS127: B-Trees. B-Trees CS127: B-Trees B-Trees 1 Data Layout on Disk Track: one ring Sector: one pie-shaped piece. Block: intersection of a track and a sector. Disk Based Dictionary Structures Use a disk-based method when the

More information

Introduction to Indexing 2. Acknowledgements: Eamonn Keogh and Chotirat Ann Ratanamahatana

Introduction to Indexing 2. Acknowledgements: Eamonn Keogh and Chotirat Ann Ratanamahatana Introduction to Indexing 2 Acknowledgements: Eamonn Keogh and Chotirat Ann Ratanamahatana Indexed Sequential Access Method We have seen that too small or too large an index (in other words too few or too

More information

Tree-Structured Indexes

Tree-Structured Indexes Tree-Structured Indexes Chapter 9 Database Management Systems, R. Ramakrishnan and J. Gehrke 1 Introduction As for any index, 3 alternatives for data entries k*: ➀ Data record with key value k ➁

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

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

Binary Trees

Binary Trees Binary Trees 4-7-2005 Opening Discussion What did we talk about last class? Do you have any code to show? Do you have any questions about the assignment? What is a Tree? You are all familiar with what

More information

COSC160: Data Structures Balanced Trees. Jeremy Bolton, PhD Assistant Teaching Professor

COSC160: Data Structures Balanced Trees. Jeremy Bolton, PhD Assistant Teaching Professor COSC160: Data Structures Balanced Trees Jeremy Bolton, PhD Assistant Teaching Professor Outline I. Balanced Trees I. AVL Trees I. Balance Constraint II. Examples III. Searching IV. Insertions V. Removals

More information

Binary heaps (chapters ) Leftist heaps

Binary heaps (chapters ) Leftist heaps Binary heaps (chapters 20.3 20.5) Leftist heaps Binary heaps are arrays! A binary heap is really implemented using an array! 8 18 29 20 28 39 66 Possible because of completeness property 37 26 76 32 74

More information

CS301 - Data Structures Glossary By

CS301 - Data Structures Glossary By CS301 - Data Structures Glossary By Abstract Data Type : A set of data values and associated operations that are precisely specified independent of any particular implementation. Also known as ADT Algorithm

More information

CSCI2100B Data Structures Trees

CSCI2100B Data Structures Trees CSCI2100B Data Structures Trees Irwin King king@cse.cuhk.edu.hk http://www.cse.cuhk.edu.hk/~king Department of Computer Science & Engineering The Chinese University of Hong Kong Introduction General Tree

More information

Trees. (Trees) Data Structures and Programming Spring / 28

Trees. (Trees) Data Structures and Programming Spring / 28 Trees (Trees) Data Structures and Programming Spring 2018 1 / 28 Trees A tree is a collection of nodes, which can be empty (recursive definition) If not empty, a tree consists of a distinguished node r

More information

catch(...){ printf( "Assignment::SolveProblem() AAAA!"); }

catch(...){ printf( Assignment::SolveProblem() AAAA!); } catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS 2-3 Tree www.serc.iisc.ernet.in/~viren/courses/2009/ SE286/2-3Trees-mod.ppt Outline q Balanced Search Trees 2-3 Trees

More information

CS102 Binary Search Trees

CS102 Binary Search Trees CS102 Binary Search Trees Prof Tejada 1 To speed up insertion, removal and search, modify the idea of a Binary Tree to create a Binary Search Tree (BST) Binary Search Trees Binary Search Trees have one

More information

Week 3 Web site:

Week 3 Web site: Week 3 Web site: https://pages.cs.wisc.edu/~deppeler/cs400/ (announcements and resources) Canvas: https://canvas.wisc.edu/ (modules, assignments, grades) Top Hat join code: X-Team Exercise #1: (in-class

More information

Hash Tables. CS 311 Data Structures and Algorithms Lecture Slides. Wednesday, April 22, Glenn G. Chappell

Hash Tables. CS 311 Data Structures and Algorithms Lecture Slides. Wednesday, April 22, Glenn G. Chappell Hash Tables CS 311 Data Structures and Algorithms Lecture Slides Wednesday, April 22, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks CHAPPELLG@member.ams.org 2005

More information

CMPS 2200 Fall 2015 Red-black trees Carola Wenk

CMPS 2200 Fall 2015 Red-black trees Carola Wenk CMPS 2200 Fall 2015 Red-black trees Carola Wenk Slides courtesy of Charles Leiserson with changes by Carola Wenk 9/9/15 CMPS 2200 Intro. to Algorithms 1 ADT Dictionary / Dynamic Set Abstract data type

More information