Advanced Implementations of Tables: Balanced Search Trees and Hashing
Balanced Search Trees � Binary search tree operations such as insert, delete, retrieve, etc. depend on the length of the path to the desired node � The path length can vary from log 2 (n+1) to O(n) depending on how balanced or unbalanced the tree is � The shape of the tree is determined by the values of the items and the order in which they were inserted Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 2
Examples 10 40 20 20 60 30 40 50 70 10 30 50 Can you get the same tree with 60 different insertion orders? 70 Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 3
2-3 Trees � Each internal node has two or three children � All leaves are at the same level � 2 children = 2-node, 3 children = 3-node Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 4
2-3 Trees (continued) � 2-3 trees are not binary trees (duh) � A 2-3 tree of height h always has at least 2 h -1 nodes � i.e. greater than or equal to a binary tree of height h � A 2-3 tree with n nodes has height less than or equal to log 2 ( n +1) � i.e less than or equal to the height of a full binary tree with n nodes Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 5
Definition of 2-3 Trees � T is a 2-3 tree of height h if � T is empty (height 0), OR � T is of the form r T L T R � Where r is a node that contains one data item and T L and T R are 2-3 trees, each of height h -1, and the search key of r is greater than any in T L and less than any in T R , OR Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 6
Definition of 2-3 Trees (continued) � T is of the form r T L T M T R � Where r is a node that contains two data items and T L , T M , and T R are 2-3 trees, each of height h -1, and the smaller search key of r is greater than any in T L and less than any in T M and the larger search key in r is greater than any in T M and smaller than any in T R . Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 7
Placing Data in a 2-3 tree 1. A 2-node must contain a single data item whose value is � greater than any in its left subtree, and � smaller than any in its right subtree � 2. A 3-mode must contain two data items, the smaller of which is � greater than any in its left subtree, and � smaller than any in its middle subtree, and � the greater of which is � greater than any in its middle subtree, and � smaller than any in its right subtree � 3. A leaf may contain either one or two data items Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 8
2-3 Node Code import searchkeys.*; public class Tree23Node { private KeyedItem smallitem; private KeyedItem largeItem; private Tree23Node leftChild; private Tree23Node middleChild; private Tree23Node rightChild; } Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 9
Traversing a 2-3 Tree � Just like a binary tree: preorder, inorder, postorder 50 90 20 70 120 150 10 30 40 60 80 100 110 130 140 160 Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 10
Searching a 2-3 tree � Same efficiency as a balanced binary search tree: O(logn) � BUT, easy to keep balanced, unlike binary search trees � This means that as you insert elements, the balance is easily maintained, and worst case performance remains O(logn) Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 11
Inserting 39 Into A 2-3 Tree 50 30 70 90 10 20 60 80 160 40 Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 12
Inserting 38 50 30 70 90 10 20 60 80 160 39 40 Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 13
Inserting 38 (continued) 50 30 70 90 10 20 60 80 160 38 39 40 Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 14
Inserting 37 50 30 39 70 90 10 20 60 80 160 38 40 Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 15
Inserting 36 50 30 39 70 90 10 20 60 80 160 37 38 40 Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 16
Inserting 36 (continued) 50 30 39 70 90 10 20 60 80 160 36 37 38 40 Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 17
Inserting 36 (continued) 50 30 37 39 70 90 36 38 40 10 20 60 80 160 Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 18
Inserting 75 37 50 30 39 70 90 36 38 10 20 40 60 80 160 Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 19
Inserting 77 37 50 30 39 70 90 36 38 40 60 10 20 75 80 160 Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 20
Inserting 77 (continued) 37 50 30 39 70 90 36 38 40 60 10 20 75 77 80 160 Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 21
Inserting 77 (continued) 37 50 30 39 70 77 90 36 38 40 60 10 20 75 160 80 Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 22
Inserting 77 (continued) 37 50 77 30 39 70 90 36 38 40 60 10 20 75 160 80 Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 23
Inserting 77 (continued) 50 37 77 30 39 70 90 36 38 40 60 10 20 75 160 80 Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 24
Inserting Into A 2-3 Tree � Insert into the leaf node in which the search key belongs � If the leaf has two values, stop � If the leaf has three values, split the node into two nodes with the smallest and largest values, and � Push the middle value into the parent node � Continue with the parent node until either � you push a value into a node that had only one value, or � you create a new root node Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 25
Deleting From A 2-3 Tree � The inverse of inserting � Delete the value (in a leaf), then � Merge empty nodes � If necessary, delete empty root Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 26
Redistribute I P L S L S P Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 27
Merge I L S S L Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 28
Redistribute II P L S L S P � a � b � c � d � a � b � c � d Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 29
Merge II L S S L � c � a � b � a � b � c Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 30
Delete S L S L � a � b � c � a � b � c Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 31
2-3 Trees: Results � Slightly more complicated than binary search trees, BUT � 2-3 Trees are always balanced � Every operation takes O(logn) Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 32
2-3-4 Trees � Slightly less complicated than 2-3 Trees � Each node can contain 1–3 values and have 1–4 children � Inserting � Split 4-nodes on the way down � Insert into leaf � Deleting: � Only delete from 3-node or 4-node Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 33
Red-Black Trees � 2-3 Trees are always balanced � O(logn) time for all operations � 2-3-4 Trees are always balanced � O(logn) time for all operations, and � Insertion and deletion can be done in a single pass from root to leaf � But, require slightly more storage per node � Red-Black Trees have the advantage of 2-3-4 trees, without the overhead � Represent the 2-3-4 Tree as a binary tree with colored references (red or black) Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 34
Red-Black Representation of a 4-Node M S M L S L � a � b � c � d � a � b � c � d Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 35
Red-Black Representation of a 3-Node L � c S S L � a � b � a � b � c S � a L � b � c Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 36
2-3-4 Tree 37 50 30 35 70 90 39 10 20 32 33 34 36 38 40 60 80 160 Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 37
Equivalent Red-Black Tree 37 30 50 20 35 39 90 10 33 36 38 40 70 100 32 34 60 80 Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 38
Red-Black Tree Node public class RBTreeNode { public static final int RED = 0; public static final int BLACK = 1; private KeyedItem item; private RBTreeNode leftChild; private RBTreeNode rightChild; private int leftColor; private int rightColor; } Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 39
Recommend
More recommend