You are on page 1of 42

TREES

dddobach
Trees
 Generic Tree
 A Tree
 UNIX directory
 Organizational Chart
Trees: Mathematical Definition
Definition:
A tree T is a finite, non-empty set of nodes ,
 =  ∪  ∪  …  ,
with the following properties:
1. A designated node of the set, r, is called the root of the
tree; and
2. The remaining nodes are partitioned into
≥ 0
subsets,  ,  , …  , each of which is a tree
Trees: Mathematical Definition
Trees: Example
 A minimal tree
 Comprised of a single node

A



Trees: Example
 When there is more than one node, the remaining
nodes are partitioned into subtrees.


 = {, {}}
Trees: Example
 When there is more than one node, the remaining
nodes are partitioned into subtrees.

 = {, ,  , {, ,  , ,  ,  ,  }}
Trees: Terminology
 Given a tree  = ,  ,  …  ,
≥ 0
 The degree of a node is the number of subtrees
associated with that node. For example, the degree of
tree T is n
 A node of degree zero has no subtrees. Such a node is
called a leaf .
 Each root  of subtree  of tree T is called a child of r.
The term grandchild is defined in a similar manner.
Trees: Terminology
 Given a tree  = ,  ,  …  ,
≥ 0

 The root node r of tree T is the parent of all the


roots  of the subtrees  , 1 < " <
. The
term grandparent is defined in a similar manner.
 Two roots  and # of distinct subtrees  and # of
tree T are called siblings .
Trees: Definition
 Path and Path length
Given a tree T containing the set of nodes R, a
path in T is defined as a non-empty sequence of nodes
$ =  ,  , … % ,
Where  ∈ ', for 1 ≤ " ≤ ) such that the " *+ node in
the sequence,  , is the parent of the (" + 1)*+ node in
the sequence / .

Length of path P is k-1 (the number of edges).


Trees: Definition
 The level or depth of a node  ∈ ' in a tree T is
the length of the unique path in T from its root r to
the node  .
 The height of a node  ∈ ' in a tree T is the length
of the longest path from node  to a leaf.
 The height of a tree T is the height of its root node r.
Trees: Definition
 Consider two nodes  and # in a tree T. The node 
is an ancestor of the node # if there exist a path in T
from  to # .
 A node is its own ancestor
 Proper ancestor – if the length of the path is non-
zero
Trees: Definition
 Node # is a descendant of the node  if there exist
a path in T from  to # .
 A node is its own descendant
 Proper descendant – if the length of the path is non-
zero
Question!
 How many paths are there in tree  ?
Trees: Applications
 Directory structure in many common operating
systems
Binary Tree

Representation of a binary tree, T. Each node x has fields


p[x] (top), left[x] (lower left), and right[x] (lower right). The
key fields are not shown.
Binary Tree
Definition:
A binary tree T is a finite set of nodes with the
following properties:
1. Either the set is empty,  = ∅; or
2. The set consists of a root, , and exactly two distinct
binary trees 1 and 2 ,  = {, 1 , 2 }.
The tree 1 is the left subtree of T
The tree 2 is the right subtree of T
Binary Tree:
- Generic Binary Tree
Binary Tree
- Depth of an average binary tree is considerably
smaller than N
- Average depth is Ο( 4)
- Binary search tree: Average depth is Ο(log 4)
Binary Tree: Worst case
- However, depth can be as large as N-1 as shown
Tree Traversal
 An algorithm that systematically visits all the items in
a tree
 Methods :
1. depth-first traversal
 Preorder
 Inorder
 Postorder
2. breadth-first traversal
Trees

A Depth = 0
B D Depth = 1

C E H Depth = 2

F G I Depth = 3
 A sample tree
Depth-first traversal
 Preorder Traversal
1. Visit the root first; then
2. Do a preorder traversal each of the subtrees of the
root one-by-one in the order given.

 In case the tree is a binary tree


1. Visit the root first;
2. traverse the left subtree;
3. traverse the right subtree.
Depth-first traversal
 Pre order traversal?
A
B D

C E H

F G I
Pre order: 8, , , , , , , , 
Depth-first traversal
 Postorder Traversal
 Visit the root last
1. Do a postorder traversal each of the subtrees of the root
one-by-one in the order given;
2. visit the root
 In case the tree is a binary tree
1. Traverse the left subtree;
2. traverse the right subtree;
3. visit the root.
Depth-first traversal
 Post order traversal?
A
B D

C E H

F G I
Postorder: , , , , , , , , 8
Depth-first traversal
 Inorder Traversal
 visits the root in between visiting the left and right
subtrees

1. Traverse the left subtree;


2. visit the root;
3. traverse the right subtree.
Depth-first traversal
 Inorder traversal?
A
B D

C E H

F G I
Inorder: , , 8, , , , , , 
Breadth-First Traversal
 a non-recursive traversal.
 visits the nodes in the order of their depth in the
tree
 At each depth the nodes are visited from left to
right
Breadth-First Traversal

A
B D

C E H

F G I

Breadth-first: 8, , , , , , , , 
Expression Trees
 Leaves are operands (variables/constants)
 Other nodes are operators

Expression tree for (a + b * c) + ((d * e + f ) * g)


Constructing Expression Trees
 Read expression one symbol at a time
 If the symbol is an operand, create a one-node tree
and push it onto a stack
 If symbol is an operator, pop two trees,  and 
from the stack ( is popped first), and form a new
tree whose root is the operator and whose left and
right children are  and  , respectively.
 Push new tree onto the stack
Constructing Expression Trees
 Example: ab+cde+**
 Read two operands
Constructing Expression Trees
 Example: ab+cde+**
 + is read, so two trees are popped, a new tree is
formed, and it is pushed onto the stack.
Constructing Expression Trees
 Example: ab+cde+**
 c, d, and e are read, and for each a one-node tree
is created and the corresponding tree is pushed
onto the stack.
Constructing Expression Trees
 Example: ab+cde+**
 + is read, so two trees are merged
Constructing Expression Trees
 Example: ab+cde+**
 * is read, so we pop two trees and form a new tree
with a * as root
Constructing Expression Trees
 Example: ab+cde+**
 Finally

You might also like