Math Review - Proof By Induction - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Math Review - Proof By Induction

Description:

Math Review Proof By Induction – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 29
Provided by: william746
Category:
Tags: com | hotel | induction | math | proof | review

less

Transcript and Presenter's Notes

Title: Math Review - Proof By Induction


1
The Tree ADT
A tree is a collection of nodes, one of which is
identified as the root. Each non-root node is
connected to some other node (perhaps the root)
by an edge, making the non-root node the child of
the other node, the parent.
2
Linked List Implementation of the Tree ADT
struct treeNode Etype element treeNode
firstChild treeNode nextSibling
3
Tree Traversals
To examine the contents of a tree, a traversal
strategy must be selected. There are three
principal options Option 1 Preorder
Traversal First examine the current node, then
examine its offspring.
Preorder Traversal Text Chapter 1
Chapter 2 Section 2.1 Section
2.2 Section 2.3 Chapter 3
Section 3.1 Subsection 3.1.a
Subsection 3.1.b
Subsection 3.1.c Section 3.2
Chapter 4 Section 4.1 Section
4.2 Section 4.3 Chapter 5
4
Option 2 Postorder Traversal First examine the
offspring, then examine the current node.
Postorder Traversal Flight 150 Cab
25 Travel 175 Dinner 20 Food
20 Hotel 110 Lodging 110 DAY
ONE 305 Rental 45 Travel
45 Breakfast 10 Lunch 15 Dinner
35 Food 60 Hotel 110 Lodging 110 DAY
TWO 215 Rental 45 Flight 175 Travel
220 Breakfast 10 Lunch 15 Food
25 Hotel 45 Lodging 45 DAY
THREE 280 Total 800
5
Option 3 Inorder Traversal (restricted to binary
trees) Examine the left subtree, then the root,
and then the right subtree.
Inorder Traversal 11 18 23 25 29 31
40 48 57 61 64 76 89 92 93 95
6
Binary Trees
A binary tree is a tree in which no node has more
than two children. Implementation The limitation
of at most two children per node makes it
possible to implement each node with direct
pointers to its children. struct
treeNode Etype element treeNode
left treeNode right
7
Application Expression Trees
By placing the operands in the leaf nodes and the
binary (and unary) operators in the non-leaf
nodes, we can conveniently store and evaluate
arithmetic expressions.
Inorder traversal (modified to parenthesize each
non-trivial subtree) ((-5)(13(20/4)))-(17(2(5
6)))
Note that a postorder traversal produces a
postfix expression, which can be easily evaluated
via the stack operations we saw earlier.
8
Binary Search Trees
  • A convenient means of searching (or sorting) a
    list, the binary search tree uses a simple
    insertion policy
  • Starting at the root, if the new element is
    smaller than the current nodes value, go left
    otherwise, go right.
  • Insert the new element when a NULL pointer is
    reached.
  • EXAMPLE 165 213 104 122 256 240
    173

