make money fast! - department of computer science-copypasteads.com
TRANSCRIPT
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 1/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
Trees
Make Money Fast!
Stock
Fraud
Ponzi
Scheme
Bank
Robbery
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 2/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
What is a TreeIn computer science, atree is an abstract modelof a hierarchicalstructure
A tree consists of nodeswith a parent-childrelation
Applications:
! Organization charts
! File systems
! Programming
environments
Computers”R ”Us
Sales R&DManufacturing
Laptops DesktopsUS International
Europe Asia Canada
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 3/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
Formal Definition A tree T is a set of nodes such that T is! Empty, or
! Has a distinguished node r referred to asthe root and
! Each node v ! r has a unique parent nodew in T
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 4/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
subtree
Tree TerminologyRoot: node without parent (A)
Internal node: node with at leastone child (A, B, C, F)
External node (a.k.a. leaf ): node
without children (E, I, J, K, G, H, D) Ancestors of a node: parent,grandparent, grand-grandparent,etc.
Depth of a node: number ofancestors
Height of a tree: maximum depthof any node (3)
Descendant of a node: child,grandchild, grand-grandchild, etc.Siblings C & D
A
B DC
G HE F
I J K
Subtree: tree consisting ofa node and itsdescendants
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 5/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
More Terms A tree is ordered if the children of a node have alinear ordering
A edge of a tree T is a pair of nodes (u,v) such that uis a parent of v or vice-versa
A path in T is a seqence of nodes such that anyadjacent nodes share an edge
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 6/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
"
Linked Structure for Trees A node is represented byan object storing! Element
! Parent node
! List of children nodes
B
D A
C E
F
B
" "
A D F
"
C
"
E
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 7/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
Tree ADTWe use positions to abstractnodes
Generic methods:!
integer size()
! boolean isEmpty()
! Iterator iterator()
! Iterator positions()
Accessor methods:
! position root()
!
position parent(p)! positionIterator children(p)
Query methods:
! boolean isInternal(p)
! boolean isExternal(p)
! boolean isRoot(p)
Update method:! object replace (p, o)
Additional update methodsmay be defined by datastructures implementing theTree ADT
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 8/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
Preorder Traversal A traversal visits the nodes of atree in a systematic manner
In a preorder traversal, a node isvisited before its descendants
Application: print a structureddocument
Make Money Fast!
1. Motivations References2. Methods
2.1 StockFraud
2.2 PonziScheme
1.1 Greed 1.2 Avidity2.3 BankRobbery
1
2
3
5
46 7 8
9
Algorithm preOrder (v)
visit (v)
for each child w of v
preorder (w)
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 9/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
Parenthetical RepresentationParenthetical(T,v)! s = toString(v)
! if (T.isInternal(v))" s+= “(“
" for (Node<E> w: T.children(v))!
s += Parenthetical(T,w) + “ “;
"
s+=“)”
! return s
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 10/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
Postorder TraversalIn a postorder traversal, anode is visited after itsdescendants
Application: compute space
used by files in a directory andits subdirectories
Algorithm postOrder (v)
for each child w of v
postOrder (w)
visit (v)
cs16/
homeworks/todo.txt
1Kprograms/
DDR.java10K
Stocks.java25K
h1c.doc3K
h1nc.doc2K
Robot.java20K
9
3
1
7
2 4 5 6
8
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 11/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
Other PossibilitiesBreadth-first! visit all nodes at level d before visiting level
d+1
In-order/mixed
! visit some children, then parent, then
remaining children...
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 12/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
Some Natural Recursionsdepth: the number of ancestors of a tree
public static <E> int depth (Tree<E> T,Node<E> v) {
if (T.isRoot()) return 0;
else return 1 + depth(T,T.parent(v));
}
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 13/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
Some Natural Recursionsheight: the length of the longest path to an externalchild!
note height=depth of some external node
public static <E> int height (Tree<E> T, Node<E> v) {if (T.isExternal()) return 0;
int h = 0;
for (Node<E> w: T.children(v))
h=Math.max(h,height2(T,w));return 1+h; }
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 14/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
Binary Trees A binary tree is a tree with thefollowing properties:! Each internal node has at most two
children (exactly two for proper binary trees)
! The children of a node are anordered pair
We call the children of an internalnode left child and right child
Alternative recursive definition: abinary tree is either
! a tree consisting of a single node, or
! a tree whose root has an orderedpair of children, each of which is abinary tree
Applications:!
arithmetic expressions
! decision processes
! searching
A
B C
F GD E
H I
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 15/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
Arithmetic Expression TreeBinary tree associated with an arithmetic expression!
internal nodes: operators
!
external nodes: operands
Example: arithmetic expression tree for theexpression (2 # (a - 1) + (3 # b))
+
##
- 2
a 1
3 b
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 16/117
© 2004 Goodrich, Tamassia
Constructing an Expression
TreeGiven a postfix string, construct theexpression tree
Use a stack to hold intermediate results
Think of it like evaluation except the“operation” is to construct the tree
CS 600.226: Data Structures, Professor:Greg Hager
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 17/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
Decision TreeBinary tree associated with a decision process!
internal nodes: questions with yes/no answer
!
external nodes: decisions
Example: dining decision
Want a fast meal?
How about coffee? On expense account?
Starbucks Spike’s Al Forno Café Paragon
Yes No
Yes No Yes No
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 18/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
Properties of Proper Binary TreesNotationn number of nodes
e number of
external nodesi number of internal
nodes
h height
Properties:
! e = i + 1
! n = 2e 1
! h%
i
! h % (n 1)/2
! e%
2h
! h & log2 e
! h&
log2 (n + 1) 1
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 19/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
BinaryTree ADTThe BinaryTree ADTextends the Tree
ADT, i.e., it inheritsall the methods ofthe Tree ADT
Additional methods:
! position left(p)
! position right(p)
! boolean hasLeft(p)
! boolean hasRight(p)
Update methodsmay be defined by
data structuresimplementing theBinaryTree ADT
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 20/117
© 2004 Goodrich, TamassiaCS 600.226: Data Structures, Professor:
Greg Hager
Linked Structure for Binary Trees A node is represented byan object storing
! Element
! Parent node
! Left child node
! Right child node
Node objects implementthe Position ADT
B
D A
C E
B
A D
C E
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 21/117
© 2004 Goodrich, TamassiaCS 600.226: Data Structures, Professor:
Greg Hager
Array-Based Representation of
Binary Trees nodes are stored in an array
…
! let rank(node) be defined as follows:
!
rank(root) = 1! if node is the left child of parent(node),
rank(node) = 2*rank(parent(node))
! if node is the right child of parent(node),
rank(node) = 2*rank(parent(node))+1
1
2 3
6 74 5
10 11
A
HG
FE
D
C
B
J
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 22/117
© 2004 Goodrich, TamassiaCS 600.226: Data Structures, Professor:
Greg Hager
Inorder TraversalIn an inorder traversal anode is visited after its leftsubtree and before its rightsubtree
Application: draw a binarytree!
x(v) = inorder rank of v
! y(v) = depth of v
Algorithm inOrder (v)
if hasLeft (v)
inOrder (left (v))
visit (v)if hasRight (v)
inOrder (right (v))
3
1
2
5
6
7 9
8
4
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 23/117
© 2004 Goodrich, TamassiaCS 600.226: Data Structures, Professor:
Greg Hager
Print Arithmetic ExpressionsSpecialization of an inordertraversal! print operand or operator
when visiting node
! print “(“ before traversing left
subtree! print “)“ after traversing right
subtree
Algorithm printExpression(v)
if hasLeft (v) print (“(’’)
inOrder (left (v))
print (v.element ())
if hasRight (v)
inOrder (right (v))
print (“)’’)
+
##
- 2
a 1
3 b((2 # (a - 1)) + (3 # b))
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 24/117
© 2004 Goodrich, TamassiaCS 600.226: Data Structures, Professor:
Greg Hager
Evaluate Arithmetic ExpressionsSpecialization of a postordertraversal
! recursive method returningthe value of a subtree
! when visiting an internalnode, combine the values
of the subtrees
Algorithm evalExpr (v)
if isExternal (v)
return v.element ()
else
x' evalExpr (leftChild (v)) y' evalExpr (rightChild (v))
( ' operator stored at v
return x ( y+
##
- 2
5 1
3 2
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 25/117
© 2004 Goodrich, TamassiaCS 600.226: Data Structures, Professor:
Greg Hager
Binary Search Trees
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 26/117
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 27/117
© 2004 Goodrich, Tamassia
Binary Search
Trees A binary search tree is abinary tree storing keys (orkey-value entries) at itsinternal nodes and satisfying
the following property:!
Let u , v , and w be threenodes such that u is in theleft subtree of v and w is inthe right subtree of v . Wehave
key (u ) % key (v ) % key (w )External nodes do not storeitems (a proper tree)
An inorder traversal of abinary search trees visits thekeys in increasing order
6
92
41 8
The image cannot be displayed. Your computer may nothave enough memory to open the image, or the imagemay have been corrupted. Restart your computer, andthen open the file again. If the red x still appears, youmay have to delete the image and then insert it again.
CS 600.226: Data Structures, Professor:Greg Hager
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 28/117
© 2004 Goodrich, Tamassia
BST Operations
// void insert( x ) --> Insert x
// void remove( x ) --> Remove x
// boolean contains( x ) --> Return true if x is present
// Comparable findMin( ) --> Return smallest item
// Comparable findMax( ) --> Return largest item
// boolean isEmpty( ) --> Return true if empty; else false // void makeEmpty( ) --> Remove all items
// void printTree( ) --> Print tree in sorted order
CS 600.226: Data Structures, Professor:Greg Hager
public class BinarySearchTree<AnyType extends Comparable<? super AnyType>>
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 29/117
© 2004 Goodrich, Tamassia
Contains To search for a key k ,we trace a downwardpath starting at the root
The next node visited
depends on theoutcome of thecomparison of k withthe key of the currentnode
If we reach a leaf, the
key is not found and wereturn false
Example: contains(4):
! Call TreeSearch(4,root)
Algorithm TreeSearch(k , v)
if T.isExternal (v)
return false
if k < key(v)
return TreeSearch(k , T.left (v))
else if k = key(v)
return true
else { k > key(v) }
return TreeSearch(k , T.right (v))
6
92
41 8
<
>
=
CS 600.226: Data Structures, Professor:Greg Hager
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 30/117
© 2004 Goodrich, Tamassia
Insertion To perform operation insert(k,o), we search for key k (usingTreeSearch)
Assume k is not already in the
tree, and let w be the leafreached by the search
We insert k at node w andexpand w into an internalnode
Example: insert 5
6
92
41 8
6
92
41 8
5
<
>
>
w
w
CS 600.226: Data Structures, Professor:Greg Hager
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 31/117
© 2004 Goodrich, TamassiaCS 600.226: Data Structures, Professor:
Greg Hager
protected BinaryNode<AnyType> insert( AnyType x,
BinaryNode<AnyType> t ){if( t == null )
t = new BinaryNode<AnyType>( x );else if( x.compareTo( t.element ) < 0 )
t.left = insert( x, t.left );else if( x.compareTo( t.element ) > 0 )
t.right = insert( x, t.right );else
throw new DuplicateItemException( x.toString( ) );
return t;}
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 32/117
© 2004 Goodrich, Tamassia
Deletion To perform operationremove(k ), we search for key k
Assume key k is in the tree,and let let v be the node
storingk
If node v has a leaf child w , weremove v and w from the treewith operationremoveExternal(w ), whichremoves w and its parent
Example: remove 4
6
92
41 8
5
v
w
6
92
51 8
<
>
CS 600.226: Data Structures, Professor:Greg Hager
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 33/117
© 2004 Goodrich, Tamassia
Deletion (cont.)We consider the case where thekey k to be removed is stored ata node v whose children areboth internal
!
we find the internal node w thatfollows v in an inorder traversal
! we copy key (w ) into node v
! we remove node w and its leftchild z (which must be a leaf) bymeans of operationremoveExternal(z )
Example: remove 3
3
1
8
6 9
5
v
w
z
2
5
1
8
6 9
v
2
CS 600.226: Data Structures, Professor:Greg Hager
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 34/117
© 2004 Goodrich, TamassiaCS 600.226: Data Structures, Professor:
Greg Hager
protected BinaryNode<AnyType> remove( AnyType x, BinaryNode<AnyType> t )
{
if( t == null )
throw new ItemNotFoundException( x.toString( ) );if( x.compareTo( t.element ) < 0 )
t.left = remove( x, t.left );
else if( x.compareTo( t.element ) > 0 )
t.right = remove( x, t.right );
else if( t.left != null && t.right != null ) // Two children{
t.element = findMin( t.right ).element;
t.right = removeMin( t.right );
}
else
t = ( t.left != null ) ? t.left : t.right;
return t;
}
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 35/117
© 2004 Goodrich, Tamassia
Performance Consider a dictionarywith n itemsimplemented by means
of a binary search treeof height h !
the space used is O (n )
!
methods find, insert andremove take O (h ) time
The height h is O (n ) inthe worst case andO (log n ) in the bestcase
CS 600.226: Data Structures, Professor:Greg Hager
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 36/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
AVL Trees
Binary search trees which maintain O (logn ) height
Maintain height balance property
! Heights of children differ by at most 1
! Local property to maintain, but guaranteesglobal property of overall height
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 37/117
© 2004 Goodrich, TamassiaCS 600.226: Data Structures, Professor: Greg Hager
Analyzing AVL heightn(h) : minimum nodes for AVL tree of height h
Base conditionsn(1) = 1; n(2) = 2
Recurrence relation
n(h) = 1 + n(h-1) + n(h-2) > 2*n(h-2)n(h) > 2*n(h-2) > 4*n(h-4) > 8*n(h-6), etc.n(h) > 2i*n(h-2i)
Set i to achieve base conditionh-2i=2 ) i=(h-2)/2 = h/2-1n(h) > 2(h-2)/2*n(1) = 2h/2-1*2 = 2h/2
Bounding h! log(n(h)) > h/2!
h < 2(log(n(h)) ==> O (logn )
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 38/117
© 2004 Goodrich, Tamassia
Inserting with balanced heightInsert node into binary search tree asusual
! Insert occurs at leaves
! Increases height of some nodes along pathto root
Walk up towards root
! If unbalanced height is found, restructureunbalanced region with rotation operation
CS 600.226: Data Structures, Professor:
Greg Hager
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 39/117
© 2004 Goodrich, Tamassia
Restructuring
(as Single Rotations)Single Rotations:
T 0T 1
T 2
T 3
c = x
b = y
a = z
T 0 T 1 T 2
T 3
c = x
b = y
a = zsingle rotation
T 3T 2
T 1
T 0
a = x
b = y
c = z
T 0T 1T 2
T 3
a = xb = y
c = zsingle rotation
CS 600.226: Data Structures, Professor:
Greg Hager
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 40/117
© 2004 Goodrich, Tamassia
Restructuring
(as Double Rotations)double rotations:
double rotationa = z
b = x
c = y
T 0T 2
T 1
T 3 T 0
T 2T 3T 1
a = zb = x
c = y
double rotationc = z
b = x
a = y
T 0T 2
T 1
T 3 T 0
T 2T 3 T 1
c = zb = xa = y
CS 600.226: Data Structures, Professor:
Greg Hager
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 41/117
© 2004 Goodrich, Tamassia
Insertion in an AVL TreeInsertion is as in a binary search tree Always done by expanding an external node.Example:
44
17 78
32 50 88
48 62
54w
b=x
a=yc=z
44
17 78
32 50 88
48 62
before insertion after insertion
CS 600.226: Data Structures, Professor:
Greg Hager
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 42/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:
Greg Hager
Balanced Tree50
30 75
80
65
40
15
35
45
0
0 0
1
0
2
1
2
3
60
70
0 0
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 43/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:
Greg Hager
Insert (case 1)50
30 75
80
65
40
15
35
45
0
1 0
1
0
2
2
3
4
43
0
Unbalanced node 60
70
0 0
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 44/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:
Greg Hager
“
Rotate Left”
. . .50
30
75
80
65
40
15
35
45
0
1
0
1
0
2
2
3
4
43
0
60
70
0 0
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 45/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:
Greg Hager
“
Rotate Left”
. . .50
30
75
80
65
40
15
35
45
0
1
0
1
0
2
2
3
43
0
4
60
70
0 0
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 46/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:
Greg Hager
“Rotate Left” - Balanced!
50
30
75
80
65
40
15 35
45
0
1
0
1
0
2 2
1
43
0
3
60
70
0 0
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 47/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:
Greg Hager
Insert (case 2)
50
30 75
80
65
40
15
35
45
0
0 0
2
0
3
1
2
4
53
0
Unbalanced node
60
70
1 0
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 48/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:
Greg Hager
“Rotate Right”. . .
50
30 75
80
65
40
15
35
45
0
0 0
2
0
3
1
2
4
53
0
60
70
1 0
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 49/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:
Greg Hager
“Rotate Right”. . .
50
30
75
80
65
40
15
35
45
0
0 0
2
0
3
1
2
4
53
0
60
70
1
0
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 50/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:
Greg Hager
“Rotate Right” - Balanced!
50
30
75
80
65
40
15
35
45
0
0 0
2
0
1 1
2
3
53
0
60
70
1
0
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 51/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:
Greg Hager
Insert (case 3)
50
30 75
80
65
40
15
35
45
0
1 0
1
0
2
2
3
4
60
70
0 0
33
0
Unbalanced node
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 52/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:
Greg Hager
“Double Rotation Right-Left” - Right . . .
50
30 75
80
65
40
15
35 45
0
1 0
1
0
2
2
3
60
70
0 0
33
0
4
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 53/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:
Greg Hager
“Double Rotation Right-Left” - Right . . .
50
30 75
80
65
40
15
35
45
0
1
0
1
0
2
2
3
60
70
0 0
33
0
4
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 54/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:
Greg Hager
“Double Rotation Right-Left” - Right . . .done!
50
30 75
80
65
40
15 35
45
0 2
0
1
0
2
1
3
60
70
0 0
33
0
4
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 55/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:
Greg Hager
“Double Rotation Right-Left” - Left . . .
50
75
80
65
1
0
2
60
70
0 0
4
30
40
15
35
45
0
2
0
1
3
33
0
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 56/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:
Greg Hager
“Double Rotation Right-Left” - Left . . .
50
75
80
65
1
0
2
60
70
0 0
4
30
40
15
35
45
0
2
0
1
3
33
0
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 57/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:
Greg Hager
“Double Rotation Right-Left” - Balanced!
50
75
80
65
1
0
2
60
70
0 0
3
30 40
15
35
45
0
2
0
1 1
33
0
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 58/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:
Greg Hager
Restructure Procedure
Consider the first unbalanced node encountered(walking upward) and its two descendants along thatpath
Sort them in increasing order and label as a , b , and c
Place b as the parent of a and c where the unbalancednode was
Hook up the (up to) 4 subtrees as the appropriate
children of a and c
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 59/117
© 2004 Goodrich, Tamassia
Trinode Restructuringlet (a ,b ,c ) be an inorder listing of x , y , z
perform the rotations needed to make b the topmost node ofthe three
b=y
a=z
c=x
T0
T1
T2 T3
b=y
a=z c=x
T0 T1 T2 T3
c=y
b=x
a=z
T0
T1 T2
T3 b=x
c=ya=z
T0 T1 T2 T3
case 1: single rotation
(a left rotation about a)
case 2: double rotation
(a right rotation about c,
then a left rotation about a)
(other two cases
are symmetrical)
CS 600.226: Data Structures, Professor:
Greg Hager
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 60/117
© 2004 Goodrich, Tamassia
Insertion Example, continued
88
44
17 78
32 50
48 62
2
5
1
1
3
4
2
1
54
1
T 0 T 2
T 3
x
y
z
2
3
4
5
6
7
1
The image cannot bedisplayed. Your computermay not have enoughmemory to open theimage, or the image mayhave been corrupted.Restart your computer, andthen open the file again. Ifthe red x still appears, youmay have to delete theimage and then insert itagain.
88
44
17
78
32
50
48
62 2
4
1 1
2 2
3
1 54
1
T 0 T 1
T 2
T 3
x
y z
unbalanced...
...balanced1
2
3
4
5
6
7
T 1
CS 600.226: Data Structures, Professor:
Greg Hager
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 61/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:
Greg Hager
Analyzing Insert
Upward traversal with heightrecomputation takes O (h ) = O (logn )
Restructure takes O (1)
The restructure always reduces the heightof the unbalanced node
! So only one restructure is necessary
Total time: O (logn ) + O (1) = O (logn )
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 62/117
© 2004 Goodrich, Tamassia
Removal in an AVL TreeRemoval begins as in a binary search tree, which means the noderemoved will become an empty external node. Its parent, w, maycause an imbalance.
Example:
44
17
7832 50
8848
62
54
44
17
7850
8848
62
54
before deletion of 32 after deletion
CS 600.226: Data Structures, Professor:
Greg Hager
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 63/117
© 2004 Goodrich, Tamassia
Rebalancing after a RemovalLet z be the first unbalanced node encountered while travelling up the treefrom w. Also, let y be the child of z with the larger height, and let x be the childof y with the larger height (or on same side as y if height equal.
We perform restructure(x) to restore balance at z.
As this restructuring may upset the balance of another node higher in the tree,
we must continue checking for balance until the root of T is reached
44
17
7850
8848
62
54
w
c=x
b=y
a=z
44
17
78
50 88
48
62
54
CS 600.226: Data Structures, Professor:
Greg Hager
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 64/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:
Greg Hager
Remove Algorithm
Perform removal as with binary search tree! May decrease height of some nodes on path to the
root
Walk upwards to the root
! If unbalanced height is found, restructureunbalanced region with rotation operation
Remove is also O (logn )! But multiple restructure operations may be
necessary along the way
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 65/117
© 2004 Goodrich, Tamassia
Running Times for
AVL Treesa single restructure is O(1)
! using a linked-structure binary tree
find is O(log n)
! height of tree is O(log n), no restructures needed
insert is O(log n)
! initial find is O(log n)
! Restructuring up the tree, maintaining heights is O(log n)
remove is O(log n)
! initial find is O(log n)
! Restructuring up the tree, maintaining heights is O(log n)
CS 600.226: Data Structures, Professor:
Greg Hager
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 66/117
© 2004 Goodrich, TamassiaSplay Trees
76
Splay Trees
6
3 8
4
v
z
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 67/117
© 2004 Goodrich, Tamassia
Splay Trees 77
all the keys in the yellowregion are % 20
all the keys in the blueregion are & 20
Splay Trees areBinary Search Trees
BST Rules: ! items stored only at internal
nodes!
keys stored at nodes in theleft subtree of v are lessthan or equal to the keystored at v
! keys stored at nodes in the
right subtree of v are
greater than or equal to thekey stored at v
An inorder traversal willreturn the keys in order
(20,Z)
(37,P)(21,O)(14,J)
(7,T)
(35,R)(10,A)
(1,C)
(1,Q)
(5,G)(2,R)
(5,H)
(6,Y)(5,I)
(8,N)
(7,P)
(36,L)
(10,U)
(40,X)
note that two keys ofequal value may be well-
separated
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 68/117
© 2004 Goodrich, Tamassia
Splay Tree Properties
Every access causes the deepest accessed node to be“splayed” to top
Roughly halves (on average) the depth of most
nodes on access pathWe can show M access take O(M log n) time!
Amortization analysis shows O(log n) asymptoticperformance
Any one access could be O(n) worst caseSplaying looks a lot like AVL trees but with lessalgorithmic and storage overhead
Splay Trees 78
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 69/117
© 2004 Goodrich, Tamassia
Splay Trees 79
Searching in a Splay Tree:Starts the Same as in a BST
Search proceeds downthe tree to found itemor an external node.
Example: Search for
time with key 11.
(20,Z)
(37,P)(21,O)(14,J)
(7,T)
(35,R)(10,A)
(1,C)
(1,Q)
(5,G)(2,R)
(5,H)
(6,Y)(5,I)
(8,N)
(7,P)
(36,L)
(10,U)
(40,X)
l h
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 70/117
© 2004 Goodrich, Tamassia
Splay Trees 80
Example Searching in a BST,continued
search for key 8, ends atan internal node.
(20,Z)
(37,P)(21,O)(14,J)
(7,T)
(35,R)(10,A)
(1,C)
(1,Q)
(5,G)(2,R)
(5,H)
(6,Y)(5,I)
(8,N)
(7,P)
(36,L)
(10,U)
(40,X)
S l T d R i f
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 71/117
© 2004 Goodrich, Tamassia
Splay Trees 81
Splay Trees do Rotations afterEvery Operation (Even Search)
new operation: splay ! splaying moves a node to the root using rotations
!
right rotation! makes the left child x of a node y into y’s parent; y becomes the right child
of x
y
x
T1 T2
T3
y
x
T1
T2 T3
!
left rotation! makes the right child y of a node x
into x’s parent; x becomes the left
child of y
y
x
T1 T2
T3
y
x
T1
T2 T3
(structure of tree above yis not modified)
(structure of tree above xis not modified)
a right rotation about y a left rotation about x
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 72/117
© 2004 Goodrich, Tamassia
Splay Trees 82
Splaying:
is x the
root?stop
is x a child ofthe root?
right-rotate
about the root
left-rotate about
the root
is x the left
child of theroot?
is x a left-left
grandchild?
is x a left-right
grandchild?
is x a right-right
grandchild?
is x a right-left
grandchild?
right-rotate about g ,
right-rotate about p
left-rotate about g ,
left-rotate about p
left-rotate about p,
right-rotate about g
right-rotate about p,
left-rotate about g
start with
node x
! “ x is a left-left grandchild” means x is a left child of itsparent, which is itself a left child of its parent
! p is x ’s parent; g is p’s parent
no
yes
yes
yes
yes
yes
yes
no
no
yes zig-zig
zig-zag
zig-zag
zig-zig
zigzig
Vi li i th
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 73/117
© 2004 Goodrich, Tamassia
Splay Trees 83
Visualizing theSplaying Cases
zig-zag
y
x
T2 T3
T4
z
T1
y
x
T2 T3 T4
z
T1
y
x
T1 T2
T3
z
T4
zig-zig
y
z
T4 T3
T2
x
T1
zig
x
w
T1 T2
T3
y
T4 y
x
T2 T3 T4
w
T1
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 74/117
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 75/117
© 2004 Goodrich, Tamassia
Splay Trees 85
Splaying Example, Continued
now x is the left child of the root
! right-rotate around root
(10,A)
(20,Z)
(37,P)(21,O)
(35,R)
(36,L) (40,X)
(7,T)
(1,C)
(1,Q)
(5,G)(2,R)
(5,H)
(6,Y)(5,I)
(14,J)
(8,N)
(7,P)
(10,U)
x
(10,A)
(20,Z)
(37,P)(21,O)
(35,R)
(36,L) (40,X)
(7,T)
(1,C)
(1,Q)
(5,G)(2,R)
(5,H)
(6,Y)(5,I)
(14,J)
(8,N)
(7,P)
(10,U)
x
1.(before applying
rotation)
2.(after rotation)
x is the root, so stop
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 76/117
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 77/117
© 2004 Goodrich, Tamassia
Splay Tree Manipulation
Search: Splay found node
Insert: Insert as a normal binary tree
and splayDelete:
! Search (puts node at root)
! Delete yielding TL and TR
! Access largest node in TL, gives tree withno right child
! Put in TRSplay Trees 87
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 78/117
© 2004 Goodrich, Tamassia
Splay Trees 88
Splay Tree Definition
a splay tree is a binary search treewhere a node is splayed after it is
accessed (for a search or update)! deepest internal node accessed is splayed
! splaying costs O(h), where h is height ofthe tree – which is still O(n) worst-case
"
O(h) rotations, each of which is O(1)
S l T & O d d
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 79/117
© 2004 Goodrich, Tamassia
Splay Trees 89
Splay Trees & OrderedDictionaries
which nodes are splayed after each operation?
use the parent of the internal node that was actuallyremoved from the tree (the parent of the node that theremoved item was swapped with)
removeElement
use the new node containing the item insertedinsertElement
if key found, use that nodeif key not found, use parent of ending external node
findElement
splay nodemethod
A ti d A l i f
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 80/117
© 2004 Goodrich, Tamassia
Splay Trees 90
Amortized Analysis ofSplay Trees
Running time of each operation is proportional to timefor splaying.
Define rank(v) as the logarithm (base 2) of the numberof nodes in subtree rooted at v.
Costs: zig = $1, zig-zig = $2, zig-zag = $2.
Thus, cost for playing a node at depth d = $d.
Imagine that we store rank(v) cyber-dollars at eachnode v of the splay tree (just for the sake of analysis).
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 81/117
© 2004 Goodrich, Tamassia
Splay Trees 91
Cost per zig
Doing a zig at x costs at most rank ’(x) - rank(x):
! cost = rank ’(x) + rank ’(y) - rank(y) -rank(x) < rank ’(x) -rank(x).
zig
x
w
T1 T2
T3
y
T4
y
x
T2 T3 T4
w
T1
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 82/117
© 2004 Goodrich, Tamassia
Splay Trees 92
Cost per zig-zig and zig-zag
Doing a zig-zig or zig-zag at x costs at most3(rank ’(x) - rank(x)) - 2.
y
x
T1 T2
T3
z
T4 zig-zig y
z
T4 T3
T2
x
T1
zig-zag
y
x
T2 T3
T4
z
T1
y
x
T2 T3 T4
z
T1
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 83/117
© 2004 Goodrich, Tamassia
Splay Trees 93
Cost of SplayingCost of splaying a node x at depth d of a treerooted at r:! at most 3(rank(r) - rank(x)) - d + 2:
!
Proof: Splaying x takes d/2 splaying substeps:
.2))(rank )(rank (3
2)/(2))(rank )(rank (3
2)2))(rank )(rank (3(
costcost
0
1
2/
1
2/
1
+!!"
+!!=
+!!"
"
!=
=
#
#
d xr
d d xr
x xi
d
i
i
i
d
i
Performance of
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 84/117
© 2004 Goodrich, Tamassia
Splay Trees 94
Performance ofSplay Trees
Recall: rank of a node is logarithm of its size.
Thus, amortized cost of any splay operation is
O(log n). In fact, the analysis goes through for anyreasonable definition of rank(x).
Splay trees can actually adapt to perform
searches on frequently-requested items muchfaster than O(log n) in some cases.
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 85/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:
Greg Hager
Multi-way Search Trees
Each node may store multiple key-element
pairsNode with d children (d -node) stores d -1key-element pairs
Children have keys that fall either beforesmallest parent key, after largest parentkey, or between two parent keys
Example Multi way Search
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 86/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:
Greg Hager
Example Multi-way SearchTree
External node between each pair of keys and before/after(n-1) + 1 + 1 = n+1 external nodes
50
20 30 60 70 80
10 15 25 40 42 45 55 64 66 75 85 90
22 27
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 87/117
© 2004 Goodrich, Tamassia
Multi-Way Inorder TraversalWe can extend the notion of inorder traversal from binary treesto multi-way search trees
Namely, we visit item (k i , o i ) of node v between the recursivetraversals of the subtrees of v rooted at children v i and v i + 1
An inorder traversal of a multi-way search tree visits the keys inincreasing order
11 24
2 6 8 15
30
27 32
1 3 5 7 9 11 13 19
15 17
2 4 6 14 18
8 12
10
16
CS 600.226: Data Structures, Professor:
Greg Hager
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 88/117
© 2004 Goodrich, Tamassia
Multi-Way SearchingSimilar to search in a binary search tree
A each internal node with children v 1 v 2 … v d and keys k 1 k 2 … k d -1
! k = k i (i = 1, …, d - 1): the search terminates successfully
! k < k 1: we continue the search in child v 1! k i -1 < k < k i (i = 2, …, d - 1): we continue the search in child v i! k > k d -1: we continue the search in child v d
Reaching an external node terminates the search unsuccessfully
Example: search for 30
11 24
2 6 8 15
30
27 32
CS 600.226: Data Structures, Professor:
Greg Hager
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 89/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:
Greg Hager
Multi-way Search Analysis
Number of nodes traversed is up to h
Work at each node is function of d
! O(log d ) if structure storing keys provides efficientsearch, otherwise O (d )
Total worst case time! O (h log d max) or O (h d max)
! If d max is bounded by small constant, just O (h )
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 90/117
© 2004 Goodrich, Tamassia
B-trees
A B-tree of order M is a MWST s.t.! The root has between 2 and M children
! A nonleaf nodes have ceil(M/2) to M children
! All leaves have the same depth
B+ trees (in the book) have the additionalproperties that:!
All data occurs at leaves! Leaves may have a different size than internal
nodes.
CS 600.226: Data Structures, Professor:
Greg Hager
Ho Man Nodes Can a B T ee
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 91/117
© 2004 Goodrich, Tamassia
How Many Nodes Can a B TreeStore?
A 2,4 tree
A 11,21 tree
A 17, 32 tree…
CS 600.226: Data Structures, Professor:
Greg Hager
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 92/117
© 2004 Goodrich, Tamassia
(2,4) Trees
A (2,4) tree (also called 2-4 tree or 2-3-4 tree) is a multi-waysearch with the following properties
! Node-Size Property: every internal node has at most four children
! Depth Property: all the external nodes have the same depth
Depending on the number of children, an internal node of a(2,4) tree is called a 2-node, 3-node or 4-node
10 15 24
2 8 12 27 3218
CS 600.226: Data Structures, Professor:
Greg Hager
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 93/117
© 2004 Goodrich, Tamassia
Height of a (2,4) TreeTheorem: A (2,4) tree storing n items has height O (log n )
Proof:! Let h be the height of a (2,4) tree with n items
! Since there are at least 2i items at depth i = 0, … , h - 1 and noitems at depth h , we have
n & 1 + 2 + 4 + … + 2h -1= 2h - 1
! Thus, h % log (n + 1)
Searching in a (2,4) tree with n items takes O (log n ) time
1
2
2h-1
0
items
0
1
h-1
h
depth
CS 600.226: Data Structures, Professor:
Greg Hager
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 94/117
© 2004 Goodrich, Tamassia
InsertionWe insert a new item (k , o ) at the parent v of the leaf reached bysearching for k
! We preserve the depth property but
! We may cause an overflow (i.e., node v may become a 5-node)
Example: inserting key 30 causes an overflow
27 32 35
10 15 24
2 8 12 18
10 15 24
2 8 12 27 30 32 3518
v
v
CS 600.226: Data Structures, Professor:Greg Hager
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 95/117
© 2004 Goodrich, Tamassia
Overflow and SplitWe handle an overflow at a 5-node v with a split operation:! let v 1 … v 5 be the children of v and k 1 … k 4 be the keys of v
! node v is replaced nodes v ' and v "
" v ' is a 3-node with keys k 1 k 2 and children v 1 v 2 v 3
" v " is a 2-node with key k 4 and children v 4 v 5
!
keyk
3 is inserted into the parentu
ofv
(a new root may be created)The overflow may propagate to the parent node u
15 24
12 27 30 32 3518 v
u
v1 v2 v3 v4 v5
15 24 32
12 27 3018 v'
u
v1 v2 v3 v4 v5
35v"
CS 600.226: Data Structures, Professor:Greg Hager
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 96/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
Simple Insertion (no overflow)
10
5 12 14
10
5 12 14 15
Insert 15
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 97/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
Insertion with Overflow
10
5 12 14 15
Insert 11 10
5 11 12 14 15
Split
10 14
5 11 12 15
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 98/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
Insert with Cascading Split
6 8 10
5 12 14 15
Insert 11
7 9
6 8 10
5 11 12 14 157 9
Split
6 8 10 14
5 11 127 9 15
Split
6 8
5 11 127 9 15
14
10
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 99/117
© 2004 Goodrich, Tamassia
Analysis of Insertion
Algorithm insert (k , o )
1. We search for key k to locate theinsertion node v
2. We add the new entry (k , o ) atnode v
3. while overflow (v )
if isRoot (v )
create a new empty root above v v ' split (v )
Let T be a (2,4) treewith n items
! Tree T has O (log n )
height
!
Step 1 takes O (log n )time because we visitO (log n ) nodes
! Step 2 takes O (1) time
! Step 3 takes O (log n )time because each split
takes O (1) time and weperform O (log n ) splits
Thus, an insertion in a(2,4) tree takes O (log n )time
CS 600.226: Data Structures, Professor:Greg Hager
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 100/117
© 2004 Goodrich, Tamassia
DeletionWe reduce deletion of an entry to the case where the item is at the node withleaf children
Otherwise, we replace the entry with its inorder successor (or, equivalently, withits inorder predecessor) and delete the latter entry
Example: to delete key 24, we replace it with 27 (inorder successor)
27 32 35
10 15 24
2 8 12 18
32 35
10 15 27
2 8 12 18
CS 600.226: Data Structures, Professor:Greg Hager
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 101/117
© 2004 Goodrich, Tamassia
Underflow and FusionDeleting an entry from a node v may cause an underflow, wherenode v becomes a 1-node with one child and no keys
To handle an underflow at node v with parent u , we consider twocases
Case 1: the adjacent siblings of v are 2-nodes
! Fusion operation: we merge v with an adjacent sibling w and move
an entry from u to the merged node v '
! After a fusion, the underflow may propagate to the parent u
9 14
2 5 7 10
u
v
9
10 14
u
v'w 2 5 7
CS 600.226: Data Structures, Professor:Greg Hager
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 102/117
© 2004 Goodrich, Tamassia
Underflow and TransferTo handle an underflow at node v with parent u , we considertwo cases
Case 2: an adjacent sibling w of v is a 3-node or a 4-node!
Transfer operation:
1. we move a child of w to v
2. we move an item from u to v
3. we move an item from w to u
! After a transfer, no underflow occurs
4 9
6 82
u
v w
4 8
62 9
u
v w
CS 600.226: Data Structures, Professor:Greg Hager
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 103/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
Simple Removal
6 8 10
5 12 14 157 9
Remove 14 6 8 10
5 12 157 9
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 104/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
Removal with Swap
6 8 10
5 12 14 157 9
Remove 10 6 8
5 12 14 157 9
Swap
6 8 12
5 14 157 9
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 105/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
Removal with Transfer
6 8 10
5 12 14 157 9
Remove 9 6 8 10
5 12 14 157
Transfer
(~rotate)
6 8 12
5 14 157 10
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 106/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
Removal with Fusion
6 8 10
5 12 14 157 9
Remove 7 6 8 10
5 12 14 159
Fusion
6 10
5 12 14 158 9
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 107/117
© 2004 Goodrich, Tamassia
Analysis of Deletion
Let T be a (2,4) tree with n items!
Tree T has O (log n ) height
In a deletion operation!
We visit O (log n ) nodes to locate the node from which todelete the entry
!
We handle an underflow with a series of O (log n ) fusions,followed by at most one transfer
!
Each fusion and transfer takes O (1) time
Thus, deleting an item from a (2,4) tree takes O (logn ) time
CS 600.226: Data Structures, Professor:Greg Hager
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 108/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
External Memory SearchingMemory Hierarchy
Registers
Cache
RAM
External Memory
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 109/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
Types of External Memory
Hard disk
Floppy diskCompact disc
Tape
Distributed/networked memory
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 110/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
Primary Motivation
External memory access much slower
than internal memory access! orders of magnitude slower
! need to minimize I/O Complexity
! can afford slightly more work on data inmemory in exchange for lower I/Ocomplexity
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 111/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
Application Areas
Searching
SortingData Processing
Data Mining
Data Exploration
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 112/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
Disk Blocks
Data is read one block at a time! pack as much into a block as possible
! minimize number of block reads necessary
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 113/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
I/O Efficient Dictionaries
Balanced tree structures!
Typically O (log2n) transfers for query orupdate
! Want to reduce height by constant factor asmuch as possible
! Can be reduced to O (logBn ) = O (log2n /log2B)" B is number of nodes per block
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 114/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
(a,b) Trees
Generalization of (2,4) trees
Size property: internal node has at least a children and at most b children! 2 <= a <= (b+1)/2
Depth property: all external nodes have
same depthHeight of (a,b) tree is * (logn/logb) andO(logn/loga)
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 115/117
© 2004 Goodrich, Tamassia CS 600.226: Data Structures, Professor:Greg Hager
B-Trees
Choose a and b to be +
B)Height is now O (logB n )
I/O complexity for search is
O(logBn)
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 116/117
© 2004 Goodrich, Tamassia
Sets and Maps
Set: a Collection that does not allowduplicates! http://download.oracle.com/javase/tutorial/collections/interfaces/set.html
! Can be implemented in many ways, including TreeSet
Map: a Collection of Key-Value pairsthat does not allow duplicate keys! http://download.oracle.com/javase/tutorial/collections/interfaces/map.html
! Can be implemented in many ways, including TreeMap
CS 600.226: Data Structures, Professor:Greg Hager
7/23/2019 Make Money Fast! - Department of Computer Science-copypasteads.com
http://slidepdf.com/reader/full/make-money-fast-department-of-computer-science-copypasteadscom 117/117
Summary
Binary search trees
AVL trees and balancing
Splay treesMulti-way trees
2-4 trees
B-trees