Presentation is loading. Please wait.

Presentation is loading. Please wait.

Wednesday, April 18, 2018 Announcements… For Today…

Similar presentations


Presentation on theme: "Wednesday, April 18, 2018 Announcements… For Today…"— Presentation transcript:

1 Wednesday, April 18, 2018 Announcements… For Today…
Self-Balancing Search Trees Announcements… Bonus AVL Lab and Quicksort Reviews due Friday Final Exam Richard's Building April 20-25th. Exam review. Direct s to (w/netid) For Today… 11.3 Trees

2 Attendance Quiz #41 Self-Balancing Search Trees

3 An algorithm completes 32 iterations in 5 ms
An algorithm completes 32 iterations in 5 ms. Fill in the following times: O(1) O(log n) O(n) O(n2) O(n3) O(2n) O(n!) 32 5 33 64 128

4 Which STL Container to Use?

5 Which STL Container to Use?
Self-Balancing Search Trees

6 11.4, pgs. 656-672 11.4 2-3 Trees Searching a 2-3 Tree
Inserting an Item into a 2-3 Tree Analysis of 2-3 Trees and Comparison with Balanced Binary Trees Removal from a 2-3 Tree 11.4, pgs

7 2-3 Trees Self-Balancing Search Trees A 2-3 tree consists of nodes designated as either 2-nodes or 3- nodes A 2-node is the same as a binary search tree node: it contains a data field and references to two child nodes one child node contains data less than the node's data value the other child contains data greater than the node's data value A 3-node contains two data fields, ordered so that first is less than the second, and references to three children One child contains data values less than the first data field One child contains data values between the two data fields One child contains data values greater than the second data field All the leaves of a 2-3 tree are at the lowest level

8 2-3 Trees Self-Balancing Search Trees

9 Searching a 2-3 Tree Self-Balancing Search Trees
Searching a 2-3 tree is very similar to searching a binary search tree. if the local root is NULL Return NULL; the item is not in the tree. else if this is a 2-node if the item is equal to the data1 field Return the data1 field. else if the item is less than the data1 field Recursively search the left subtree. else Recursively search the right subtree. else // This is a 3-node else if the item is equal to the data2 field Return the data2 field. else if the item is less than the data2 field Recursively search the middle subtree.

10 Searching a 2-3 Tree To search for 13 Self-Balancing Search Trees 7 3
11, 15 1 5 9 13 17, 19

11 Searching a 2-3 Tree To search for 13 Self-Balancing Search Trees
Compare 13 and 7 7 3 11, 15 1 5 9 13 17, 19

12 Searching a 2-3 Tree To search for 13 Self-Balancing Search Trees
13 is greater than 7 7 3 11, 15 1 5 9 13 17, 19

13 Searching a 2-3 Tree To search for 13 Self-Balancing Search Trees 7
Compare 13 with 11 and 15 3 11, 15 1 5 9 13 17, 19

14 Searching a 2-3 Tree To search for 13 Self-Balancing Search Trees 7
13 is in between 11 and 15 11 < 13 < 15 3 11, 15 1 5 9 13 17, 19

15 Searching a 2-3 Tree To search for 13 Self-Balancing Search Trees 7 3
11, 15 1 5 9 13 17, 19 13 is in the middle child

16 Inserting an Item into a 2-3 Tree
Self-Balancing Search Trees A 2-3 tree maintains balance by being built from the bottom up, not the top down Instead of hanging a new node onto a leaf, we insert the new node into a leaf

17 Inserting an Item into a 2-3 Tree
Self-Balancing Search Trees Insert 15 7 3 11

18 Inserting an Item into a 2-3 Tree
Self-Balancing Search Trees Insert 15 7 3 11 Because this node is a 2-node, we insert directly into the node creating a 3-node

19 Inserting an Item into a 2-3 Tree
Self-Balancing Search Trees Insert 15 7 3 11, 15

20 Inserting an Item into a 2-3 Tree
Self-Balancing Search Trees Insert 17 7 3 11, 15

21 Inserting an Item into a 2-3 Tree
Self-Balancing Search Trees Insert 17 7 3 11, 15 Because we insert into leaves, 17 is virtually inserted into this node

22 Inserting an Item into a 2-3 Tree
Self-Balancing Search Trees Insert 17 7 3 11, 15, 17 Because a node can't store three values, the middle value propagates up to the 2-node parent and this leaf node splits into two new 2-nodes

23 Inserting an Item into a 2-3 Tree
Self-Balancing Search Trees Insert 17 7, 15 3 11 17

24 Inserting an Item into a 2-3 Tree
Self-Balancing Search Trees Insert 5, 10, 20 7, 15 3 11 17

25 Inserting an Item into a 2-3 Tree
Self-Balancing Search Trees Insert 5, 10, 20 7, 15 3, 5 11 17

26 Inserting an Item into a 2-3 Tree
Self-Balancing Search Trees Insert 5, 10, 20 7, 15 3, 5 10, 11 17

27 Inserting an Item into a 2-3 Tree
Self-Balancing Search Trees Insert 5, 10, 20 7, 15 3, 5 10, 11 17, 20

28 Inserting an Item into a 2-3 Tree
Self-Balancing Search Trees Insert 13 7, 15 3, 5 10, 11 17, 20

29 Inserting an Item into a 2-3 Tree
Self-Balancing Search Trees Insert 13 7, 15 3, 5 10, 11, 13 17, 20

30 Inserting an Item into a 2-3 Tree
Self-Balancing Search Trees Insert 13 7, 15 3, 5 10, 11, 13 17, 20 Since a node with three values is a virtual node, move the middle value up and split the remaining values into two nodes

