Docsity

Tree Searching Algorithms: Pre, In, Postorder & Conversion to Positional Binary Trees, Study notes of Computer Science

Toggle sidebar
bg1
1
Searching Trees – Page 1CSCI 1900 – Discrete Structures
CSCI 1900
Discrete Structures
Searching Trees
Reading: Kolman,
Section 7.3
Searching Trees – Page 2CSCI 1900 – Discrete Structures
Tree Searching
Trees can represent the organization of
elements, but they can also represent a
process with each vertex specifying a task.
– For-loop example
for i = 1 to 3
for j = 1 to 5
array[i,j] = 10*i + j
next j
next i
– Mathematical expression from section 7.2 – each
vertex represents a computation that combines its
offspring.
Searching Trees – Page 3CSCI 1900 – Discrete Structures
Terminology
• “Visiting” a vertex – the act of performing a
task at a vertex, e.g., perform a computation
or make a decision.
• “Searching” the tree – the process of visiting
each vertex in a specific order or path. The
term “searching” can be misleading. Just
think of it as “traversing” the tree.
Searching Trees – Page 4CSCI 1900 – Discrete Structures
Tree Search
• The application of some trees involves traversing
the tree in a methodical pattern so as to address
every vertex.
• Our book uses the term “search”, but sometimes
search can imply we’re looking for a particular
vertex. This is not the case.
• Example:
Assume we want to compute the average age,
maximum age, and minimum age of all of the
children from five families. (Tree is on next slide.)
Searching Trees – Page 5CSCI 1900 – Discrete Structures
Search Example
Neighborhood
families
A. Jones Halls Smith Taylor B. Jones
Katy
age 3 Tommy
age 5
Phil
age 8
Taylor
age 1 Lori
age 4
Lexi
age 2
Karen
age 14
Bart
age 12
Mike
age 6
Ben
age 2
Searching Trees – Page 6CSCI 1900 – Discrete Structures
Search Example (continued)
• To calculate the average, max, and min ages
for all of the children, we need to have a
method for going through the tree so that we
don’t miss a child.
• By defining a rigorous process, not only can a
human be sure not to miss a vertex, but also
an algorithm can be defined for a computer
pf3
pf4
pf5

Partial preview of the text

Download Tree Searching Algorithms: Pre, In, Postorder & Conversion to Positional Binary Trees and more Study notes Computer Science in PDF only on Docsity!

CSCI 1900 – Discrete Structures Searching Trees – Page 1

CSCI 1900

Discrete Structures

Searching Trees

Reading: Kolman,

Section 7.

CSCI 1900 – Discrete Structures Searching Trees – Page 2

Tree Searching

Trees can represent the organization of

elements, but they can also represent a

process with each vertex specifying a task.

– For-loop example

for i = 1 to 3 for j = 1 to 5 array[i,j] = 10*i + j next j next i

– Mathematical expression from section 7.2 – each

vertex represents a computation that combines its

offspring.

CSCI 1900 – Discrete Structures Searching Trees – Page 3

Terminology

• “Visiting” a vertex – the act of performing a

task at a vertex, e.g., perform a computation

or make a decision.

• “Searching” the tree – the process of visiting

each vertex in a specific order or path. The

term “searching” can be misleading. Just

think of it as “traversing” the tree.

CSCI 1900 – Discrete Structures Searching Trees – Page 4

Tree Search

• The application of some trees involves traversing

the tree in a methodical pattern so as to address

every vertex.

• Our book uses the term “search”, but sometimes

search can imply we’re looking for a particular

vertex. This is not the case.

• Example:

Assume we want to compute the average age,

maximum age, and minimum age of all of the

children from five families. (Tree is on next slide.)

CSCI 1900 – Discrete Structures Searching Trees – Page 5

Search Example

Neighborhood families

A. Jones Halls Smith Taylor B. Jones

Katy age 3

Tommy age 5

Phil age 8

Taylor age 1

Lori age 4

Lexi age 2

Karen age 14

Bart age 12

Mike age 6

Ben age 2

CSCI 1900 – Discrete Structures Searching Trees – Page 6

Search Example (continued)

• To calculate the average, max, and min ages

for all of the children, we need to have a

method for going through the tree so that we

don’t miss a child.

• By defining a rigorous process, not only can a

human be sure not to miss a vertex, but also

an algorithm can be defined for a computer

CSCI 1900 – Discrete Structures Searching Trees – Page 7

A Suggested Process for

Searching a Tree

1. Starting at the root, repeatedly take the leftmost

“untraveled” edge until you arrive at a leaf which

should be a child.

2. Include this child in the average, max, and min

calculations.

3. One at a time, go back up the edges until you

reach a vertex that hasn’t had all of its outgoing

edges traveled.

4. If you get back to the root and cannot find an

