Download presentation
Presentation is loading. Please wait.
Published byDorthy Black Modified over 9 years ago
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 ä 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 eaglegiraffe
2
Duke CPS 100 17. 2 Animal Games: two views l Traditional view: nodes store data, intelligence/behavior is in the code that manipulates the nodes while (! isLeaf(tree)) { cout info << “[yes/no] “; getline(cin,response); if (tolower(response[0]) == ‘y’) tree = tree->left; else tree = tree->right; } // process leaf node here tree->info = string_user_entered_as_new_question; tree->left = new TreeNode(yesAnswer); tree->right = new TreeNode(noAnswer); l 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)
3
Duke CPS 100 17. 3 Expression Trees l How to evaluate an expression tree? ä If it’s a leaf? ä If it’s an internal node? l What kinds of operators are there? ä unary ä binary ä tertiary… l Different degrees of OO-ness ä exptree.cc ä exptree2.cc ä exptree3.cc (optional) + * 856 If we’re at a leaf node, how can a new subtree be added?
4
Duke CPS 100 17. 4 Reading information l 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
5
Duke CPS 100 17. 5 Making nodes l An InternalNode is an AnimalNode, a LeafNode is as well AnimalNode * tree = new InternalNode(...); does tree have myLeft ? what about tree->askQuestion() ? ä who knows how to make node? who has access to myLeft? l A friend class has access to private information ä grant friendship sparingly if at all ä declaration of friendship appears in header file l Factories create objects when part of an inheritance hierarchy ä makeIterator, makeNode, makeGame,...
6
Duke CPS 100 17. 6 Reading/Writing/Deleting trees l Use pre-order traversal to read/write trees ä uniquely determines tree (compare in-order) ä create root, then subtrees l To delete a tree, either: ä ask the tree to delete itself ä use post-order traversal (why?) l What about reading several files? problems?
7
Duke CPS 100 17. 7 Sorting l Why do we study sorting? ä we have to l When are slow sorts better than fast sorts? ä what does fast mean? ä how do we measure fast? l Families of sorts: ä selection, insertion, bubble, ä quick, merge, tree ä other sorts: shell, radix, bucket,...
8
Duke CPS 100 17. 8 Pseudo-code/Pseudo-sort l 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); } l quick sort quickSort(List & a) { partition(small numbers to left, large to right); hopefulMiddle = left/right border; quickSort(left); quickSort(right); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.