Professional Documents
Culture Documents
Make Money Fast! - Department of Computer
Make Money Fast! - Department of Computer
Stock
Fraud
Ponzi
Scheme
Bank
Robbery
What is a Tree
! In computer science, a
!
!
Organization charts
File systems
Europe
Programming
environments
ComputersRUs
Sales
Manufacturing
International
Asia
Laptops
Canada
Desktops
R&D
Formal Definition
! A tree T is a set of nodes such that T is
Empty, or
n Has a distinguished node r referred to as
the root and
n Each node v r has a unique parent node
w in T
n
Tree Terminology
! Root: node without parent (A)
! Internal node: node with at least
!
!
!
!
!
!
subtree
I
More Terms
! A tree is ordered if the children of a node have a
linear ordering
A node is represented by
an object storing
n
n
n
Element
Parent node
List of children nodes
B
D
A
C
E
C
Tree ADT
! We use positions to abstract
!
nodes
Generic methods:
n
n
n
n
n
n
integer size()
boolean isEmpty()
Iterator iterator()
Iterator positions()
! Accessor methods:
n
n
n
position root()
position parent(p)
positionIterator children(p)
! Query methods:
boolean isInternal(p)
boolean isExternal(p)
boolean isRoot(p)
! Update method:
n
Preorder Traversal
! A traversal visits the nodes of a
!
!
1. Motivations
9
2. Methods
1.1 Greed
1.2 Avidity
Algorithm preOrder(v)
visit(v)
for each child w of v
preorder (w)
6
2.1 Stock
Fraud
7
2.2 Ponzi
Scheme
References
8
2.3 Bank
Robbery
Parenthetical Representation
! Parenthetical(T,v)
s = toString(v)
n if (T.isInternal(v))
n
w s+= (
w for (Node<E> w: T.children(v))
n
s += Parenthetical(T,w) + ;
w s+=)
n
return s
Postorder Traversal
! In a postorder traversal, a
!
Algorithm postOrder(v)
for each child w of v
postOrder (w)
visit(v)
cs16/
homeworks/
todo.txt
1K
programs/
h1c.doc
3K
h1nc.doc
2K
4
DDR.java
10K
5
Stocks.java
25K
6
Robot.java
20K
Other Possibilities
! Breadth-first
n
! In-order/mixed
n
! ...
2004 Goodrich, Tamassia
Binary Trees
! A binary tree is a tree with the
following properties:
n
! Applications:
n
n
n
arithmetic expressions
decision processes
searching
A
H
2004 Goodrich, Tamassia
2
a
2004 Goodrich, Tamassia
-
1
Constructing an Expression
Tree
! Given a postfix string, construct the
expression tree
! Use a stack to hold intermediate results
! Think of it like evaluation except the
operation is to construct the tree
Decision Tree
! Binary tree associated with a decision process
n
n
Yes
On expense account?
Yes
No
Yes
No
Starbucks
Spikes
Al Forno
Caf Paragon
! Properties:
n number of nodes
e number of
external nodes
i number of internal
nodes
h height
n
n
n
n
n
n
n
e=i+1
n = 2e - 1
hi
h (n - 1)/2
e 2h
h log2 e
h log2 (n + 1) - 1
BinaryTree ADT
! The BinaryTree ADT
extends the Tree
ADT, i.e., it inherits
all the methods of
the Tree ADT
! Additional methods:
n
n
n
n
! Update methods
may be defined by
data structures
implementing the
BinaryTree ADT
position left(p)
position right(p)
boolean hasLeft(p)
boolean hasRight(p)
A node is represented by
an object storing
n
n
n
n
Element
Parent node
Left child node
Right child node
B
A
D
C
Array-Based Representation of
Binary Trees
! nodes are stored in an array
1
A
3
B
6
C
10
11
G
Inorder Traversal
! In an inorder traversal a
Algorithm inOrder(v)
if hasLeft (v)
inOrder (left (v))
visit(v)
if hasRight (v)
inOrder (right (v))
4
3
Specialization of an inorder
traversal
n
-
a
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))
if isExternal (v)
return v.element ()
else
x evalExpr(leftChild (v))
y evalExpr(rightChild (v))
operator stored at v
return x y
-
5
1
CS 600.226: Data Structures, Professor:
Greg Hager
Binary Search
!
Example: find(7)
l
0
11
14
16
18
l
0
1
1
3
3
11
14
16
18
19
11
14
16
18
19
11
14
16
18
19
h
4
l=m =h
2004 Goodrich, Tamassia
19
Binary Search
Trees
! A binary search tree is a
! An inorder traversal of a
6
2
1
9
4
BST Operations
public class BinarySearchTree<AnyType extends Comparable<? super AnyType>>
!
!
!
!
!
!
!
!
//
//
//
//
//
//
//
//
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
Contains
! To search for a key k,
!
!
!
we trace a downward
path starting at the root
The next node visited
depends on the
outcome of the
comparison of k with
the key of the current
node
If we reach a leaf, the
key is not found and we
return false
Example: contains(4):
n
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))
2
1
Call TreeSearch(4,root)
<
>
4
Insertion
!
!
!
<
2
1
>
>
w
6
2
1
9
4
8
5
Deletion
!
!
!
To perform operation
remove(k), we search for key k
Assume key k is in the tree,
and let let v be the node
storing k
If node v has a leaf child w, we
remove v and w from the tree
with operation
removeExternal(w), which
removes w and its parent
Example: remove 4
<
2
>
9
4
8
5
6
2
1
9
5
Deletion (cont.)
!
n
n
1
3
8
6
z
1
5
Example: remove 3
8
6
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
Performance
! Consider a dictionary
with n items
implemented by means
of a binary search tree
of height h
n
n
AVL Trees
! Binary search trees which maintain O(log
n) height
! Maintain height balance property
! Recurrence relation
! Bounding h
n
n
Restructuring
(as Single Rotations)
! Single Rotations:
a=z
b=y
single rotation
b=y
a=z
c=x
c=x
T0
T1
T3
T2
c=z
T0
T2
b=y
single rotation
b=y
T1
T3
a=x
c=z
a=x
T0
T1
2004 Goodrich, Tamassia
T2
T3
T3
T2
T1
T0
Restructuring
(as Double Rotations)
! double rotations:
double rotation
a=z
b=x
c=y
a=z
c=y
b=x
T0
T3
T2
T0
T2
T1
T3
T1
double rotation
c=z
b=x
a=y
a=y
c=z
b=x
T3
T0
T2
T3
T2
T1
2004 Goodrich, Tamassia
T1
T0
78
17
78
c=z
a=y
32
50
48
88
32
62
50
48
before insertion
2004 Goodrich, Tamassia
88
62
54
after insertion
b=x
Balanced Tree
3
2
0
15
30
50
75
1
0
35
40
1
0
45
65
60
70
80
Insert (case 1)
4
3
0
30
15
Unbalance
d node
50
75
2
0
35
40
1
1
45
0
65
60
43
2004 Goodrich, Tamassia
70
80
Rotate Left . . .
4
3
30
75
15
50
1
40
35
45
65
60
43
2004 Goodrich, Tamassia
70
80
Rotate Left . . .
4
3
30
75
40
45
35
15
50
65
60
43
2004 Goodrich, Tamassia
70
80
40
15
75
30
0
50
1
45
0
35 43
65
60
70
80
Insert (case 2)
4
2
0
30
15
50
75
1
0
35
40
2
0
45
65
60
0
53
2004 Goodrich, Tamassia
70
80
Unbalance
d node
Rotate Right. . .
4
2
0
30
15
50
75
1
0
35
40
2
0
45
65
60
0
53
2004 Goodrich, Tamassia
70
80
Rotate Right. . .
4
2
0
30
15
50
1
0
35
40
1
0
45
60
0
53
2004 Goodrich, Tamassia
75
65
0
0
70
80
30
15
1
0
35
50
40
60
45
53
65
1
0
70
75
80
Insert (case 3)
4
3
0
30
15
Unbalance
d node
50
75
2
1
35
40
1
0
45
65
60
33
2004 Goodrich, Tamassia
70
80
30
15
75
2
1
50
35
40
0
65
45 60
33
2004 Goodrich, Tamassia
70
80
50
30
1
15
0
33
35
75
40
0
0
45
65
60
70
80
15
30
0
33
50
75
35
1
1
40
0
0
65
60
45
70
80
15
30
0
33
50
75
35
1
1
40
0
0
65
60
45
2004 Goodrich, Tamassia
70
80
15
30
35
33
50
75
40
0
0
45
65
60
70
80
15
30
35
0
33
50
75
40
1
0
45
65
60
70
80
Restructure Procedure
! Consider the first unbalanced node encountered
Trinode Restructuring
! let (a,b,c) be an inorder listing of x, y, z
! perform the rotations needed to make b the topmost node of
the three
a=z
a=z
c=y
b=y
T0
T0
b=x
c=x
T1
T3
b=y
T2
T1
T3
a=z
T0
b=x
T2
c=x
T1
T2
a=z
T3
T0
c=y
T1
T2
T3
17
3
1
32
unbalanced...
50
48
2
1
78
2y
64
4
62
88
x
5
T3
54
T2
T0
T1
44
4
3
2
17
1
32
...balanced
2 y
2
48
T0
50
x
z6
62
78
54
T1
7
88
T2
T3
Analyzing Insert
! Upward traversal with height
44
17
62
32
50
48
17
78
54
50
88
before deletion of 32
2004 Goodrich, Tamassia
62
48
78
54
after deletion
88
Let z be the first unbalanced node encountered while travelling up the tree
from w. Also, let y be the child of z with the larger height, and let x be the child
of 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
a=z
w
62
44
17
50
48
c=x
78
54
44
b=y
62
88
17
78
50
48
88
54
Remove Algorithm
! Perform removal as with binary search tree
n
! find is O(log n)
n
! insert is O(log n)
n
n
! remove is O(log n)
n
n
Splay Trees
v
6
8
3
4
Splay Trees
76
(20,Z)
(10,A)
(35,R)
! BST Rules:
n
(7,T)
(1,Q)
(1,C)
(5,H)
Splay Trees
(5,I)
77
(7,P)
(36,L)
(37,P)
(40,X)
(10,U)
(5,G)
(21,O)
(8,N)
(2,R)
(14,J)
(6,Y)
Splay Trees
78
(10,A)
(7,T)
(1,Q)
(1,C)
(5,H)
79
(7,P)
(5,G)
(5,I)
Splay Trees
(14,J)
(21,O)
(8,N)
(2,R)
(35,R)
(6,Y)
(36,L)
(10,U)
(37,P)
(40,X)
(10,A)
(7,T)
(1,Q)
(1,C)
(5,H)
80
(7,P)
(5,G)
(5,I)
Splay Trees
(14,J)
(21,O)
(8,N)
(2,R)
(35,R)
(6,Y)
(36,L)
(10,U)
(37,P)
(40,X)
right rotation
n
left rotation
n
x
x
T3
T1
y
T1
T2
(structure of tree above y
is not modified)
Splay Trees
x
T2
T1
T2
T3
T3
81
T3
T1
T2
Splaying:
start with
node x
is x the
root?
yes
is x a left-left
grandchild?
stop
yes
no
is x a child of
the root?
is x a right-right
grandchild?
no
yes
yes
is x a right-left
grandchild?
is x the left
child of the
root?
yes
zig
right-rotate
about the root
2004 Goodrich, Tamassia
Splay Trees
no
yes
is x a left-right
grandchild?
zig
left-rotate about
the root
yes
82
zig-zig
right-rotate about g,
right-rotate about p
zig-zig
left-rotate about g,
left-rotate about p
zig-zag
left-rotate about p,
right-rotate about g
zig-zag
right-rotate about p,
left-rotate about g
Visualizing the
Splaying Cases
zig-zag
z
z
y
T4
x
T4
T3
T1
T1
zig-zig
T2
T1
T2
T3
T4
T3
T2
T4
T1
z
T3
T3
T2
Splay Trees
zig
T4
T1
T2
83
T1
T2
T3
T4
Splaying Example
!
let x = (8,N)
n x is the right child of its parent,
which is the left child of the
grandparent
n left-rotate around p, then rightrotate around g
(20,Z)
(10,A)
(35,R)
g
(14,J)
(7,T)
p
(1,Q)
(21,O)
(8,N)
1.
(37,P)
(36,L)
(before
rotating)
(40,X)
x
(1,C)
(5,H)
(2,R)
(10,U)
(7,P)
(5,G)
(20,Z)
(6,Y)
(5,I)
g
x
(10,A)
(14,J)
(8,N)
(20,Z)
(35,R)
(21,O)
(37,P)
(35,R)
(8,N)
g
p
(10,U)
(7,T)
(1,Q)
(1,C)
(36,L)
(40,X)
(7,T)
(1,Q)
(7,P)
2.
(5,H)
(2,R)
(1,C)
(5,I)
(6,Y)
Splay Trees
(7,P)
84
(37,P)
(36,L)
(14,J)
(40,X)
3.
(after second
rotation)
(5,G)
(5,I)
(21,O)
(10,U)
(5,H)
(2,R)
(5,G)
(10,A)
(6,Y)
(1,Q)
(10,A)
(7,P)
(2,R)
(5,G)
(5,I)
(21,O)
(14,J)
(10,U)
(5,H)
(35,R)
(8,N)
(7,T)
(1,C)
(6,Y)
(37,P)
(36,L)
(40,X)
(8,N)
2.
(20,Z)
(7,T)
1.
(before applying
rotation)
(1,Q)
(1,C)
(7,P)
(14,J)
(10,U)
(5,G)
(5,I)
(35,R)
(10,A)
(5,H)
(2,R)
(after rotation)
(21,O)
(37,P)
(36,L)
(6,Y)
Splay Trees
85
(40,X)
Example Result
of Splaying
(20,Z)
(10,A)
before
(14,J)
(7,T)
!
!
(35,R)
(21,O)
(8,N)
(37,P)
(36,L)
(40,X)
(10,U)
(7,P)
(5,G)
(40,X)
(6,Y)
(5,I)
(20,Z)
(20,Z)
(7,T)
(1,Q)
(1,C)
(14,J)
(2,R)
(7,P)
(1,Q)
(35,R)
(10,U)
(21,O)
(1,C)
(36,L)
Splay Trees
(5,H)
86
(14,J)
(7,P)
(5,G)
(5,I)
(6,Y)
(37,P)
(35,R)
(8,N)
(2,R)
(5,G)
(5,I)
(7,T)
(37,P)
(8,N)
(5,H)
(10,A)
(40,X)
(10,A)
(6,Y)
(21,O)
(36,L)
(10,U)
after second
splay
Splay Trees
87
Splay Trees
88
insertElement
removeElement
Splay Trees
splay node
if key found, use that node
if key not found, use parent of ending external node
use the new node containing the item inserted
use the parent of the internal node that was actually
removed from the tree (the parent of the node that the
removed item was swapped with)
89
Amortized Analysis of
Splay Trees
! Running time of each operation is proportional to time
!
!
!
!
for splaying.
Define rank(v) as the logarithm (base 2) of the number
of 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 each
node v of the splay tree (just for the sake of analysis).
Splay Trees
90
zig
x
x
T4
T3
T1
T2
T1
T2
T3
T4
Splay Trees
91
zig-zig
T4
T1
T3
T1
y
z
T2
T2
T3
y
T1
T4
T2
Splay Trees
T1
T3
92
T2
T3
T4
T4
Cost of Splaying
! Cost of splaying a node x at depth d of a tree
rooted at r:
n
n
cost cost i
i =1
d /2
Splay Trees
93
Performance of
Splay 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 any
reasonable definition of rank(x).
! Splay trees can actually adapt to perform
searches on frequently-requested items much
faster than O(log n) in some cases.
2004 Goodrich, Tamassia
Splay Trees
94
20 30
10 15
25
22
40 42 45
55
64 66
75
85 90
27
11
8
2 6 8
2
24
15
12
27
14
10
7
11
13
30
18
19
16
15
2004 Goodrich, Tamassia
32
17
Multi-Way Searching
!
!
!
!
11
2 6 8
24
15
27
32
30
B-trees
! A B-tree of order M is a MWST s.t.
n
n
n
A 2,4 tree
A 11,21 tree
A 17, 32 tree
(2,4) Trees
! A (2,4) tree (also called 2-4 tree or 2-3-4 tree) is a multi-way
search with the following properties
n
n
10 15 24
2 8
12
18
27
32
h-1
2h-1
Insertion
!
12
18
10 15 24
2 8
12
18
27 32 35
27 30 32 35
u
12
15 24
18
27 30 32 35
12
v1 v2 v3 v4 v5
2004 Goodrich, Tamassia
18
15 24 32
v'
27 30
v1 v2 v3 v4
v"
35
v5
10
12 14
Insert 15
12 14 15
10
Insert 11
12 14 15
11 12 14 15
Split
10 14
5
2004 Goodrich, Tamassia
11 12
15
Insert 11
12 14 15
6 8 10
5
10
6 8
5
11 12 14 15
Split
Split
14
9
11 12
15
6 8 10 14
5
11 12
15
Analysis of Insertion
!
!
!
!
Algorithm insert(k, o)
1.
We search for key k to locate the
insertion node v
2.
We add the new entry (k, o) at
node v
3. while overflow(v)
if isRoot(v)
create a new empty root above v
v split(v)
n
n
! Thus, an insertion in a
Deletion
!
!
!
We reduce deletion of an entry to the case where the item is at the node with
leaf children
Otherwise, we replace the entry with its inorder successor (or, equivalently, with
its inorder predecessor) and delete the latter entry
Example: to delete key 24, we replace it with 27 (inorder successor)
10 15 24
2 8
12
18
27 32 35
10 15 27
2 8
12
18
32 35
9 14
2 5 7
10
2 5 7
9
10 14
v'
two cases
Case 2: an adjacent sibling w of v is a 3-node or a 4-node
n
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
u
4 9
w
6 8
u
4 8
w
6
Simple Removal
6 8 10
5
Remove 14
12 14 15
6 8 10
7
12 15
6 8
Remove 10
12 14 15
12 14 15
Swap
6 8 12
5
2004 Goodrich, Tamassia
14 15
6 8 10
5
12 14 15
6 8 10
5
12 14 15
Transfer
(~rotate)
6 8 12
5
2004 Goodrich, Tamassia
10
14 15
6 8 10
Remove 7
12 14 15
12 14 15
Fusion
6 10
5
2004 Goodrich, Tamassia
8 9
12 14 15
Analysis of Deletion
! Let T be a (2,4) tree with n items
n
! In a deletion operation
n
External Memory
2004 Goodrich, Tamassia
Hard disk
Floppy disk
Compact disc
Tape
Distributed/networked memory
Primary Motivation
! External memory access much slower
than internal memory access
Application Areas
!
!
!
!
!
Searching
Sorting
Data Processing
Data Mining
Data Exploration
Disk Blocks
! Data is read one block at a time
pack as much into a block as possible
n minimize number of block reads necessary
n
(a,b) Trees
! Generalization of (2,4) trees
! Size property: internal node has at least a
children and at most b children
n
same depth
! Height of (a,b) tree is (logn/logb) and
O(logn/loga)
2004 Goodrich, Tamassia
B-Trees
http://download.oracle.com/javase/tutorial/collections/interfaces/set.html
Can be implemented in many ways, including TreeSet
http://download.oracle.com/javase/tutorial/collections/interfaces/map.html
Can be implemented in many ways, including TreeMap
Summary
!
!
!
!
!
!