decision making based on state of an object (polymorphism) • Inheritance is well-understood for binary search trees, AVL trees, and heaps, although the
View & Download This PDF
Second approach to implement BST • What do we mean by polymorphic? • Implement two subtypes of Tree • EmptyTree • NonEmptyTree • Use EmptyTree to
Trees Binary Search Trees Trees • Terminology – Root ⇒ node with no parent – Leaf ⇒ all nodes with no Choice #2: Using a Polymorphic Binary Tree
A generic binary search tree II let rec find_max = function Empty -> assert false Node (_, x, Empty) -> x Node (_, x, r) -> find_max r;; # val find_max : 'a bst
Let's implement a BST class, avoiding iteration This will give us more practice with trees, and with recursion It will also give us a chance for a continued
The left and right subtrees are also binary search trees Give in Coq a ( polymorphic) definition of the predicate “being a binary search tree with respect to the
Node find ( Value data3 ) { } } Polymorphic Binary Tree Implement Interface Node { Node insert ( Value data1 ) { } } Class EmptyNode implements Node
Polymorphic functions shine when used with polymorphic data types In: Binary search trees are binary trees with elements stored at the interior nodes, such
Trees Binary Search Trees Department Using a Polymorphic Binary Tree Binary Search Trees Examples Binary search trees Non-binary search tree 5
Expands on the binary search technique and A binary search tree is a binary tree in which every node's left use structural recursion and polymorphism
decision making based on state of an object (polymorphism) • Inheritance is well-understood for binary search trees, AVL trees, and heaps, although the
[PDF] polymorphic binary search tree java
[PDF] polymorphism in java example
[PDF] polymorphism in java example javatpoint
[PDF] polymorphism java example stackoverflow
[PDF] polynesie 2016 maths es
[PDF] polynésie 2016 maths es corrigé
[PDF] polynésie juin 2016 maths corrigé es
[PDF] polynesie juin 2016 maths s
[PDF] polynôme caractéristique
[PDF] polynome et fraction rationnelle cours
[PDF] polynomial lens distortion model
[PDF] polynomial solution
[PDF] polynomials and conjugate roots
[PDF] polynomials class 9 worksheet pdf
[PDF] polyphemus pronunciation
CS310Inheritance,Page1' OOProgramming variables thedata
CS310Inheritance,Page2' AnExampleofUsingInheritance binarysearchtrees,AVLtrees,andheaps. as,traversals,insertion,anddeletion. classes?
CS310Inheritance,Page3' SeveralObservations thesetreetypes. preordertraversal).
CS310Inheritance,Page4' Inheritance template
template classBinary_tree{ public: Binary_tree();~Binary_tree(); voidinorder(); voidpreorder(); voidpostorder(); boolsearch(T&entry);//How? T&find_max(); protected: Binary_node*root; ...auxiliaryfunctionsomitted... CS310Inheritance,Page5' template classSearch_tree:publicBinary_tree{ public: Search_tree();~Search_tree(); boolsearch(T&entry); voidinsert(T&entry); voidremove(T&target); T&find_max(); protected: ...auxiliaryfunctionsomitted... CS310Inheritance,Page6' Binary tree +Binary treeiscalledthebase/parentclass. +Search treeiscalledthederived/childclass. Search tree Search treeoverridesthesearch()andfindmax() methodsofitsbaseclass. Search CS310Inheritance,Page7' ofthederivedclass). tousersofthenewclass. classes. classes,butnotothers. CS310Inheritance,Page8' DerivingAVLTrees template classAVL_tree:publicSearch_tree{ public: AVL_tree();~AVL_tree(); voidinsert(T&entry); voidremove(T&target); protected: ...auxiliaryfunctionsomitted... AVL Binary tree. AVL Search tree. CS310Inheritance,Page9' ADilemma DowewanttoderiveclassHeapfromBinarytree? Binary treeclass. overridden. tradeos. Binary tree. CS310Inheritance,Page10' DerivingtheHeapClass template classHeap:publicBinary_tree{ public: Heap();~Heap(); voidinorder(); voidpreorder(); voidpostorder(); boolsearch(T&entry); T&find_max(); T&remove_max(); voidinsert(T&new_entry); protected: ...auxiliarystuffsomittedhere... CS310Inheritance,Page11' ClassHierarchy Heap Binary_tree Search_tree AVL_tree CS310Inheritance,Page12' HowResourcesareSharedinTheHierarchy BTBSTAVLHeap traversalsbyitselffromBTfromBTbyitself searchbyitselfbyitselffromBSTbyitself insertNAbyitselfbyitselfbyitself removeNAbyitselfbyitselfbyitself assignmentbyitselffromBTfromBTbyitself ndmaxbyitselfbyitselffromBSTbyitself CS310Inheritance,Page13' Polymorphism classX{inta,b;}; classY:publicX{intc;}; main() Xx,*x_ptr=&x; Yy,*y_ptr=&y; x=y;//welosey.c y=x;//illegal x_ptr=&y;//legal; y_ptr=&x;//illegal x_ptr=y_ptr;//legal y_ptr=x_ptr;//illegal CS310Inheritance,Page14' Discussion consideredtobeoftypeX. +SinceSearch treeisaderivedclassofBinarytree,a binarysearchtreeisalsoabinarytree. +Statement"x=y;"willnotcopyy.ctox. class. +ABinary tree*canpointtoaSearchtree. +Inourexample,x ptr=&yislegal. Soisx ptr=yptr. CS310Inheritance,Page15' ASecondExample main() Binary_treebt,*bt_ptr; Search_treebst,*bst_ptr; heaphp,*hp_ptr; AVL_treeavl,*avl_ptr; bt=bst;bt_ptr=&bst; bt=hp;bt_ptr=hp_ptr; bst=hp;bst_ptr=&hp; bst=avl;bst_ptr=avl_ptr; bst_ptr=bt_ptr; avl_ptr=bst_ptr; hp_ptr=avl_ptr; CS310Inheritance,Page16' WaitaMinute... AssumethatavlisacorrectAVLtree. Letbst ptr=&avl; Question:Istheresultofbst ptr->insert(30);acorrect AVLtree? +Search pointerbst ptr) +AVL objectbst ptrpointsto) Solution? CS310Inheritance,Page17' VirtualFunctions template classBinary_tree{ virtualvoidinsert(T&x); virtualvoidremove(T&x); virtualboolfind(T&x); voidinorder(); template classSearch_tree:publicBinary_tree{ voidinsert(T&x); voidremove(T&x); boolfind(T&x); CS310Inheritance,Page18' template classAVL:publicSearch_tree{ voidinsert(T&x); voidremove(T&x); main() Binary_treebt,*bt_ptr; Search_treebst,*bst_ptr; AVLavl,*avl_ptr; ...constructtreesbt,bst,hp,andavl... CS310Inheritance,Page19' bt_ptr=&bt; bt_ptr->insert(30);//theinsert()ofBT bt_ptr->find(30);//thefind()ofBT bt_ptr->inorder();//theinorder()ofBT bt_ptr=&bst; bt_ptr->insert(30);//theinsert()ofBST bt_ptr->find(30);//thefind()ofBST bt_ptr->inorder();//theinorder()ofBT bt_ptr=&avl; bt_ptr->insert(30);//theinsert()ofAVL bt_ptr->find(30);//thefind()ofBST bt_ptr->inorder();//theinorder()ofBT CS310Inheritance,Page20' AnExercise classA{ public: intx; A(){x=1;} voidf(){x+=1;} voidg(){x+=10;} virtualvoidh(){x+=100;} classB:publicA{ public: voidg(){x+=100;} voidh(){x+=1000;} CS310Inheritance,Page21' main() Aa,*a_ptr=&a; Bb,*b_ptr=&b; a_ptr->f();a_ptr->g();a_ptr->h(); cout<x; b_ptr->f();b_ptr->g();b_ptr->h(); cout<x; a_ptr=&b; a_ptr->f();a_ptr->g();a_ptr->h(); cout<x; CS310Inheritance,Page22' AbstractClasses LetusconsiderclassBinarytreeagain. WecannotinsertanythingtoaBinary treeandtoremove anythingfromthetree. ItmakesnosensetoactuallycreateaBinary treeobject. However,theexistenceoftheBinary treeclassmakessense. inheritedbyderivedbinarytrees. ThesolutionistodeneBinary treeasanabstractclass. CS310Inheritance,Page23' template classBinary_tree{ public: Binary_tree();~Binary_tree(); voidinorder(); voidpreorder(); voidpostorder(); Binary_tree&operator=(Binary_tree&); virtualboolsearch(T&entry); virtualvoidinsert(T&entry)=0; virtualvoidremove(T&entry)=0; private: ...Wedon'tcareaboutprivateparthere... CS310Inheritance,Page24' forthefunction. abstractclass. +\Binary treeg;"isillegalwiththenewBinarytree denition. +\Binary tree*g=newBinarytree;"isalsoillegal. However,\Binary tree*g=newAVLtree"islegal,because classes.quotesdbs_dbs19.pdfusesText_25
×
if you Get
No preview available Click on (Next PDF)
Next PDF