Trees - SIUE Computer Science

advertisement
MATH 224 – Discrete Mathematics
Rooted Trees
A rooted tree is a tree that has a distinguished node called the root. Just as with all
trees a rooted tree is acyclic (no cycles) and connected. Rooted trees have many
applications in computer science, for example the binary search tree.
The root is typically drawn at the top.
Children of the root.
Grandchildren of the root and leaf nodes.
4/13/2015
1
MATH 224 – Discrete Mathematics
Rooted Binary Trees
A Binary tree is a rooted tree where no node has more than two children. As shown in
the previous slide all nodes, except for leaf nodes, have children and some have
grandchildren. All nodes except the root have a parent and some have a grandparent
and ancestors. A node has at most one parent and at most one grandparent.
The root of a binary tree at level 4 also height 4 and depth 0.
Nodes at level 3 and depth 1.
Nodes with only 1 child at level 2 and depth 2.
Nodes with no children
are called leaf nodes.
Nodes a level 1, and depth 3.
Nodes a level 0, and depth 4.
4/13/2015
2
MATH 224 – Discrete Mathematics
Balanced Trees
A Balanced tree is a rooted tree where the leaf nodes have depths that vary by no
more than one. In other words, the depth of a leaf node is either equal to the height of
the tree or one less than the height. All of the trees below are balanced.
What are the heights of each of these trees?
4/13/2015
3
MATH 224 – Discrete Mathematics
A Binary Search Tree
15
10
30
35
5
8
4/13/2015
32
45
4
MATH 224 – Discrete Mathematics
Some Tree Applications
Binary Search Trees to store items for easy retrieval, insertion and deletion. For
balanced trees, each of these steps takes lg(N) time for an N node tree.
Variations of the binary search tree include, the AVL tree, Red-Black tree and the
B-tree. These are all designed to keep the tree nearly balanced. The first two are
binary trees while the B-tree has more than two children for each internal node.
Game trees are used extensively in AI. The text has a simple example of a tic-tactoe tree on Page 654.
Huffman trees are used to compress data. They are most commonly used to
compress faxes before transmission.
Spanning trees are subgraphs that have applications to computer and telephone
networks. Minimum spanning trees are of special interest.
Steiner trees are a generalization of the spanning tree with are useful in multicast
communication.
4/13/2015
5
MATH 224 – Discrete Mathematics
Binary Tree Traversal Functions
void inorder(node T)
if (T ≠ null) {
inorder(T.left)
print(T .data) ; print(“, ”)
inorder(T.right)
} fi
b
end inorder
d
a
c
e
f
g
h
output will be something like
i
j
d, b, e, a, f, c, g, i, h, j
4/13/2015
6
MATH 224 – Discrete Mathematics
Binary Tree Traversal Functions
void inorder(node T)
if (T ≠ null) {
inorder(T.left)
print(T .data) ; print(“, ”)
inorder(T.right)
} fi
b
end inorder
T is NULL
d
a
c
e
f
g
T is d
prints d
T is b
h
i
j
T is a
main calls
inorder
4/13/2015
7
MATH 224 – Discrete Mathematics
Binary Tree Traversal Functions
void pre_order(node T)
if (T ≠ null) {
cout << T −>data << “, ”
pre_order(T−>left)
pre_order(T −>right)
b
}
end pre_order
d
a
c
e
f
g
h
output will be something like
i
j
a, b, d, e, c, f, g, h, i, j
4/13/2015
8
MATH 224 – Discrete Mathematics
Binary Tree Traversal Functions
void post_order(node T)
if (T ≠ null) {
post_order(T−>left)
post_order(T − >right)
cout << T − >data << “, ”
}
end post_order
d
a
b
c
e
f
g
h
output will be something like
i
j
d, e, b, f, i, j, h, g, c, a
4/13/2015
9
MATH 224 – Discrete Mathematics
Counting the nodes in a Binary Tree
int count(node T)
int num = 0
if (T ≠ null)
num = 1 + count(T−>left) + count(T− >right)
fi
a
return num
end count
b
c
d
e
f
g
h
i
4/13/2015
j
10
MATH 224 – Discrete Mathematics
The Height of a Binary Tree
int height(node T)
int ht = −1
if (T ≠ null)
ht = 1 + max(height(T−>left), height(T−>right))
fi
return ht
a
end height
b
c
Called initially at a
Then at b
d
f
e
Then a d
Then at NULL
Returns −1 to d from left and right
Returns 0 to b from d and from e
Returns 1 to a from left.
Returns 3 to a from right
Returns 4 from a to original caller
4/13/2015
g
h
i
j
11
MATH 224 – Discrete Mathematics
Inserting into a Binary Search Tree
// Note that T is passed by reference
insert(node & T, int X)
if (T = = null)
T = node(X)
else if X < T−>value
insert(T −>left, X)
else
insert(T −>right, X)
fi
end height
Where would 60 be inserted?
If the original value for T is the
node containing 30, where is the
first recursive call, the second etc?
Where would −10 be inserted?
4/13/2015
30
22
17
45
24
32
70
84
75
98
12
MATH 224 – Discrete Mathematics
What Does this Code do?
Boolean T_alg(node T, int X)
if (T = = null)
return FALSE
else {
print T.value
if X = =T.value
return TRUE
else if T.value > X
return T_alg(T.left, X)
else return T_alg(T.right X)
} //fi
} //fi
} //end T_alg
4/13/2015
30
22
17
45
32
24
70
28
26
84
75
98
13
MATH 224 – Discrete Mathematics
Counting the Nodes with even Integers in a Binary Tree
int even_count(node T)
int num = 0
if (T ≠ null)
num = (1 - T.data%2) + even_count(T−>left) + even_count(T− >right)
fi
15
return num
end even_count
20
4
31
8
6
3
9
5
4/13/2015
2
14
MATH 224 – Discrete Mathematics
What does cnt return when given a Binary Tree
int cnt(node T)
int num = 0
if (T ≠ null) {
num = cnt(T−>left) + cnt(T− >right)
if (num = = 0)
num = 1
15
fi
}fi
20
return num
end even_count
31
8
4
6
3
9
5
4/13/2015
2
15
Download