Title: POLYNOMIALS and Tree sort
1Lecture 11
- POLYNOMIALS and Tree sort
-
Prof. Sin-Min Lee Department of Computer Science
2 Topics include
- INTRODUCTION
- EVALUATING POLYNOMIAL FUNCTIONS
- Horners method
- Permutation
- Tree sort
3(No Transcript)
4INTRODUCTION
- The problems examined in this lecture are
polynomial evaluation (with and without
preprocessing of the coefficients), polynomial
multiplication, and multiplication of matrices
and vectors. - Several algorithms in this lecture use the
divide-and-conquer method evaluating a
polynomial with preprocessing of coefficients,
Strassens matrix multiplication algorithm.
5EVALUATING POLYNOMIAL FUNCTIONS
- Consider the polynomial
- P(x) anxn an-1xn-1 a1x a0
- with real coefficients and ngt1
- Suppose the coefficients of a0, a1, , an and x
are given and that the problem is the evaluate
p(x). - In this section we look at some algorithms and
some lower bounds for this problem.
6(No Transcript)
7(No Transcript)
8(No Transcript)
9Synthetic Division is a process whereby the
quotient and remainder can be determined when a
polynomial function f is divided by g(x) x - c.
10Use synthetic division to find the quotient and
remainder when
11(No Transcript)
12(No Transcript)
13(No Transcript)
14(No Transcript)
15f(-3) 278
16 Algorithms
- The obvious way to solve the problem is to
compute each term and add it to the sum of the
others already computed. The following algorithm
does this
17Algorithm Polynomial EvaluationTerm by Term
- Input The coefficients of polynomial p(x) in
the array a ngt0, the degree of p and x, the
point at which to evaluate p. - Output The value of p(x).
- float poly(float a, int n, float x)
- float p, xpower
- int i
- p a0 xpower 1
- for (i 1 i lt n i)
- xpower xpower x
- pai xpower
- return p
- Note This algorithm does 2n multiplications and
n additions
18Horners method
- The key to Horners method for evaluating p(x) is
simply a particular factorization of p - p(x) (((anx an-1)x an-2)xa1)x a0.
- The computation is done in a short loop with only
n multiplications and n additions.
19(No Transcript)
20Algorithm Polynomial Evaluation Horners Method
- Input a, n, and x as in Algorithm 12.1.
- Output The value of p(x).
- float hornerPoly(floata, int n, float x)
- float p
- int i
- p an
- for (i n 1 igt 0 i--)
- p p x ai
- return p
- Thus simply by factoring p we have cut the
number of multiplications in half without
increasing the number of additions.
21(No Transcript)
22(No Transcript)
23(No Transcript)
24(No Transcript)
25(No Transcript)
26(No Transcript)
27Binary Tree
- A binary tree is a structure in which
- Each node can have at most two children, and
in which a unique path exists from the root to
every other node. - The two children of a node are called the left
child and the right child, if they exist.
28A Binary Tree
V
Q
L
T
A
E
K
S
29How many leaf nodes?
V
Q
L
T
A
E
K
S
30How many descendants of Q?
V
Q
L
T
A
E
K
S
31How many ancestors of K?
32Implementing a Binary Tree with Pointers and
Dynamic Data
V
Q
L
T
A
E
K
S
33Each node contains two pointers
templatelt class ItemType gt struct TreeNode
ItemType info // Data member
TreeNodeltItemTypegt left // Pointer to
left child TreeNodeltItemTypegt right //
Pointer to right child
NULL A 6000
. left . info . right
34A Binary Search Tree (BST) is . . .
- A special kind of binary tree in which
- 1. Each node contains a distinct data value,
- 2. The key values in the tree can be compared
using greater than and less than, and - 3. The key value of each node in the tree is
- less than every key value in its right subtree,
and greater than every key value in its left
subtree.
35Shape of a binary search tree . . .
- Depends on its key values and their order of
insertion. - Insert the elements J E F T A
in that order. - The first value to be inserted is put into the
root node.
36Inserting E into the BST
- Thereafter, each value to be inserted begins by
comparing itself to the value in the root node,
moving left it is less, or moving right if it is
greater. This continues at each level until it
can be inserted as a new leaf.
37Inserting F into the BST
- Begin by comparing F to the value in the root
node, moving left it is less, or moving right if
it is greater. This continues until it can be
inserted as a leaf.
38Inserting T into the BST
- Begin by comparing T to the value in the root
node, moving left it is less, or moving right if
it is greater. This continues until it can be
inserted as a leaf.
39Inserting A into the BST
- Begin by comparing A to the value in the root
node, moving left it is less, or moving right if
it is greater. This continues until it can be
inserted as a leaf.
40What binary search tree . . .
- is obtained by inserting
- the elements A E F J T in
that order?
41Binary search tree . . .
- obtained by inserting
- the elements A E F J T in
that order.
42Another binary search tree
T
E
A
H
M
P
K
Add nodes containing these values in this
order D B L Q S
V Z
43Is F in the binary search tree?
J
T
E
A
V
M
H
P
44Inorder Traversal A E H J M T Y
Print second
tree
T
E
A
H
M
Y
Print left subtree first
Print right subtree last
45Preorder Traversal J E A H T M Y
Print first
tree
T
E
A
H
M
Y
Print left subtree second
Print right subtree last
46Postorder Traversal A H E M Y T J
Print last
tree
T
E
A
H
M
Y
Print left subtree first
Print right subtree second