Download Why Trees-Computer Programming For Aeronautical Engineering And Sciences-Lecture Slides and more Slides Aeronautical Engineering in PDF only on Docsity!
[Theorem]
- Theorem : a mathematical statement that can be shown to be true - Can be proved using other theorems, axioms (statements which are given to be true) and rules of inference
- Lemma : a pre-theorem or result needed to prove a theorem
- Corollary : post-theorem or result which follows directly from a theorem
- Proposition
- Claim
- Remark
Why should we use trees?
Binary Search Tree
5
A tree is a connected undirected graph
with no simple circuits.
- it cannot contain multiple edges or loops
Trees
Theorem : An undirected graph is a tree if and only if there is a unique simple path between any two of its vertices.
6
Which graphs are trees?
a) b)
c) d)
9
• A vertex that has children is called an
internal vertex
• A graph H( W, F ) is a subgraph of a
graph G ( V,E ) iff W ⊆ V and F ⊆ E
• The subtree at vertex v is the subgraph
of the tree consisting of vertex v and its
descendants and all edges incident to
those descendants
Internal Vertex
10
- The parent of a non-root vertex v is the unique vertex u with a directed edge from u to v.
- A vertex is called a leaf if it has no children.
- The ancestors of a non-root vertex are all the vertices in the path from root to this vertex.
- The descendants of vertex v are all the vertices that have v as an ancestor.
Tree Properties
11
• The level of vertex v in a rooted tree is the
length of the unique path from the root to v.
• The height of a rooted tree is the maximum
of the levels of its vertices.
Tree Properties
c
a
b
d e f g
k l
m
h i j
Level of vertex f = Height of tree =
Level of vertex f = Height of tree =
2 4
12
• An m-ary tree is a rooted tree in which
each internal vertex has at most m children
• A rooted tree is called a binary tree if
every internal vertex has no more than 2
children.
• The tree is called a full binary tree if every
internal vertex has exactly 2 children.
Binary Tree
15
• An ordered rooted tree is a rooted
tree where the children of each
internal vertex are ordered.
• In an ordered binary tree, the two
possible children of a vertex are called
the left child and the right child, if
they exist.
Ordered Binary Tree
Example
Children of b? Parent of b? Ancestors of g? Descendants of b?
Leafs? Internal vertices? Left child of g? Right child of g?
c
a
b
d e f g
k l
m
h i j
d, e a c, a
FAREWELL TO AMY ADAMS The party atmosphere of Motown and theterrific backing rhythms of the Funk Brothers just couldn't bring out the best in Amy, whowas sent home this week.
d, e, h, i
h, i, e, j, k, m a, b, c, d, f, g k l
17
- A traversal algorithm is a procedure for systematically visiting every vertex of an ordered binary tree
- Tree traversals are defined recursively
- Three commonly used traversals are:
- preorder
- inorder
- postorder
Traversal Algorithms
18
Let T be an ordered binary tree with root R
If T has only R then R is the preorder traversal Else Let T 1 , T 2 be the left and right subtrees at R Visit R Traverse T 1 in preorder Traverse T 2 in preorder
PREORDER Traversal Algorithm
21
POSTORDER Traversal Algorithm
Let T be an ordered binary tree with root R
If T has only R then R is the postorder traversal Else Let T 1 , T 2 be the left and right subtrees at R Traverse T 1 in postorder Traverse T 2 in postorder Visit R
22
A special kind of binary tree in which:
- Each leaf node contains a single operand
- Each inner vertex contains a single binary operator
- The left and right subtrees of an operator node represent sub-expressions that must be evaluated before applying the operator at the root of the subtree.
Binary Expression Tree
23
Binary Expression Tree
ROOT
INORDER TRAVERSAL: 8 - 5 has value 3 PREORDER TRAVERSAL: - 8 5 POSTORDER TRAVERSAL: 8 5 -
24
Binary Expression Tree
What value does it have?What value does it have?
( 4 + 2 ) * 3 = 18
27
Binary Expression Tree
**Infix: ( ( 8 - 5 ) * ( ( 4 + 2 ) / 3 ) ) Prefix: * - 8 5 / + 4 2 3 Postfix: 8 5 - 4 2 + 3 / ***
Trees - Glossary
Perfectly balanced tree Height balanced tree
Root
Leaf
Inner Vertex
A
B C
Parent of B and C
Child of A
B and C are siblings
M-ary tree