165
9
The Tree ADT
A tree is a collection of nodes, one of which is
identified as the root. Each non-root node is
connected to some other node (perhaps the root)
by an edge, making the non-root node the child of
the other node, the parent.
10
Linked List Implementation of the Tree ADT
struct treeNode Etype element treeNode
firstChild treeNode nextSibling
11
Tree Traversals
To examine the contents of a tree, a traversal
strategy must be selected. There are three
principal options Option 1 Preorder
Traversal First examine the current node, then
examine its offspring.
Preorder Traversal Text Chapter 1
Chapter 2 Section 2.1 Section
2.2 Section 2.3 Chapter 3
Section 3.1 Subsection 3.1.a
Subsection 3.1.b
Subsection 3.1.c Section 3.2
Chapter 4 Section 4.1 Section
4.2 Section 4.3 Chapter 5
12
Option 2 Postorder Traversal First examine the
offspring, then examine the current node.
Postorder Traversal Flight 150 Cab
25 Travel 175 Dinner 20 Food
20 Hotel 110 Lodging 110 DAY
ONE 305 Rental 45 Travel
45 Breakfast 10 Lunch 15 Dinner
35 Food 60 Hotel 110 Lodging 110 DAY
TWO 215 Rental 45 Flight 175 Travel
220 Breakfast 10 Lunch 15 Food
25 Hotel 45 Lodging 45 DAY
THREE 280 Total 800
13
Option 3 Inorder Traversal (restricted to binary
trees) Examine the left subtree, then the root,
and then the right subtree.
Inorder Traversal 11 18 23 25 29 31
40 48 57 61 64 76 89 92 93 95
14
Binary Trees
A binary tree is a tree in which no node has more
than two children. Implementation The limitation
of at most two children per node makes it
possible to implement each node with direct
pointers to its children. struct
treeNode Etype element treeNode
left treeNode right
15
Application Expression Trees
By placing the operands in the leaf nodes and the
binary (and unary) operators in the non-leaf
nodes, we can conveniently store and evaluate
arithmetic expressions.
Inorder traversal (modified to parenthesize each
non-trivial subtree) ((-5)(13(20/4)))-(17(2(5
6)))
Note that a postorder traversal produces a
postfix expression, which can be easily evaluated
via the stack operations we saw earlier.
16
Binary Search Trees
  • A convenient means of searching (or sorting) a
    list, the binary search tree uses a simple
    insertion policy
  • Starting at the root, if the new element is
    smaller than the current nodes value, go left
    otherwise, go right.
  • Insert the new element when a NULL pointer is
    reached.
  • EXAMPLE 165 213 104 122 256 240
    173

165
17
Removal From A Binary Search Tree
Removing a node with no children Set parents
appropriate pointer to NULL.
Removing a node with one child Set parents
appropriate pointer to nodes child.
Removing a node with two children Replace the
nodes value with the smallest value in its right
subtree, and then recursively remove that value
from the right subtree.
18
Array Implementation Of Binary Tree
  • Place root in slot 0
  • Place left child of slot k's node in slot
    (2k1)
  • Place right child of slot k's node in slot
    (2k2)
  • Locate parent of slot k's node in slot ((k-1)/2)

19
An Application Using The Array Implementation
A heap structure makes sure that the data in
each node is greater than or equal to the data in
both of its subtrees. It is used to ensure that
the largest elements are always most accessible
(i.e., nearest to the root).
20
AVL Trees
Although an average search in an n-node binary
search tree is O(logn), the worst case could be
as bad as O(n). An AVL (Adelson-Velskii and
Landis) tree places a balance condition on a
binary search tree by requiring the left and
right subtrees of each node to have heights
differing by at most one. During insertion, this
is accomplished by means of single and double
rotations. Single rotations Note that in each
of the trees illustrated below (any
element of X) ? k1 ? (any element of Y) ? k2 ?
(any element of Z)
So when a new elements insertion causes an
imbalance, rotate the tree to restore the
balance.
21
Single Rotation Examples
22
Double rotations If a single rotation doesnt
restore balance, a double rotation will. Note
that in the two trees illustrated below (any
value in A) ? k1 ? (any value in B) ? k2 ? (any
value in C) ? k3 ? (any value in D)
Also note that in the two trees illustrated
below (any value in E) ? k1 ? (any value in F)
? k2 ? (any value in G) ? k3 ? (any value in H)
If single rotation fails to restore balance after
a new insertion, double rotation may be tried.
23
Double Rotation Example
STILL UNBALANCED
BALANCED!
24
Splay Trees
Rather than guaranteeing O(logn) time for every
access within a binary search tree, we might try
obtaining an amortized running time of O(logn)
(i.e., m consecutive operations will take a total
time of O(mlogn)). Splay trees accomplish this by
adjusting the trees balance with every access,
via an AVL-type single rotation, an AVL-type
double rotation (called a zig-zag), or a new type
of double rotation (called a zig-zig).
25
Splay Tree Example
26
Splay Tree Example (Continued)
27
B-Trees
  • A B-tree of order m is a tree with the following
    properties
  • The root is either a leaf or has between 2 and m
    children.
  • All non-leaf nodes (except the root) have between
    ?m/2? and m children.
  • All leaf nodes have the same depth.
  • Example A 2-3 Tree

10,20,30
28
2-3 Tree Example (continued)
Write a Comment
User Comments (0)
About PowerShow.com