31 Inserting an Item into a 2-3 Tree
Self-Balancing Search Trees Insert 13 7, 11, 15 3, 5 10 13 17, 20 Repeat

32 Inserting an Item into a 2-3 Tree
Self-Balancing Search Trees 11 Insert 13 7, 15 3, 5 10 13 17, 20 Move the middle value up

33 Inserting an Item into a 2-3 Tree
Self-Balancing Search Trees 11 Insert 13 7, 15 3, 5 10 13 17, 20 Split the remaining values into two nodes

34 Inserting an Item into a 2-3 Tree
Self-Balancing Search Trees 11 Insert 13 7 15 3, 5 10 13 17, 20 Split the remaining values into two nodes

35 “The quick brown fox jumps over the lazy dog”
Self-Balancing Search Trees “The quick brown fox jumps over the lazy dog”

36 “The quick brown fox jumps over the lazy dog”
Insertion Example Self-Balancing Search Trees “The quick brown fox jumps over the lazy dog” The

37 “The quick brown fox jumps over the lazy dog”
Insertion Example Self-Balancing Search Trees “The quick brown fox jumps over the lazy dog” The, quick

38 “The quick brown fox jumps over the lazy dog”
Insertion Example Self-Balancing Search Trees “The quick brown fox jumps over the lazy dog” The, brown, quick

39 “The quick brown fox jumps over the lazy dog”
Insertion Example Self-Balancing Search Trees “The quick brown fox jumps over the lazy dog” brown The quick

40 “The quick brown fox jumps over the lazy dog”
Insertion Example Self-Balancing Search Trees “The quick brown fox jumps over the lazy dog” brown The fox, quick

41 “The quick brown fox jumps over the lazy dog”
Insertion Example Self-Balancing Search Trees “The quick brown fox jumps over the lazy dog” brown The fox, jumps, quick

42 “The quick brown fox jumps over the lazy dog”
Insertion Example Self-Balancing Search Trees “The quick brown fox jumps over the lazy dog” The brown, jumps fox quick

43 “The quick brown fox jumps over the lazy dog”
Insertion Example Self-Balancing Search Trees “The quick brown fox jumps over the lazy dog” The brown, jumps fox over, quick

44 “The quick brown fox jumps over the lazy dog”
Insertion Example Self-Balancing Search Trees “The quick brown fox jumps over the lazy dog” The brown, jumps fox over, quick, the

45 “The quick brown fox jumps over the lazy dog”
Insertion Example Self-Balancing Search Trees “The quick brown fox jumps over the lazy dog” brown, jumps, quick The fox over the

46 “The quick brown fox jumps over the lazy dog”
Insertion Example Self-Balancing Search Trees “The quick brown fox jumps over the lazy dog” jumps brown quick The fox over the

47 “The quick brown fox jumps over the lazy dog”
Insertion Example Self-Balancing Search Trees “The quick brown fox jumps over the lazy dog” jumps brown quick The fox lazy, over the

48 “The quick brown fox jumps over the lazy dog”
Insertion Example Self-Balancing Search Trees “The quick brown fox jumps over the lazy dog” jumps brown quick The dog, fox lazy, over the

49 2-3 Trees vs. Balanced Binary Trees
Self-Balancing Search Trees 2-3 trees do not require the rotations needed for AVL and Red- Black trees. The number of items that a 2-3 tree of height h can hold is between 2h -1 (all 2 nodes) and 3h – 1 (all 3-nodes). Therefore, the height of a 2-3 tree is between log3 n and log2 n. The search time is O(log n) -- logarithms are all related by a constant factor, and constant factors are ignored in big-O notation.

50 11.4-5, pgs. 656-672 11.5 2-3-4 and B-Trees 2-3-4 Trees
Implementation of the Two_Three_Four_Tree Class Relating Trees to Red-Black Trees B-Trees 11.4-5, pgs

51 B-Tree A B-Tree is a "fat" self-balancing search tree.
Self-Balancing Search Trees A B-Tree is a "fat" self-balancing search tree. Self-balancing search trees (like AVL and Red Black Trees), assume that everything is in main memory. B-Trees work with huge amounts of data that cannot fit in main memory. Since disk access time is very high compared to main memory access time, B-Trees make accesses more productive. Using B-Trees reduces the number of disk accesses by putting maximum possible keys in a B-Tree node. Most of the tree operations (search, insert, delete, max, min, ..etc ) require O(h) disk accesses where h is height of the tree. Height of B-Trees is kept low such that the total disk accesses for most operations is significantly reduced. Generally, a B-Tree node size is kept equal to the disk block size.

52 B-Trees and Trees Self-Balancing Search Trees The 2-3 tree was the inspiration for the more general B-tree which allows up to n children per node, where n may be a very large number. 2-3-4 trees are a special case of the B-tree where order is fixed at 4. A node in a tree is called a 4-node. A 4-node has space for three data items and four children.

53 Properties of B-Tree All leaves are at same level.
Self-Balancing Search Trees All leaves are at same level. A B-Tree is defined by the term minimum degree ‘t’. The value of t depends upon disk block size. Every node except root must contain at least t-1 keys. Root may contain minimum 1 key. All nodes (including root) may contain at most 2t – 1 keys. Number of children of a node is equal to the number of keys in it plus 1. All keys of a node are sorted in increasing order. The child between two keys k1 and k2 contains all keys in range from k1 and k2. B-Tree grows and shrinks from root which is unlike Binary Search Tree. Binary Search Trees grow downward and also shrink from downward. Like other balanced Binary Search Trees, time complexity to search, insert and delete is O(Log n).

54


Download ppt "Wednesday, April 18, 2018 Announcements… For Today…"

Similar presentations


Ads by Google