Download presentation
Presentation is loading. Please wait.
Published byBernadette Armstrong Modified over 9 years ago
2
1 Lecture 11 POLYNOMIALS and Tree sort
3
2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort
4
3
5
4 INTRODUCTION The problems examined in this lecture are polynomial evaluation (with and without preprocessing of the coefficients), polynomial multiplication, and multiplication of matrices and vectors. 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, Strassen’s matrix multiplication algorithm. Several algorithms in this lecture use the divide-and-conquer method: evaluating a polynomial with preprocessing of coefficients, Strassen’s matrix multiplication algorithm.
6
5 EVALUATING POLYNOMIAL FUNCTIONS Consider the polynomial: P(x) = a n x^ n + a n-1 x^ n-1 + … + a 1 x +a 0 with real coefficients and n>=1 Suppose the coefficients of a 0, a 1, …, a n 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.
7
6
8
7
9
8
10
Synthetic Division is a process whereby the quotient and remainder can be determined when a polynomial function f is divided by g(x) = x - c.
11
Use synthetic division to find the quotient and remainder when
16
f(-3) = 278
17
16 Algorithms 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 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
18
17 Algorithm Polynomial Evaluation—Term by Term Input: The coefficients of polynomial p(x) in the array a; n>=0, the degree of p; and x, the point at which to evaluate p. Input: The coefficients of polynomial p(x) in the array a; n>=0, the degree of p; and x, the point at which to evaluate p. Output: The value of p(x). Output: The value of p(x). float poly(float[] a, int n, float x) float p, xpower; int i; p = a[0]; xpower = 1; for (i = 1; i <= n; i++) for (i = 1; i <= n; i++) xpower = xpower * x; p+=a[i] * xpower; return p; Note: This algorithm does 2n multiplications and n additions
19
18 Horner’s method The key to Horner’s method for evaluating p(x) is simply a particular factorization of p: The key to Horner’s method for evaluating p(x) is simply a particular factorization of p: p(x) = (…((a n x = a n-1 )x + a n-2 )x+…+a 1 )x + a 0. The computation is done in a short loop with only n multiplications and n additions. The computation is done in a short loop with only n multiplications and n additions.
20
19
21
20 Algorithm Polynomial Evaluation – Horner’s Method Input: a, n, and x as in Algorithm 12.1. Input: a, n, and x as in Algorithm 12.1. Output: The value of p(x). Output: The value of p(x). float hornerPoly(float[]a, int n, float x) float p; float p; int i; int i; p = a[n]; p = a[n]; for (i = n – 1; i>= 0; i--) for (i = n – 1; i>= 0; i--) p = p * x + a[i]; p = p * x + a[i]; return p; return p; Thus simply by factoring p we have cut the number of multiplications in half without increasing the number of additions. Thus simply by factoring p we have cut the number of multiplications in half without increasing the number of additions.
22
21
23
22
24
23
25
24
26
25
27
26
28
27 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. 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. Binary Tree
29
28 A Binary Tree Q V T K S A E L
30
29 How many leaf nodes? Q V T K S A E L
31
30 How many descendants of Q? Q V T K S A E L
32
31 How many ancestors of K? Q V T K S A E L
33
32 Implementing a Binary Tree with Pointers and Dynamic Data Q V T K S A E L
34
33 Each node contains two pointers template struct TreeNode { ItemType info; // Data member TreeNode * left; // Pointer to left child TreeNode * right; // Pointer to right child };. left. info. right NULL ‘A’ 6000
35
34 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. A Binary Search Tree (BST) is...
36
35 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. Shape of a binary search tree... ‘J’
37
36 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. Inserting ‘ E ’ into the BST ‘J’ ‘E’
38
37 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. Inserting ‘ F ’ into the BST ‘J’ ‘E’ ‘F’
39
38 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. Inserting ‘ T ’ into the BST ‘J’ ‘E’ ‘F’ ‘T’
40
39 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. Inserting ‘ A ’ into the BST ‘J’ ‘E’ ‘F’ ‘T’ ‘A’
41
40 is obtained by inserting the elements ‘ A ’ ‘ E ’ ‘ F ’ ‘ J ’ ‘ T ’ in that order? the elements ‘ A ’ ‘ E ’ ‘ F ’ ‘ J ’ ‘ T ’ in that order? What binary search tree... ‘A’
42
41 obtained by inserting the elements ‘ A ’ ‘ E ’ ‘ F ’ ‘ J ’ ‘ T ’ in that order. the elements ‘ A ’ ‘ E ’ ‘ F ’ ‘ J ’ ‘ T ’ in that order. Binary search tree... ‘A’ ‘E’ ‘F’ ‘J’ ‘T’
43
42 Another binary search tree Add nodes containing these values in this order: ‘D’ ‘B’ ‘L’ ‘Q’ ‘S’ ‘V’ ‘Z’ ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’ ‘K’ ‘P’
44
43 Is ‘ F ’ in the binary search tree? ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’ ‘K’ ‘V’ ‘P’ ‘Z’‘D’‘Q’‘L’‘B’‘S’
45
44 Inorder Traversal: A E H J M T Y ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’‘Y’ tree Print left subtree firstPrint right subtree last Print second
46
45 Preorder Traversal: J E A H T M Y ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’‘Y’ tree Print left subtree secondPrint right subtree last Print first
47
46 ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’‘Y’ tree Print left subtree firstPrint right subtree second Print last Postorder Traversal: A H E M Y T J
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.