8
Duke CPS 100 17. 1 Animal guessing and sorting Inheritance gives animal nodes intelligence internal node asks a question -- always goes left or right don’t know if moving to leaf node or internal node leaf node asks a question -- may add new knowledge to tree must know parent to add new node; why? ugly? does it fly eagle giraffe

Duke CPS 100 17. 1 Animal guessing and sorting l Inheritance gives animal nodes intelligence ä internal node ä asks a question -- always goes left or right

Embed Size (px)

Citation preview

Page 1: Duke CPS 100 17. 1 Animal guessing and sorting l Inheritance gives animal nodes intelligence ä internal node ä asks a question -- always goes left or right

Duke CPS 100 17. 1

Animal guessing and sorting

Inheritance gives animal nodes intelligence internal node

asks a question -- always goes left or right don’t know if moving to leaf node or

internal node

leaf node asks a question -- may add new knowledge

to tree must know parent to add new node; why?

ugly?

does it fly

eagle giraffe

Page 2: Duke CPS 100 17. 1 Animal guessing and sorting l Inheritance gives animal nodes intelligence ä internal node ä asks a question -- always goes left or right

Duke CPS 100 17. 2

Animal Games: two views

Traditional view: nodes store data, intelligence/behavior is in the code that manipulates the nodes

while (! isLeaf(tree)){ cout << tree->info << “[yes/no] “; getline(cin,response); if (tolower(response[0]) == ‘y’) tree = tree->left; else tree = tree->right;}// process leaf node heretree->info = string_user_entered_as_new_question;tree->left = new TreeNode(yesAnswer);tree->right = new TreeNode(noAnswer);

this is acceptable, but doesn’t move us closer to an object-oriented view of the universe, or closer to patterns of programming that are useful in other contexts (well, to some degree it does)

Page 3: Duke CPS 100 17. 1 Animal guessing and sorting l Inheritance gives animal nodes intelligence ä internal node ä asks a question -- always goes left or right

Duke CPS 100 17. 3

Expression Trees

How to evaluate an expression tree? If it’s a leaf? If it’s an internal node?

What kinds of operators are there? unary binary tertiary…

Different degrees of OO-ness exptree.cc exptree2.cc exptree3.cc (optional)

+

*

85

6

If we’re at a leaf node, how can a new subtree be added?

Page 4: Duke CPS 100 17. 1 Animal guessing and sorting l Inheritance gives animal nodes intelligence ä internal node ä asks a question -- always goes left or right

Duke CPS 100 17. 4

Reading information

Recursive file/stream reader read one line from stream if question, build internal node ...

if not question, build leaf node --- what’s needed?

does it fly

giraffe

Page 5: Duke CPS 100 17. 1 Animal guessing and sorting l Inheritance gives animal nodes intelligence ä internal node ä asks a question -- always goes left or right

Duke CPS 100 17. 5

Making nodes

An InternalNode is an AnimalNode, a LeafNode is as wellAnimalNode * tree = new InternalNode(...); does tree have myLeft? what about tree->askQuestion()? who knows how to make node? who has access to

myLeft?

A friend class has access to private information grant friendship sparingly if at all declaration of friendship appears in header file

Factories create objects when part of an inheritance hierarchy makeIterator, makeNode, makeGame, ...

Page 6: Duke CPS 100 17. 1 Animal guessing and sorting l Inheritance gives animal nodes intelligence ä internal node ä asks a question -- always goes left or right

Duke CPS 100 17. 6

Reading/Writing/Deleting trees

Use pre-order traversal to read/write trees uniquely determines tree (compare in-order) create root, then subtrees

To delete a tree, either: ask the tree to delete itself use post-order traversal (why?)

What about reading several files? problems?

Page 7: Duke CPS 100 17. 1 Animal guessing and sorting l Inheritance gives animal nodes intelligence ä internal node ä asks a question -- always goes left or right

Duke CPS 100 17. 7

Sorting

Why do we study sorting? we have to

When are slow sorts better than fast sorts? what does fast mean? how do we measure fast?

Families of sorts: selection, insertion, bubble, quick, merge, tree other sorts: shell, radix, bucket, ...

Page 8: Duke CPS 100 17. 1 Animal guessing and sorting l Inheritance gives animal nodes intelligence ä internal node ä asks a question -- always goes left or right

Duke CPS 100 17. 8

Pseudo-code/Pseudo-sort

selection sort -- issues with pseudo-code? selectSort(List & a)

{

int minPos = findMinIndex(a);

swap(a[minPos],a[0]);

selectSort(a with first element in place);

}

quick sort quickSort(List & a)

{

partition(small numbers to left, large to right);

hopefulMiddle = left/right border;

quickSort(left); quickSort(right);

}