Lecture 6 – Ov

advertisement
TDDB57 DALG – Lecture 6: ADT Tree, Binary Search Trees
Lecture 6 – Overview
Page 1
J. Maluszynski, IDA, Linköpings Universitet, 2004.
[Lewis/Denenberg p.103]
[Lewis/Denenberg 4.4]
ADT Tree
Tree representations
TDDB57 DALG – Lecture 6: ADT Tree, Binary Search Trees
ADT Tree (1)
Page 2
J. Maluszynski, IDA, Linköpings Universitet, 2004.
Domain: tree nodes, maybe associated with additional information
Operations on a single tree node v:
[Lewis/Denenberg 4.3]
[Lewis/Denenberg pp.193-196, 198-199]
Tree traversal strategies
Binary search trees, implementation of Dictionary
Insertion and deletion take constant time.
RC
J. Maluszynski, IDA, Linköpings Universitet, 2004.
LC
RC
RC
RightChild
If required, a “backward” pointer to the parent node can be added.
Alternatively, the pointers to a node’s children can be stored in a linked list.
LC
LeftChild
Tree representations (1): using pointers
For binary trees:
2 pointers per node, LC and RC
[1..nchilds]
ADT Tree (2)
Page 4
Parent v returns parent of v, or Λ if v root
Children v returns set of children of v, or Λ if v leaf
FirstChild v returns first child of v, or Λ if v leaf
LeftChild v , RightChild v returns left / right child of v, or Λ if not existing
RightSibling v returns right sibling of v, or Λ if v is a rightmost child
LeftSibling v returns left sibling of v, or Λ if v is a leftmost child
IsLeaf v returns true iff v is a leaf
TDDB57 DALG – Lecture 6: ADT Tree, Binary Search Trees
Type Tnode denotes a pointer to a structure storing node information:
J. Maluszynski, IDA, Linköpings Universitet, 2004.
Operations on an entire tree T :
Page 3
record node record
nchilds: integer
child: table Tnode
info: infotype
TDDB57 DALG – Lecture 6: ADT Tree, Binary Search Trees
Size T returns number of nodes of T
Root T returns root node of T
IsRoot v T returns true iff v is root of T
Depth v T returns depth of v in T
Height v T returns height of v in T
Height T returns height of the root of T
10
0
5
11
6
5
7
2
8
6
9
TDDB57 DALG – Lecture 6: ADT Tree, Binary Search Trees
Tree traversals (1)
Page 6
Regard a tree T as a building:
nodes as rooms, edges as doors, root as entry
7
3
8
1
9
4
TDDB57 DALG – Lecture 6: ADT Tree, Binary Search Trees
Page 8
Tree traversals(3): Level-order traversal
6
10
15
18
21
Traverse all the nodes of a tree:
by increasing depth
nodes of the same depth from left to right
5
25
0
11
5
2
J. Maluszynski, IDA, Linköpings Universitet, 2004.
Level-order traversal implemented on Queue (see course book p.124)
15, 6, 21, 5, 10, 18, 25
6
J. Maluszynski, IDA, Linköpings Universitet, 2004.
10
How to explore an unknown (acyclic) labyrinth and get out again?
Proceed by always keeping a wall to the right!
Generic tree traversal routine:
procedure traverse ( node v )
explore subtree rooted at v
for all u Children v do
traverse u
exactly one call for each node in T ; recursive: needs Stack!!
Call traverse( Root(T ) ):
J. Maluszynski, IDA, Linköpings Universitet, 2004.
index positions
Page 5
10 11
TDDB57 DALG – Lecture 6: ADT Tree, Binary Search Trees
4
4
J. Maluszynski, IDA, Linköpings Universitet, 2004.
Tree representations (2): array indexing
1
9
3
LeftChild i : 2i 1
(none if 2i 1 n)
RightChild i : 2i 2
(none if 2i 1 n)
IsLeaf i : 2i 1 n
LeftSibling i : i 1
(none if i 0 or i odd)
RightSibling i : i 1
(none if i n 1 or i even)
Parent i : i 1 2 (none if i 0)
Depth i : log2 i 1
Height i : log2 n 1 i 1
1
For a complete binary tree holds:
There is exactly one complete binary tree with n nodes.
3
8
2
Implicit representation of edges: Numbering of nodes
7
1
Page 7
only for binary trees
0
TDDB57 DALG – Lecture 6: ADT Tree, Binary Search Trees
Tree traversals (2)
procedure preorder visit ( node v )
output v
before any of the subtree nodes are output
for all u Children v do
preorder visit u
procedure postorder visit ( node v )
for all u Children v do
postorder visit u
output v
after all of the subtree nodes have been output
procedure inorder visit ( node v )
inorder visit LC v
output v
inorder visit RC v
Page 9
J. Maluszynski, IDA, Linköpings Universitet, 2004.
TDDB57 DALG – Lecture 6: ADT Tree, Binary Search Trees
Page 10
RC
LC
34
RC
41
RC
Insert v : add v as new leaf on look-up failure
worst-case: height T
1 comparisons
Delete v :
if v is a leaf, remove v
if v has one child u, replace v by u
42
21
24
44
LookUp(22)
Page 12
1 comparisons
12
20
28
49
48
56
[Lewis/Denenberg Alg. 6.8]
J. Maluszynski, IDA, Linköpings Universitet, 2004.
if v has two children, replace v by its inorder successor
(alternatively: by its inorder predecessor)
worst-case: height T
TDDB57 DALG – Lecture 6: ADT Tree, Binary Search Trees
LookUp in BST
6
11
22
[Lewis/Denenberg Alg. 6.7]
J. Maluszynski, IDA, Linköpings Universitet, 2004.
Dictionary as a Binary Search Tree
TDDB57 DALG – Lecture 6: ADT Tree, Binary Search Trees
Implementing Dictionaries as Binary Search Trees
Not unique: same data may be represented by different BST
25
65
J. Maluszynski, IDA, Linköpings Universitet, 2004.
5
8
LookUp: comparison controlled traversal
worst-case: height T
1 comparisons
A binary search tree (BST) is a binary tree such that:
Information associated with a node includes a key,
linear ordering of nodes determined by keys.
LC
28
Page 11
2
4
The key of each node is:
greater than the keys of all left descendants, and
smaller than the keys of all right descendants.
13
TDDB57 DALG – Lecture 6: ADT Tree, Binary Search Trees
1
Different BST representing the same data
1
2
4
5
8
6
12
11
TDDB57 DALG – Lecture 6: ADT Tree, Binary Search Trees
Insert in BST
42
21
44
24
48
49
20
42
21
44
24
27
Delete(21)
6
28
Insert(27)
Page 13
22
Page 15
56
48
49
12
11
J. Maluszynski, IDA, Linköpings Universitet, 2004.
42
20
44
24
28
48
49
56
J. Maluszynski, IDA, Linköpings Universitet, 2004.
56
22
42
21
44
24
28
TDDB57 DALG – Lecture 6: ADT Tree, Binary Search Trees
12
20
Delete in BST
6
11
inorder
successor
22
TDDB57 DALG – Lecture 6: ADT Tree, Binary Search Trees
48
49
Delete(12)
Page 14
56
Page 16
6
Successful LookUp: Worst and Average Case
20
11
Worst Case BST
BST degenerated to a linear sequence
expected number of comparisons is n 1 2
22
Balanced BST
the depths of leaves differ by at most 1
Apply the Binary Search Expected Time Theorem
O log2 n comparisons.
TDDB57 DALG – Lecture 6: ADT Tree, Binary Search Trees
12
20
28
Delete in BST: an alternative approach
6
11
22
inorder
predecessor
44
48
49
56
J. Maluszynski, IDA, Linköpings Universitet, 2004.
42
21
24
28
J. Maluszynski, IDA, Linköpings Universitet, 2004.
Download