Title: Trees II
1Trees II
- Kruse and Ryba
- Ch 10.1,10.2,10.4 and 11.3
2Menu
- Taxonomy tree (decision tree)
- Tree traversal
- Heaps
- B-trees
- Other trees
- binary space partitioning trees
- game trees
3Binary Taxonomy Tree
Are you a mammal?
yes
no
Are you bigger than a cat?
Do you live underwater?
yes
no
yes
no
Are you a primate?
Are you a house pet?
fish
legs?
yes
no
yes
no
yes
no
Talks?
pig
Barks?
mouse
toad
slug
yes
no
yes
no
dog
cat
human
slug
4Implementation Tree Nodes
Using a typedef statement struct
BinaryTreeNode typedef string Item
Item data BinaryTreeNode left
BinaryTreeNode right
Using a template parameter template ltclass
Itemgt struct BinaryTreeNode Item data
BinaryTreeNode left BinaryTreeNode
right
5Binary Tree Represented with BinaryTreeNode
Structs
Using a template parameter template ltclass
Itemgt struct BinaryTreeNode Item data
BinaryTreeNode left BinaryTreeNode
right
A
G
L
T
R
O
6A
G
L
root_ptr
T
R
O
data A
left
right
data L
data G
left
right
left
right
data O
data T
data R
left
right
left
right
left
right
7Tree Traversal
- Pre order process parent before children
- Post order process parent after children
- In order process left subtree, parent, right
subtree
Pre order C A T Post order A T C In order A C T
C
T
A
8In-Order Traversal
- Process the nodes in the left subtree with a
recursive call - Process the root
- Process the nodes in the right subtree with a
recursive call
9Implementation
template ltclass Itemgt void inorder_print(BinaryTre
eNodeltItemgt node_ptr) if(node_ptr !
NULL) inorder_print(node_ptr-gtleft) cout
ltlt node_ptr-gtdata ltlt endl inorder_print(node_pt
r-gtright)
10A
G
L
root_ptr
T
R
O
data A
left
right
data L
data G
left
right
left
right
data O
data T
data R
left
right
left
right
left
right
11A
G
L
root_ptr
T
R
O
data A
left
right
data L
data G
left
right
left
right
data O
data T
data R
left
right
left
right
left
right
12O
A
G
L
root_ptr
T
R
O
data A
left
right
data L
data G
left
right
left
right
data O
data T
data R
left
right
left
right
left
right
13O L
A
G
L
root_ptr
T
R
O
data A
left
right
data L
data G
left
right
left
right
data O
data T
data R
left
right
left
right
left
right
14O L R
A
G
L
root_ptr
T
R
O
data A
left
right
data L
data G
left
right
left
right
data O
data T
data R
left
right
left
right
left
right
15O L R A
A
G
L
root_ptr
T
R
O
data A
left
right
data L
data G
left
right
left
right
data O
data T
data R
left
right
left
right
left
right
16O L R A G
A
G
L
root_ptr
T
R
O
data A
left
right
data L
data G
left
right
left
right
data O
data T
data R
left
right
left
right
left
right
17O L R A G T
A
G
L
root_ptr
T
R
O
data A
left
right
data L
data G
left
right
left
right
data O
data T
data R
left
right
left
right
left
right
18In-order Traversal of Binary Search Trees
5
453
10
3
600
55
11
6
2
4
9
62
In order traversal 2, 3, 4, 5, 6, 10, 11
In order traversal 9, 55, 62, 453, 600
19Representing Equations As Binary Trees
Each internal node stores a binary operator Each
leaf stores a value (or variable)
6
-
-
/
2
5
22
22
2
5
22
In order traversal (22 2) (225)
In order traversal ((22/5) 2)6
20Pre-Order Traversal
- Process the nodes in the left subtree with a
recursive call - Process the nodes in the right subtree with a
recursive call - Process the root
21Implementation
template ltclass Itemgt void preorder_print(BinaryTr
eeNodeltItemgt node_ptr) if(node_ptr !
NULL) preorder_print(node_ptr-gtleft) preor
der_print(node_ptr-gtright) cout ltlt
node_ptr-gtdata ltlt endl
22Post-Order Traversal
- Process the root
- Process the nodes in the left subtree with a
recursive call - Process the nodes in the right subtree with a
recursive call
23Implementation
template ltclass Itemgt void postorder_print(BinaryT
reeNodeltItemgt node_ptr) if(node_ptr !
NULL) cout ltlt node_ptr-gtdata ltlt
endl postorder_print(node_ptr-gtleft) postord
er_print(node_ptr-gtright)
24Heaps
The next nodes always fill the next level from
left-to-right.
25Heaps
The next nodes always fill the next level from
left-to-right.
26Heaps
27Heaps
45
- A heap is a certain kind of complete binary tree.
23
35
4
22
21
27
19
Each node in a heap contains a key that can be
compared to other nodes' keys.
28Heaps
45
- A heap is a certain kind of complete binary tree.
- Can be used to store priority queue
23
35
4
22
21
27
19
The "heap property" requires that each node's key
is gt the keys of its children
29Adding a Node to a Heap
45
- Put the new node in the next available spot.
- Push the new node upward, swapping with its
parent until the new node reaches an acceptable
location.
23
35
4
22
21
27
19
42
30Adding a Node to a Heap
45
- Put the new node in the next available spot.
- Push the new node upward, swapping with its
parent until the new node reaches an acceptable
location.
23
35
4
22
21
42
19
27
31Adding a Node to a Heap
45
- Put the new node in the next available spot.
- Push the new node upward, swapping with its
parent until the new node reaches an acceptable
location.
23
42
4
22
21
35
19
27
32Adding a Node to a Heap
45
- The parent has a key that is gt new node, or
- The node reaches the root.
- The process of pushing the new node upward
is called reheapification
upward.
23
42
4
22
21
35
19
27
33Removing the Top of a Heap
45
- Move the last node onto the root.
23
42
4
22
21
35
19
27
34Removing the Top of a Heap
27
- Move the last node onto the root.
23
42
4
22
21
35
19
35Removing the Top of a Heap
27
- Move the last node onto the root.
- Push the out-of-place node downward, swapping
with its larger child until the new node reaches
an acceptable location.
23
42
4
22
21
35
19
36Removing the Top of a Heap
42
- Move the last node onto the root.
- Push the out-of-place node downward, swapping
with its larger child until the new node reaches
an acceptable location.
23
27
4
22
21
35
19
37Removing the Top of a Heap
42
- Move the last node onto the root.
- Push the out-of-place node downward, swapping
with its larger child until the new node reaches
an acceptable location.
23
35
4
22
21
27
19
38Removing the Top of a Heap
42
- The children all have keys lt the out-of-place
node, or - The node reaches the leaf.
- The process of pushing the new node downward
is called reheapification
downward.
23
35
4
22
21
27
19
39Implementing a Heap
42
- We will store the data from the nodes in a
partially-filled array.
23
35
21
27
An array of data
40Implementing a Heap
42
- Data from the root goes in the first
location of the
array.
23
35
21
27
42
An array of data
41Implementing a Heap
42
- Data from the next row goes in the next two array
locations.
23
35
21
27
42
35
23
An array of data
42Implementing a Heap
42
- Data from the next row goes in the next two array
locations.
23
35
21
27
42
35
23
27
21
An array of data
43Implementing a Heap
42
- Data from the next row goes in the next two array
locations.
23
35
21
27
42
35
23
27
21
An array of data
We don't care what's in this part of the array.
44Important Points about the Implementation
42
- The links between the tree's nodes are not
actually stored as pointers, or in any other way. - The only way we "know" that "the array is a tree"
is from the way we manipulate the data.
23
35
21
27
42
35
23
27
21
An array of data
45B-Trees
- B-tree is a special kind of tree, similar to a
binary search tree where each node holds entries
of some type. - But a B-tree is not a binary tree
- nodes can have more than two children
- Nodes can contain more than just a single entry
- Rules of B-tree make it easy to search for an
item. - Also ensures that the leaves do not become too
deep.
46B-tree Example
93 and 107
subtree number 0
subtree number 1
subtree number 2
Each entry in subtree 0 is less than 93
Each entry in subtree 2 is greater than 107
Each entry in subtree 1 is between 93 and 107
47B-tree Rules
- 1. Every node has at least MINIMUM number of
entries. MINIMUM can be as small as 1, or as big
as 100s or 1000s. - 2. Maximum number of entries in a node is twice
the value of MINIMUM. - 3. The entries in a B-tree are stored in a
partially-filled array, sorted from the smallest
entry to the largest entry. - 4. The number of subtrees below a node depends on
how many entries are in the node.
48B-tree Rule 4
The number of subtrees below a node depends on
how many entries are in the node.
93 and 107
subtree number 0
subtree number 1
subtree number 2
Each entry in subtree 2 is greater than 107
Each entry in subtree 0 is less than 93
Each entry in subtree 1 is between 93 and 107
49B-tree Rule 5
For any non-leaf node (a) an entry at index i is
greater than all the entries in subtree number i
of the node, and (b) an entry at index i is less
than all the entries in subtree number i1 of the
node.
93 and 107
subtree number 0
subtree number 1
subtree number 2
Each entry in subtree 2 is greater than 107
Each entry in subtree 0 is less than 93
Each entry in subtree 1 is between 93 and 107
50B-tree Rule 6
Every leaf in a B-tree has the same depth.
6
2 and 4
9
7 and 8
10
1
3
5
51Operations on B-trees
- Searching for an item easy
- Inserting item see text
- Removing item see text
6
2 and 4
9
7 and 8
10
1
3
5
52Binary Space Partitioning Tree
2D Example We are given a set of N
points in the plane. Each point has an
associated (x,y) position.
53Binary Space Partitioning Tree
Problem How to efficiently determine if
a query point (x,y) is contained in the set of
points?
??
54Possible Solutions
- Go through N points comparing to see if their
(x,y) value matches query point (x,y). Drawback
O(N) complexity. - Use some sort of tree structure to organize the
points, and formulate query in terms of searching
tree. Assuming uniform distribution of points,
O(logN) complexity. - How to structure tree??? Partition space using
Binary Space Partitioning (BSP).
55Building BSP Tree
Approach Recursively split space in
half, in such a way that number of points in each
half is roughly equal.
56Building BSP Tree
Approach Recursively split space in
half, in such a way that number of points in each
half is roughly equal.
57Building BSP Tree
Approach Recursively split space in
half, in such a way that number of points in each
half is roughly equal.
58Building BSP Tree
Approach Recursively split space in
half, in such a way that number of points in each
half is roughly equal.
59Building BSP Tree
- Recursively divide set of points using planes
positioned that split the point set roughly in
half. - Stop recursively dividing when set is empty, or
set is smaller than threshold M, or maximum tree
depth is reached.
60 BSP Tree
Result
61 BSP Tree Search
Is there a point in set that matches query point
(x,y)?
62 BSP Tree Search
Is there a point in set that matches query point
(x,y)?
63 BSP Tree Search
Is there a point in set that matches query point
(x,y)?
64 BSP Tree Search
Is there a point in set that matches query point
(x,y)?
65 BSP Tree Search
Compare query point only with those points in
this leaf node.
66BSP Tree Search Complexity
- Assume that points are randomly, uniformly
distributed in space (within some range). - We recursively split point set in half at each
internal node. - For analysis, assume that we allow trees of
unlimited depth, and that each leaf can contain
only one point. - Average case complexity, given N points?
67Game Trees
- Consider writing a computer program that plays
Tic-Tac-Toe against a human...
human makes first move... where should the
computer move??? There are 8 possible choices.
X
68Game Trees
- The computer can make 8 possible moves
X
Each possible choice represents one node at that
level in the game tree. Which one should be
chosen??
69Game Trees
- The computer can make 8 possible moves
X
Each possible choice represents one node at that
level in the game tree. Which one should be
chosen??
70Game Tree Look Ahead
- For each possible move, see what possible moves
are for opponent. - For instance
X
O
Each node represents a possible move that the
opponent could take.
71Game Tree Look Ahead
- Proceed recursively, testing all possible moves
until opponent or computer wins.
X
O
X
Choose a move that leads to the most possible
winning moves!!
72Game Trees
- For each turn in the game, build game tree that
tests possible move scenarios. - Choose move that maximizes number of possible
wins. - Parts of the tree are reusable (depending on the
opponents choice of next move). - Problem tree is quite large. Assume we have N9
possible positions, what is the potential size
O(f(N)) number of nodes?