Title: Trees
1 2Trees
- Definition A tree is a connected undirected
graph with no simple circuits. - Since a tree cannot have a simple circuit, a tree
cannot contain multiple edges or loops. - Therefore, any tree must be a simple graph.
- Theorem An undirected graph is a tree if and
only if there is a unique simple path between any
of its vertices.
3Trees
- Example Are the following graphs trees?
Yes.
No.
No.
Yes.
4Trees
- Definition An undirected graph that does not
contain simple circuits and is not necessarily
connected is called a forest. - In general, we use trees to represent
hierarchical structures. - We often designate a particular vertex of a tree
as the root. Since there is a unique path from
the root to each vertex of the graph, we direct
each edge away from the root. - Thus, a tree together with its root produces a
directed graph called a rooted tree.
5Applications of Trees
- There are numerous important applications of
trees, only three of which we will discuss today - Network optimization with minimum spanning
trees - Problem solving with backtracking in decision
trees - Data compression with prefix codes in Huffman
coding trees
6Tree Terminology
- If v is a vertex in a rooted tree other than the
root, the parent of v is the unique vertex u such
that there is a directed edge from u to v. - When u is the parent of v, v is called the child
of u. - Vertices with the same parent are called
siblings. - The ancestors of a vertex other than the root are
the vertices in the path from the root to this
vertex, excluding the vertex itself and including
the root.
7Tree Terminology
- The descendants of a vertex v are those vertices
that have v as an ancestor. - A vertex of a tree is called a leaf if it has no
children. - Vertices that have children are called internal
vertices. - If a is a vertex in a tree, then the subtree with
a as its root is the subgraph of the tree
consisting of a and its descendants and all edges
incident to these descendants.
8Tree Terminology
- The level of a vertex v in a rooted tree is the
length of the unique path from the root to this
vertex. - The level of the root is defined to be zero.
- The height of a rooted tree is the maximum of the
levels of vertices.
9Terminology
- The degree of a node is the number of subtrees of
the node - The node with degree 0 is a leaf or terminal
node. - A node that has subtrees is the parent of the
roots of the subtrees. - The roots of these subtrees are the children of
the node. - Children of the same parent are siblings.
- The ancestors of a node are all the nodes along
the path from the root to the node.
10Tree Properties
Property Value Number of nodes Height Root
Node Leaves Interior nodes Number of
levels Ancestors of H Descendants of B Siblings
of E Right subtree
A
B
C
D
E
F
G
I
H
11Trees
James
Christine
Bob
Frank
Joyce
Petra
12Trees
/
usr
temp
bin
bin
spool
ls
13Trees
- Example III Arithmetic expressions
?
-
y
z
x
y
This tree represents the expression (y z)?(x -
y).
14Trees
- Definition A rooted tree is called an k-ary tree
if every internal vertex has no more than k
children. - The tree is called a full k-ary tree if every
internal vertex has exactly k children. - An k-ary tree with k 2 is called a binary tree.
- Theorem A tree with n vertices has (n 1)
edges. - Theorem A full k-ary tree with i internal
vertices contains n ki 1 vertices.
15Binary Search Trees
- If we want to perform a large number of searches
in a particular list of items, it can be
worthwhile to arrange these items in a binary
search tree to facilitate the subsequent
searches. - A binary search tree is a binary tree in which
each child of a vertex is designated as a right
or left child, and each vertex is labeled with a
key, which is one of the items. - When we construct the tree, vertices are assigned
keys so that the key of a vertex is both larger
than the keys of all vertices in its left subtree
and smaller than the keys of all vertices in its
right subtree.
16Binary Search Trees
- Example Construct a binary search tree for the
strings math, computer, power, north, zoo,
dentist, book.
math
17Binary Search Trees
- Example Construct a binary search tree for the
strings math, computer, power, north, zoo,
dentist, book.
math
computer
18Binary Search Trees
- Example Construct a binary search tree for the
strings math, computer, power, north, zoo,
dentist, book.
math
power
computer
19Binary Search Trees
- Example Construct a binary search tree for the
strings math, computer, power, north, zoo,
dentist, book.
math
power
computer
north
20Binary Search Trees
- Example Construct a binary search tree for the
strings math, computer, power, north, zoo,
dentist, book.
math
power
computer
north
zoo
21Binary Search Trees
- Example Construct a binary search tree for the
strings math, computer, power, north, zoo,
dentist, book.
math
power
computer
north
zoo
dentist
22Binary Search Trees
- Example Construct a binary search tree for the
strings math, computer, power, north, zoo,
dentist, book.
math
power
computer
north
zoo
dentist
book
23Binary Search Trees
- To perform a search in such a tree for an item x,
we can start at the root and compare its key to
x. If x is less than the key, we proceed to the
left child of the current vertex, and if x is
greater than the key, we proceed to the right
one. - This procedure is repeated until we either found
the item we were looking for, or we cannot
proceed any further. - In a balanced tree representing a list of n
items, search can be performed with a maximum of
?log(n 1)? steps (compare with binary search).
24Representation of Tree Nodes
- Every tree node has the following
- Object or data useful information
- Children pointers to its children nodes
O
O
O
O
O
25Left Child - Right Sibling
26Tree Implementation
- typedef struct tnode
- int key
- struct tnode lchild
- struct tnode sibling
- ptnode
- Create a tree with three nodes (one root two
children) - Insert a new node (in tree with root R, as a new
child at level L) - Delete a node (in tree with root R, the first
child at level L)
27Binary Trees
- A special class of trees max degree for each
node is 2 - Recursive definition A binary tree is a finite
set of nodes that is either empty or consists of
a root and two disjoint binary trees called the
left subtree and the right subtree. - Any tree can be transformed into binary tree.
- by left child-right sibling representation
28Example
A
B
C
E
D
G
F
K
H
L
I
M
J
29Node Structure for Binary Trees
Left data Right
typedef struct TreeNode PtrToNode typedef
struct PtrToNode Tree struct TreeNode
ElementType data Tree
left Tree right
30Tree Traversal
- Three main methods
- Preorder
- Postorder
- Inorder
- Recursive definition
- PREorder
- Visit the root
- Traverse in preorder the children (subtrees)
- POSTorder
- Traverse in postorder the children (subtrees)
- Visit the root
- INorder
- Traverse inorder the left child (subtree)
- Visit the root
- Traverse inorder the rest of the subtrees
31Preorder
- preorder traversal
- Algorithm preOrder(v)
- visit node v
- for each child w of v do
- recursively perform preOrder(w)
A
B
C
D
E
F
G
A B D E G H I F C
I
H
32Preorder Traversal (recursive version)
void preorder(ptnode ptr) / preorder tree
traversal / if (ptr)
printf(d, ptr-data)
preorder(ptr-left) predorder(ptr-right)
33Postorder
- postorder traversal
- Algorithm postOrder(v)
- for each child w of v do
- recursively perform postOrder(w)
- visit node v
A
B
C
D
E
F
G
D H I G E F B C A
I
H
34Postorder Traversal (recursive version)
void postorder(ptnode ptr) / postorder tree
traversal / if (ptr)
postorder(ptr-left) postdorder(ptr-righ
t) printf(d, ptr-data)
35Inorder
- inorder traversal
- Algorithm inOrder(v)
- for left child w of v do
- recursively perform inOrder(w)
- visit node v
- for remaining child vertices u do
- recursively perform inOrder(u)
A
B
C
D
E
F
G
D B H G I E F A C
I
H
36Inorder Traversal (recursive version)
void inorder(ptnode ptr) / inorder tree
traversal / if (ptr)
inorder(ptr-left) printf(d,
ptr-data) indorder(ptr-right)
37Program 7
- Implement a binary search tree. Build it using
the representation on page 97. Your binary tree
will hold 30-character strings. It will
initially be empty, and the user will be prompted
for data to insert in the tree. The following
options will be provided in your program - insert data include a file option
- preorder printout
- inorder printout
- postorder printout