untraveled edge, you are done. Otherwise,

return to step 1.

CSCI 1900 – Discrete Structures Searching Trees – Page 8

Vertices Numbered

in Order of Visits

Neighborhood families

A. Jones Halls Smith Taylor B. Jones

Katy age 3

Tommy age 5

Phil age 8

Taylor age 1

Lori age 4

Lexi age 2

Karen age 14

Bart age 12

Mike age 6

Ben age 2

CSCI 1900 – Discrete Structures Searching Trees – Page 9

Preorder Search

• This methodical pattern of traversing a tree is called

a preorder search.

• Assume v is the root of a binary positional tree T.

  • Each vertex of this tree has at most a left vertex, v L , and a

right vertex, v R.

  • If either v L or v R have offspring, then they are subtrees of T,

and a search can be performed of them too.

  • By viewing a tree this way, then the search method we

described in earlier slides can be performed using a

recursive algorithm applied to each vertex.

  • The recursive algorithm is repeatedly applied until every

leaf has been reached.

CSCI 1900 – Discrete Structures Searching Trees – Page 10

Preorder Search Algorithm

• A preorder search of a tree has the

following three steps:

1. Visit the root

2. Search the left subtree if it exists

3. Search the right subtree if it exists

• The term “search” in steps 2 and 3

implies that we apply all three steps to

the subtree beginning with step 1.

CSCI 1900 – Discrete Structures Searching Trees – Page 11

Vertices Visited in Alphabetical

Order Using Preorder Search

A

B

C

D F G J L

E I K

H

CSCI 1900 – Discrete Structures Searching Trees – Page 12

Prefix or Polish Form

x

a

d e

b c ÷

Preorder search produces: × – a b + c ÷ d e

Binary tree representing: ( a – b ) × ( c + ( d ÷ e ))

CSCI 1900 – Discrete Structures Searching Trees – Page 19

Evaluation of Tree Using

Postorder Search

A

B

C

D F G J L

E I K

H

Resulting string: DCFGEBJILKHA

CSCI 1900 – Discrete Structures Searching Trees – Page 20

Infix Form

x

a

d e

b c ÷

Inorder search produces: a – b × c + d ÷ e

Unfortunately, without parenthesis, we can’t

do anything with this expression.

Binary tree representing: ( a – b ) × ( c + ( d ÷ e ))

CSCI 1900 – Discrete Structures Searching Trees – Page 21

Postfix or Reverse Polish Form

x

a

d e

b c ÷

Inorder search produces: a b – c d e ÷ + ×

Binary tree representing: ( a – b ) × ( c + ( d ÷ e ))

CSCI 1900 – Discrete Structures Searching Trees – Page 22

Reverse Polish Form (continued)

• Allows us to write complex arithmetic

expressions without using parenthesis

• Expression is evaluated by performing

following steps:

– Move left to right until you find a string of the

form xyF , where F is the symbol for a binary

operation and x and y are numbers.

– Evaluate x F y and substitute answer for string

xyF.

– Repeat starting at beginning of string again.

CSCI 1900 – Discrete Structures Searching Trees – Page 23

Reverse Polish Form Example

From left-to-right evaluate xyF first.

1. 2 1 – 3 4 2 ÷ + × 1 st^ pattern: 2 1 –

2. 1 3 4 2 ÷ + × 2 nd^ pattern: 4 2 ÷

3. 1 3 2 + × 3 rd^ pattern: 3 2 +

4. 1 5 × 4 th^ pattern: 1 5 ×

(2 – 1) × (3 + (4 ÷ 2)) = 5

CSCI 1900 – Discrete Structures Searching Trees – Page 24

Converting an Ordered

n-tree to a Positional Binary Tree

• An ordered n-tree where some vertices

have more than two offspring can be

converted to a positional binary tree.

• This allows easier computer

representation with methods such as

linked lists.

• A process exists for this conversion that

works on any finite tree.

CSCI 1900 – Discrete Structures Searching Trees – Page 25

Process to Convert Ordered n-tree

to Positional Binary Tree

• Starting at the root, the first or

leftmost offspring of a vertex

remains the leftmost vertex in

the binary tree

• The first sibling to the right

of the leftmost vertex

becomes the right offspring

of the leftmost vertex

• Subsequent siblings

become the right offspring

in succession until last

sibling is converted.

A

B C D E

A

B C

D

E

CSCI 1900 – Discrete Structures Searching Trees – Page 26

Conversion Example

A

B C^ D

E F^ G H^ I

J K L

CSCI 1900 – Discrete Structures Searching Trees – Page 27

Conversion Example

A

B C^ D

E F^ G H^ I

J K L

A

B

C

D

E

F G

H

I

J

K

L

Preorder search:

Left tree – ABEFCGJKLHID

Right tree – ABEFCGJKLHID