Docsity

Data Structures and Algorithms: CSCE 350 Announcement and Binary Tree Traversals - Prof. C, Study notes of Computer Science

Toggle sidebar
bg1
1
CSCE 350:
Data Structures and Algorithms
Chin-Tser Huang
University of South Carolina
02/23/2009 2
Announcement
Reading assignment: Chapter 4.4, 4.5
Homework 3 is assigned on February 16 and
is due on Wednesday, February 25 in class
Please download it from class website
Have your answers neatly typed
Explain your answer clearly and adequately by
showing the steps
Draft lecture slides will be available on class
website one hour before class
02/23/2009 3
Binary Tree
A binary tree
T
is defined as a finite set of nodes
that is either empty or consists of a root and two
disjoint binary trees
T
L
and
T
R
called the left and
right subtree of the root
TT
LR
02/23/2009 4
Binary Tree Traversals
For analysis convenience, we can extend binary trees by
replacing empty subtrees by special nodes
Internal and external nodes:
#external_node = #internal_node+1
: internal
: external
02/23/2009 5
Binary Tree Algorithm 1:
Height of a binary tree
Input size
n
(
T
): # nodes in
T
, basic operation: “+”
Recurrence
Æ
A
(
n
)=
n
why?
ÆIf the basic operation is the line to check whether a tree
is empty Æ
C
(
n
)=2
n
+1 why?
1)}(),(max{ return else
1 return if
)(ALGORITHM
+
−=
RL THeightTHeight
T
THeight
φ
0)0(
0)(,1))(())(())((
=
>++=
A
TnTnATnATnA rL for
02/23/2009 6
Binary Tree Algorithm 2:
Traverse the binary tree
List all the nodes
Preorder traversal: root Æleft subtree Æright subtree
Inorder traversal: left subtree Æroot Æright subtree
Postorder traversal: left subtree Æright subtree Æroot
What is the efficiency?
)(
)(
output
if
)(ALGORITHM
R
L
root
Tpreorder
Tpreorder
T
T
Tpreorder
φ
≠
pf3
pf4

Partial preview of the text

Download Data Structures and Algorithms: CSCE 350 Announcement and Binary Tree Traversals - Prof. C and more Study notes Computer Science in PDF only on Docsity!

CSCE 350:

Data Structures and Algorithms

Chin-Tser Huang

[email protected]

University of South Carolina

02/23/2009 2

Announcement

„ Reading assignment: Chapter 4.4, 4.

„ Homework 3 is assigned on February 16 and

is due on Wednesday, February 25 in class

„ Please download it from class website

„ Have your answers neatly typed

„ Explain your answer clearly and adequately by

showing the steps

„ Draft lecture slides will be available on class

website one hour before class

02/23/2009 3

Binary Tree

„ A binary treeT is defined as a finite set of nodes

that is either empty or consists of a root and two

disjoint binary treesT

L

andT

R

called the left and

right subtree of the root

T L (^) T R

02/23/2009 4

Binary Tree Traversals

„ For analysis convenience, we can extend binary trees by

replacing empty subtrees by special nodes

„ Internal and external nodes:

#external_node = #internal_node+

: internal

: external

02/23/2009 5

Binary Tree Algorithm 1:

Height of a binary tree

„ Input sizen(T): # nodes inT, basic operation: “+”

„ Recurrence

Æ A(n)=n why?

Æ If the basic operation is the line to check whether a tree

is empty Æ C(n)=2n+1 why?

elsereturnmax{ ( ), ( )} 1

if return 1

ALGORITHM ()

L R HeightT HeightT

T

HeightT

φ

A

A nT AnT AnT nT L r for

02/23/2009 6

Binary Tree Algorithm 2:

Traverse the binary tree

„ List all the nodes

„ Preorder traversal : root Æ left subtree Æ right subtree

„ Inorder traversal : left subtree Æ root Æ right subtree

„ Postorder traversal : left subtree Æ right subtree Æ root

„ What is the efficiency?

output

if

ALGORITHM ()

R

L

root

preorderT

preorderT

T

T

preorderT

≠ φ

02/23/2009 7

Binary Tree Traversal Example

Preorder:

Inorder:

Postorder:

02/23/2009 8

Large Integer Multiplication

„ Some applications, notably modern cryptology, require

manipulation of integers over 100 decimal digits long

„ Such integers are too long to fit a single word of a

computer; therefore, they require special treatment

„ Consider the multiplication of two such long integers

„ If we use the classic pen-and-pencil algorithm to

multiply twon-digit integers, we needn

2 digit

multiplications

„ Can we design a divide-and-conquer algorithm to solve

this problem with better efficiency?

02/23/2009 9

The Basic Idea

„ We want to calculate 23 x 14

„ Since

„ We have

„ Which includes four digit multiplications (n

2 )

„ But

„ Therefore, we only need three digit multiplications

1 0 1 0 23 = 2 ⋅ 10 + 3 ⋅ 10 and 14 = 1 ⋅ 10 + 4 ⋅ 10

2 1 0

1 0 1 0

02/23/2009 10

One Formula

„ Givena=a

1

a

0

andb=b

1

b

0

, computec=a *b

„ We have

„ That means only three digit multiplications are needed

to multiply two 2-digit integers

1 1 0 1 0 2 0

0 0 0

2 1 1

0 0

1 1

2 2

c a a b b c c

c a b

c a b

c ab c c c

where

02/23/2009 11

To Multiply Twon-digit Integers

„ Assumen is even, write

„ Then

„ To calculate the involved three multiplications –

recursion! Stops whenn=

0

/ 2 0 1

/ 2 1 a a 10 a b b 10 b

n n = + and = +

1 1 0 1 0 2 0

0 0 0

2 1 1

0 0

/ 2 2 1

c a a b b c c

c a b

c a b

c ab c c c

n n

where

02/23/2009 12

Efficiency

„ The recurrence relation is

„ Solving it by backward substitution forn=

k yields

„ Therefore,

T ( n )= 3 T ( n / 2 ) for n > 1 , T ( 1 )= 1

k kk k

k k k

T

T T T

1 2 2

−

− −

log 2 log 23 1. 585 2

T ( n ) 3 n n n

n

02/23/2009 19

Closest Pair by Divide-and-

Conquer (cont.)

Step 2 Find recursively the closest pairs for the left and right

subsets.

Step 3 Setd = min{d 1 ,d 2 }

We can limit our attention to the points in the symmetric vertical strip of width 2d as possible closest pair. Let C 1 and C 2 be the subsets of points in the left subset S 1 and of the right subset S 2 , respectively, that lie in this vertical strip. The points in C 1 and C 2 are stored in increasing order of theiry coordinates, which is maintained by merging during the execution of the next step.

Step 4 For every pointP(x,y) in C 1 , we inspect points in

C 2 that may be closer toP thand. There can be no more than 6 such points (becaused ≤ d 2 )!

02/23/2009 20

Closest Pair by Divide-and-

Conquer: Worst Case

The worst case scenario is depicted

below:

02/23/2009 21

Efficiency of Closest-Pair Algorithm

Running time of the algorithm is described by

T(n) = 2T(n/2) + M(n), where M(n) ∈ O(n)

By the Master Theorem (witha = 2,b = 2,d = 1)

T(n) ∈ O(n logn)