Sie sind auf Seite 1von 6

CmSc 175 Discrete Mathematics

Lesson 23: Trees


Examples of trees
family trees
organizational hierarchies
parsing English sentences
disk directories
expression trees

Here is an example of a tree:

root
A
Child (of root)

M
L N

C O
R D

Leaves or terminal nodes


Parent (of P, U, T, E)

P U T E

1
Definitions

Tree - a connected undirected graph with no cycles, where one of the vertices is
chosen to be the root. When we choose a root we impose an orientation of the edges
from the root to the adjacent vertices, and then from these vertices to their adjacent
vertices, etc,
Node, vertex - can have a name and carry other associated information
Root - starting point (top) of the tree
Parent (ancestor) of a node A - the node above node A
Child (descendent) of a node A - the node below node A
Siblings - nodes that have same parent
Leaves (terminal nodes) nodes with no children
Level of a node - then number of edges between this node and the root
Path from node ni to nm: a sequence of nodes ni1, ni2, nm such that n k is the parent
of n k+1
Depth of a node - the length of the path from the root to that node
Depth of the root: 0
Height of a node - the length of the longest path from that node to a leaf
Height of any leaf: 0
Height of a tree the length of the longest path from the root to a leaf (same as
height of the root)
Binary tree each node has at most two children
Perfect binary tree each node except the leaves has exactly two children and all
leaves have same depth

Properties:

Exactly one path connecting any two nodes


A tree with N nodes has N - 1 edges

2
Examples of various trees
A binary tree:

P T
O E

C L
M

R N

A D

Expression tree: A binary tree, the leaves contain the operands of the expression, and the
other nodes contain the operators.

+ +

a *
d e

b c

(a + b*c) * ( d + e)

3
A game tree:

A parse tree:

4
Computing the height of a perfect binary tree with N nodes

Consider the following binary tree:

Level/ depth 0

Level/ depth 1

Level/ depth 2

Level/ depth 3

At each level the number of the nodes is doubled.

There are three levels, and the total number of nodes is:

1 + 2 + 22 + 23 = 24 - 1 = 15

Note that 24 - 1 = 2*23 - 1


Note also that 3 = log(23)

In general, if we have a tree with M levels, the number of the nodes would be:

1 + 2 + 22 + . 2M = 2(M+1) - 1 = 2*2M - 1

Let N be the number of the nodes.

We will find now how M (the height of the tree) depends on the number of the nodes.

N = 2*2M - 1

2*2M = N + 1

2M = (N+1)/2

M = log((N+1)/2)

when the tree is a list - all nodes except the leaf have only one child:
height : N-1

A perfect binary tree with p levels contains 2 p + 1 1 nodes

5
Binary Tree Traversals:

O T
M E

P U R

L A
Pre-order traversal:
Visit the root
Pre-order traverse left sub-tree
Pre-order traverse right sub-tree N D
The tree above in pre-order traversal reads C O M P U T E R L A N D

In-order Traversal
In-order traverse left sub-tree
Visit the root
In-order traverse right sub-tree

The tree above in in-order traversal reads P M U O C T E L R N A D

Post-order Traversal
Post-order traverse left sub-tree
Post-order traverse right sub-tree
Visit the root

The tree above in post-order traversal reads P U M O L N D A R E T C

Breadth first traversal - visit all nodes in level 0, then in level 1, etc.
Depth first traversal = Pre-order traversal for binary trees

Note: Depth-first and breadth-first traversals can be applied to any tree

Das könnte Ihnen auch gefallen