Priority Queues and Heaps (Ch 5.5) Huffman Coding Trees (Ch 5.6) Binary Search Trees (Ch 5.4) Lec 5: Binary Tree. Dr. Patrick Chan

Size: px
Start display at page:

Download "Priority Queues and Heaps (Ch 5.5) Huffman Coding Trees (Ch 5.6) Binary Search Trees (Ch 5.4) Lec 5: Binary Tree. Dr. Patrick Chan"

Transcription

1 ata Structure hapter Biary Trees r. Patrick ha School of om puter Sciece ad Egieerig South hia Uiversity of Techolog y Recursio recursio is a procedure which calls itself The recursive procedure call must use differet argumets from the origial oe Otherwise the procedure would always get ito a ifiite loop Outlie Recursio (h.) Biary Trees (h ) Itroductio (h.) Traversal (h.) Implemetatio (h.) Biary Search Trees (h.) Priority Queues ad Heaps (h.) Huffma odig Trees (h.) Recursio as = Example: Factorial fuctio! = (-) f ( ) = f ( it factorial(it ) { if ( == ) retur ; ) retur * factorial(-); if = * factorial() factorial()= * factorial() factorial()= * factorial() factorial()= * factorial() factorial()= factorial() factorial() factorial() factorial() factorial()

2 factorial() factorial() factorial() factorial() factorial() Lec : Biar y Tree Base ase Recursive all How may iphoes the families of studets i our school have? Recursio Lec : Biar y Tree Values of the iput variables for which we perform o recursive calls are called base cases t least oe base case Every possible chai of recursive calls must evetually reach a base case Base ase alls to the curret method Each recursive call should be defied so that it makes progress towards a base case Recursive all it factorial(it ) { if ( == ) retur ; retur * factorial(-); Recursio if x > is eve if x > is odd if x = (/)* = ( /) = () = = +( /)* = (/) = () = () = (/ )* = (/) = () = = +( /)* = (/) = () = () = It is importat that we used a variable twice here rather tha callig the method twice Recursive all Base ase = = = = Lec : Biar y Tree B F E G I J H biary tree is made up of a fiite set of odes that is either empty or cosists of a ode called the together with two biary trees which are disjoit from each other ad from the Biary Tree: Itroductio Lec : Biar y Tree it power(it x, it ) { if ( == ) retur ; if ( % == ) { y = Power(x, ( - )/); retur x * y * y; { y = Power(x, /); retur y y; p( x, ) = x p( x, ( ) / ) p( x, / ) Example has two Recursive alls Recursio

3 Biary Tree: Itroductio Root epth: Level: epth: Level: epth: Level: epth: Level: epth: Level: Paret of V Siblig of V H Subtree ed at V Height is P G V R hildre of V F B J cestors of V Iteral Node Edge Leaf Node escedats of V Biary Tree omplete biary tree If the height of the tree is d, the all levels except possibly level d- are completely full The bottom level has all odes filled i from the left side B H E T H E J I G T B Biary Tree Full biary tree Each ode beig either Leaf Iteral ode with exactly two o-empty childre E H F G E H F Biary Tree Full Biary Tree Theorem Theorem The umber of leaves i a o-empty full biary tree is oe more tha the umber of iteral odes H E T B E H F G H I Leaves Iteral Nodes Leaves Iteral Nodes 9

4 Biary Tree Full Biary Tree Theorem Theorem The umber of ull poiters i a o-empty biary tree is oe more tha the umber of odes i the tree H E T B E H F G H I Leaves Iteral Nodes Null Poiters x = Nodes + = Leaves Iteral Nodes Null Poiters x = Nodes + = 9 Biary Tree: Node // Biary tre e ode class class BiNodePtr : public BiNode<Elem> { private: Elem it; // The ode's value BiNodePtr* lc; // Poiter to left child BiNodePtr* rc; // Poiter to right child public: BiNodePtr() { lc = rc = ; BiNodePtr(Elem e, BiNodePtr* l=, BiNodePtr* r=) { it = e; lc = l; rc = r; Elem& val() { retur it; void setval(cost Elem& e) { it = e; ilie BiNode<Elem>* left() co st { retur lc; ; void setleft(binode<elem>* b) { lc = (BiNodePtr*)b; ilie BiNode<Elem>* right() cost { retur rc; void setright(binode<elem>* b) { rc = (BiNodePtr*)b; bool isleaf() { retur (lc == ) && (rc == ); BiNodePtr anode(); Biary Tree: Node T Template <class Elem> class BiNode { public: virtual Elem& val( ) =; virtual BiNode* left( ) cost = ; virtual BiNode* right( ) cost = ; virtual void setval( cost Elem& ) = ; virtual void setleft( BiNode* ) = ; virtual void setright( BiNode* ) = ; virtual bool isleaf( ) = ; F G I J Biary Tree: Traversals Traversal is a process for visitig the odes i some order y traversal that lists every ode i the tree exactly oce is called a eumeratio of the tree s odes F G I J

5 Biary Tree: Traversals Preorder traversal each ode before visitig its childre B E F G H I J Postorder traversal each ode after visitig its childre B F G E J I H Iorder traversal the left subtree, the the ode, the the right subtree B F E G I J H F G I J preorder() E H I E H subtree= I.L.R B void preorder(binode<elem>* sub) { if (sub == ) retur; visit(sub); preorder(sub->left()); preorder(sub->right()); B.L.L.R Null B.R Null E H E.L H.L Null E.R Null Null H.R I I.L Null I.R Null 9 Biary Tree: Traversals Preorder traversal each ode before visitig its childre void preorder(binode<elem>* sub) { if (sub == ) retur; // Em pty visit(sub); // Perform some actios preorder(sub->left()); // left preorder(sub->right()); // right Postorder traversal each ode after visitig its childre Iorder traversal the left subtree, the the ode, the the right subtree void postorder(binode<elem>* sub) { if (sub == ) retur; // Em pty postorder(sub->left()); // left postorder(sub->right()); // right visit(sub); // Perform some actios void iorder(binode<elem>* sub) { if (sub == ) retur; // Em pty iorder(sub->left()); // left visit(sub); // Perform some actios iorder(sub->right()); // right Biary Tree: Traversals Which oe is better? () void preorder(binode<elem>* sub) { if (sub == ) retur; // Empty visit(sub); // Perform some actio preorder(sub->lc()); preorder(sub->rc()); check if the tree is empty at first F G I J () void preorder(binode<elem>* sub) { visit(sub); // Perform some actio if (sub->left()!= ) preorder(sub->lc()); if (sub->right()!= ) preorder(sub->rc()); check if the right ad left poit are empty

6 Biary Tree: Traversals Which oe is better? dvatage of ase Reduce half recursive calls (refer to Theorem i slide ) dvatage of ase Oly oe base case checkig For complex traversals, checkig criteria may be very complicated void the bugs E.g. the iitial call passes a empty tree ase should be used F G I J Biary Tree Space Requiremets Example Full tree with all odes are the same (two poiters to childre ad oe elemet): Total space required: (p + d) : umber of odes p: the amout of space required by a poiter d: the amout of space required by a data value Overhead Space: p Overhead Fractio: p / (p + d) If p = d, it meas / of total space is take up i overhead E H F G Biary Tree Space Requiremets Overhead Space mout of space ecessary to maitai the data structure i.e ay space ot used to store the data records Overhead Fractio mout of overhead space divided by amout of total space used Small Exercise!!!! Q. Write a recursive fuctio that returs the umber of odes i a biary tree it fidnumber(binode<elem>* sub) if (sub == ) retur ; // Empty subtree it L = fidnumber(sub->left()); it R = fidnumber(sub->right()); retur L+R+; F G

7 Small Exercise!!!! Q. Write a recursive fuctio that returs the umber of iteral odes i a biary tree it fiditnumber(binode<elem>* sub) if (sub == ) retur ; // Empty subtree it L = fiditnumber(sub->left()); it R = fiditnumber(sub->right()); if (sub->left()!= sub->right()!= retur L+R+; retur L+R; F G Small Exercise!!!! Q. Write a recursive fuctio that search value K i a biary tree. The fuctio returs true if value K is exist template <class Key, class Elem, class KEomp> bool search(binode<elem>* sub, Key K) if (sub == ) retur false; bool L = search(sub->left()); bool R = search(sub->right()); bool M = (sub->value() == K); retur L R M; F G Small Exercise!!!! Q. Write a recursive fuctio that returs the height of a biary tree it fidheight(binode<elem>* sub) if (sub == ) retur ; // Empty subtree it L = fidheight(sub->left()); it R = fidheight(sub->right()); if (L > R) retur L + ; retur R + ; F G Biary Search Trees Biary Search Tree (BST) is a special case of Biary Tree ll elemets stored i the left subtree of a ode with value K have values < K ll elemets stored i the right subtree of a ode with value K have values >= K

8 Biary Search Trees: T bool isert(cost Elem& e) { = iserthelp(, e); odecout++; bool remove(cost Key& K, Elem& e) { BiNode<Elem>* t = ; = removehelp(, K, t); if (t == ) retur false; e = t->val(); odecout; delete t; bool isert(cost Elem& e) { = iserthelp(, e); odecout++; Biary Search Trees: T iserthelp fuctio template <class Key, class Elem, class KEomp, class EEomp> BiNode<Elem>* BST<Key,Elem,KEomp,EEomp>:: iserthelp( BiNode<Elem>* sub, cost Elem& val ) { Base ase if (sub == ) // Empty: create ode retur ew BiNodePtr<Elem>(val,,); Recursive all if (EEomp::lt(val, sub->val())) sub->setleft(iserthelp(sub->left(), val)); Recursive all sub->setright(iserthelp(sub->right(), val)); retur sub; // Retur subtree with ode iserted =iserthelp(,); subtree= Retur Biary Search Trees: T // BST implemetatio for the ictioary T template <class Key, class Elem, class KEomp, class EEomp> class BST : public ictioary<key, Elem,KEomp, EEomp> { private: BiNode<Elem>* ; // Root of the BST it odecout; // Number of odes void clearhelp(binode<elem>*); BiNode<Elem>* iserthelp(binode<elem>*, cost Elem&); BiNode<Elem>* deletemi(binode<elem>*,binode<elem>*&); BiNode<Elem>* removehelp (BiNode<Elem>*,cost Key&,BiNode<Elem>*&); bool fidhelp(binode<elem>*, cost Key&,Elem&) cost; void prithelp(binode<elem>*, it) cost; public: BST() { = ; odecout = ; ~BST() { clearhelp(); void clear() { clearhelp(); = ; odecout = ; 9 Biary Search Trees: T bool removey(elem& e) { // elete mi value if ( == ) retur false; // Empty BiNode<Elem>* t; = deletemi(, t); e = t->val(); delete t; odecout; bool fid(cost Key& K, Elem& e) cost { retur fidhelp(, K, e); it size() { retur odecout; void prit() cost { if ( == ) cout << "The BST is empty.\"; prithelp(, );

9 Biary Search Trees: T iserthelp fuctio template <class Key, class Elem, class KEomp, class EEomp> BiNode<Elem>* BST<Key,Elem,KEomp,EEomp>:: iserthelp( BiNode<Elem>* sub, cost Elem& val ) { if (sub == ) // Empty: create ode retur ew BiNodePtr<Elem>(val,,); if (EEomp::lt(val, sub->val())) sub->setleft(iserthelp(sub->left(), val)); sub->setright(iserthelp(sub->right(), val)); retur sub; // Retur subtree with ode iserted subtree= subtree= Retur.L Retur =iserthelp(,); =iserthelp(,); bool isert(cost Elem& e) { = iserthelp(, e); odecout++; Base ase Recursive all Recursive all Biary Search Trees: T deletemi fuctio template <class Key, class Elem, class KEomp, class EEomp> BiNode<Elem>* BST<Key, Elem, KEomp, EEomp>:: deletemi(binode<elem>* sub, BiNode<Elem>*& mi) { if (sub->left() == ) { mi = sub; retur sub->right(); { // otiue left sub->setleft( deletemi(sub->left(), mi)); retur sub; Recursive all subtree= subtree=.l.r (9) Base ase ami 9 =deletemi(,ami); Biary Search Trees: T iserthelp fuctio template <class Key, class Elem, class KEomp, class EEomp> BiNode<Elem>* BST<Key,Elem,KEomp,EEomp>:: iserthelp( BiNode<Elem>* sub, cost Elem& val ) { if (sub == ) // Empty: create ode retur ew BiNodePtr<Elem>(val,,); if (EEomp::lt(val, sub->val())) sub->setleft(iserthelp(sub->left(), val)); sub->setright(iserthelp(sub->right(), val)); retur sub; // Retur subtree with ode iserted subtree= Retur.L subtree= Retur =iserthelp(,); =iserthelp(,); =iserthelp(,9);.r 9 subtree= Retur bool isert(cost Elem& e) { = iserthelp(, e); odecout++; Base ase 9 Recursive all Recursive all Biary Search Trees: T removehelp fuctio template <class Key, class Elem, class KEomp, class EEomp> BiNode<Elem>* BST<Key,Elem,KEomp,EEomp>:: removehelp(binode<elem>* sub, cost Key& K, BiNode<Elem>*& t) { if (sub == ) retur ; aot fid the target if (KEomp::lt(K, sub->val())) Searchig sub->setleft(removehelp(sub->left(), K, t)); if (KEomp::gt(K, sub->val())) sub->setright(removehelp(sub->right(), K, t)); { // Foud it: remove it Foud the target BiNode<Elem>* temp; t = sub; if (sub->left() == ) sub = sub->right(); if (sub->right() == ) sub = sub->left(); { // Both childre are o-empty sub->setright(deletemi(sub->right(), temp)); Elem te = sub->val(); sub->setval(temp->val()); temp->setval(te); t = temp; retur sub; =removehelp(,,abnode); Left Right Bigo! 99.L bool remove(cost Key& K, Elem& e) { BiNode<Elem>* t = ; = removehelp(, K, t); if (t == ) retur false; e = t->val(); odecout; delete t; te = Left Right Bigo!.L t temp sub Left Right Bigo! 99

10 Biary Search Trees: T removehelp fuctio No Left hild 9 No Right hild 9 9 Have both hildre Biary Search Trees: T prithelp fuctios template <class Key, class Elem, class KEomp, class EEomp> void BST<Key,Elem,KEomp,EEomp>:: prithelp(binode<elem>* sub, it level) { if (sub == ) retur ; prithelp(sub->left(), level+); for (it i=; i<level; i++) cout << ; cout << sub->val() << \ ; prithelp(sub->right(), level+); Base ase Recursive all Prit the ode Recursive all prithelp(, ); Biary Search Trees: T clearhelp fuctios void clear() { clearhelp(); = ; odecout = ; template <class Key, class Elem, class KEomp, class EEomp> void BST<Key,Elem,KEomp,EEomp>:: clearhelp(binode<elem>* sub) { if (sub == ) retur; Base ase clearhelp(sub->left()); Recursive all clearhelp(sub->right()); Recursive all delete sub; clearhelp(); elete the ode 9 Small Quiz!!!! Q. raw the BST that results from each of the followig commads: abst abst.isert(); abst.removey(temp); abst.removey(temp); abst.remove(, temp); abst.remove(, temp); abst.remove(9, temp); abst.remove(, temp); abst.prit();

11 Small Quiz!!!! abst.isert(); abst.removey(temp); abst.removey(temp); abst.remove(, temp); abst.remove(, temp); abst.remove(9, temp); abst.remove(, temp); abst.prit(); Biary Tree rray-based omplete BT Simple ad compact array implemetatio approach for complete biary tree No overhead space omplete biary Tree is useful: Heap data structure Exteral sortig algorithm B H E T J I G Biary Search Trees: T ost of BST Operatios ssume d = depth of the tree Fid: Θ(d) Isert: Θ(d) elete: Θ(d) d is Θ(log ) if tree is balaced The worst case: Θ() The average case: Θ(log ) Biary Tree rray-based omplete BT Paret(r) = (r - ) / if r ad r < Leftchild(r) = r + if r+ < Rightchild(r) = r + if r + < Leftsiblig(r) = r - if r is eve, r > ad r < Rightsiblig(r) = r + if r is odd, r + < 9 Positio Paret Left hild 9 Right hild Left Siblig Right Siblig 9 9 9

12 Heaps Heap is a data structure with the followig properties: omplete Biary Tree Value stored i a heap are partially ordered There is a relatioship betwee the value stored at ay ode ad values of its childre Two types of heaps Mi-heap: ll values less tha or equal to child values Max-heap: ll values greater tha or equal to child values Heap ca be implemeted simply by usig the array-based complete biary tree Heaps: T template<class Elem,class omp> class maxheap{ private: Elem* Heap; // Poiter to the heap array it size; // Maximum size of the heap it ; // Number of elems ow i heap void siftdow(it); // Put elemet i place public: maxheap(elem* h, it um, it max); it heapsize() cost; bool isleaf(it pos) cost; it leftchild(it pos) cost; it rightchild(it pos) cost; it paret(it pos) cost; bool isert(cost Elem&); bool removemax(elem&); bool remove(it, Elem&); void buildheap(); ; Heaps Max-heap Mi-heap 99 Heaps buildheap fuctio public void buildheap() // Heapify cotets { for (it i=/-; i>=; i) siftdow(i); o t eed to call siftdow o leaf odes template <class Elem, class omp> void maxheap<elem,omp>::siftdow(it pos) { while (!isleaf(pos)) { it j = leftchild(pos); it rc = rightchild(pos); if ((rc<) && omp::lt(heap[j],heap[rc])) j = rc; if (!omp::lt(heap[pos], Heap[j])) retur; swap(heap, pos, j); pos = j; Positio Paret Left hild Right hild Left Siblig Right Siblig Positio value aheap.buildheap(); aheap.siftdow(); aheap.siftdow(); aheap.siftdow(); pos j rc

13 Heaps buildheap fuctio aheap.buildheap(); aheap.siftdow(); aheap.siftdow(); aheap.siftdow(); aheap.siftdow(); aheap.siftdow(); aheap.siftdow(); 9 Heaps remove fuctio Refer to Figure., pp template <class Elem, class omp> bool maxheap<elem, omp>::remove(it pos, Elem& it) { bool flag = false; if ((pos < ) (pos >= )) retur false; swap(heap, pos, ); while ((pos!= ) && (omp::gt(heap[pos],heap[paret(pos)]))) { swap(heap, pos, paret(pos)); pos = paret(pos); flag=true; if (!flag) siftdow(pos); it = Heap[]; avalue = aheap.remove(, avalue); 9 Heaps removemax fuctio template <class Elem, class omp> bool maxheap<elem, omp>:: removemax(elem& it) { if ( == ) retur false; // Heap is empty swap(heap,, ); // Swap max with ed if (!= ) siftdow(); it = Heap[]; // Retur max value aheap.removemax(avalue); avalue = Heaps remove fuctio Refer to Figure., pp template <class Elem, class omp> bool maxheap<elem, omp>::remove(it pos, Elem& it) { bool flag = false; if ((pos < ) (pos >= )) retur false; swap(heap, pos, ); while ((pos!= ) && (omp::gt(heap[pos],heap[paret(pos)]))) { swap(heap, pos, paret(pos)); pos = paret(pos); flag=true; if (!flag) siftdow(pos); it = Heap[]; avalue = 9 aheap.remove(, avalue); 9 9

14 Small Exercise!!!! Heaps isert fuctio public void buildheap() { for (it i=/-; i>=; i) siftdow(i); ah.siftdow(); template <class Elem, class omp> // Isert elemet bool maxheap<elem, omp>::isert(cost Elem& val) { if ( >= size) retur false; // Heap is full it curr = ++; Heap[curr] = val; // Start at ed of heap // Now sift up util curr's paret > curr while ((curr!=) && (omp::gt(heap[curr], Heap[paret(curr)]))) { swap(heap, curr, paret( curr)); curr = paret( curr); 9 9 aheap.isert(9); ah.buildheap(); 9 9 ah.removemax(temp); ah.remove(, temp); ah.siftdow(); Small Exercise!!!! ah.isert(); 9 ah.siftdow(); ah.siftdow(); ah.siftdow(); 9 ah (maxheap) ah.siftdow(); ah.siftdow(); ah.siftdow(); Small Exercise!!!! ah.removemax(temp);

15 Small Exercise!!!! ah.remove(, temp); Heaps Whe a heap with size is created, there are two choices: Buildheap fuctio Isert values to a heap usig isert fuctio Which oe is better? 9 Small Exercise!!!! ah.isert(); Heaps Time omplexity Isert values to a heap Isert oe value takes Θ(log ) i the worst case Move from the bottom to the top of the tree Buildheap fuctio ( values i the array) epeds o umber of shiftdow calls = log = ( log i =Θ ( log i ) = Θ i ( ) ) x = x = x = 9 We should use buildheap to build the heap i- / i = i i

16 Heaps: Priority Queues priority queue stores objects, ad o request releases the object with greatest value Example: Schedulig jobs i a multi-taskig operatig system Huffma odig Trees etropy ecodig algorithm used for lossless data compressio Use a variable-legth code table for ecodig a source symbol erived based o the estimated probability of occurrece for each possible value Heaps: Priority Queues Possible Solutios: Liked List Isert appeds to a liked list ( O() ) Remove max value determies the maximum by scaig the list ( O() ) Sorted Liked List (decreasig order) Isert places a elemet i its correct positio ( O() ) Remove max value simply removes the head of the list ( O() ) Heap Both isert ad remove max value are O( log ) operatios Huffma odig Trees Fixed-Legth odig SII codes: bits per character E : Z : Variable-Legth odig a take advatage of relative frequecy of letters to save space Z K F U L E Use larger umber of bits to store it Use smaller umber of bits to store it

17 Huffma odig Trees Build the tree with miimum exteral path weight ombie the two smallest values each time 9 9 Z K M L U E Huffma odig Trees Huffma odig has the prefix property No code i the set is the prefix of aother No-prefix example: : B: : What is? or B More tha oe solutios ecode: Z U M E Letter Freq ode Bit E M K L U Huffma odig Trees 9 K Z M L 9 U E Letter E M K L U Z Freq ode Bit Huffma odig Trees Expected ost Expected ost per letter (how may bits have bee used for a letter i average?) x + x + x = / =. + x + x + x + x + x Letter E M K L U Z Freq ode Bit

Data Structure Chapter 5. Binary Trees

Data Structure Chapter 5. Binary Trees http://5.6.4./css/ ata Structure hapter 5 Binary Trees r. Patrick han School of omputer Science and Engineering South hina University of Technology Outline Recursion (h.4) Binary Trees (h 5) Introduction

More information

CIS 121. Introduction to Trees

CIS 121. Introduction to Trees CIS 121 Itroductio to Trees 1 Tree ADT Tree defiitio q A tree is a set of odes which may be empty q If ot empty, the there is a distiguished ode r, called root ad zero or more o-empty subtrees T 1, T 2,

More information

Priority Queues. Binary Heaps

Priority Queues. Binary Heaps Priority Queues Biary Heaps Priority Queues Priority: some property of a object that allows it to be prioritized with respect to other objects of the same type Mi Priority Queue: homogeeous collectio of

More information

Heap: Complete binary tree with the heap property: Min-heap: All values less than child values. Max-heap: All values greater than child values.

Heap: Complete binary tree with the heap property: Min-heap: All values less than child values. Max-heap: All values greater than child values. Heaps Heap: Complete binary tree with the heap property: Min-heap: All values less than child values. Max-heap: All values greater than child values. The values are partially ordered. Heap representation:

More information

Data Structures Week #5. Trees (Ağaçlar)

Data Structures Week #5. Trees (Ağaçlar) Data Structures Week #5 Trees Ağaçlar) Trees Ağaçlar) Toros Gökarı Avrupa Gökarı October 28, 2014 Boraha Tümer, Ph.D. 2 Trees Ağaçlar) October 28, 2014 Boraha Tümer, Ph.D. 3 Outlie Trees Deiitios Implemetatio

More information

CIS 121 Data Structures and Algorithms with Java Spring Stacks, Queues, and Heaps Monday, February 18 / Tuesday, February 19

CIS 121 Data Structures and Algorithms with Java Spring Stacks, Queues, and Heaps Monday, February 18 / Tuesday, February 19 CIS Data Structures ad Algorithms with Java Sprig 09 Stacks, Queues, ad Heaps Moday, February 8 / Tuesday, February 9 Stacks ad Queues Recall the stack ad queue ADTs (abstract data types from lecture.

More information

CIS 121 Data Structures and Algorithms with Java Spring Stacks and Queues Monday, February 12 / Tuesday, February 13

CIS 121 Data Structures and Algorithms with Java Spring Stacks and Queues Monday, February 12 / Tuesday, February 13 CIS Data Structures ad Algorithms with Java Sprig 08 Stacks ad Queues Moday, February / Tuesday, February Learig Goals Durig this lab, you will: Review stacks ad queues. Lear amortized ruig time aalysis

More information

Heaps. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015

Heaps. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 Presetatio for use with the textbook Algorithm Desig ad Applicatios, by M. T. Goodrich ad R. Tamassia, Wiley, 201 Heaps 201 Goodrich ad Tamassia xkcd. http://xkcd.com/83/. Tree. Used with permissio uder

More information

Binary Trees. Directed, Rooted Tree. Terminology. Trees. Binary Trees. Possible Implementation 4/18/2013

Binary Trees. Directed, Rooted Tree. Terminology. Trees. Binary Trees. Possible Implementation 4/18/2013 Directed, Rooted Tree Binary Trees Chapter 5 CPTR 318 Every non-empty directed, rooted tree has A unique element called root One or more elements called leaves Every element except root has a unique parent

More information

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring, Instructions:

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring, Instructions: CS 604 Data Structures Midterm Sprig, 00 VIRG INIA POLYTECHNIC INSTITUTE AND STATE U T PROSI M UNI VERSI TY Istructios: Prit your ame i the space provided below. This examiatio is closed book ad closed

More information

Lecture 5. Counting Sort / Radix Sort

Lecture 5. Counting Sort / Radix Sort Lecture 5. Coutig Sort / Radix Sort T. H. Corme, C. E. Leiserso ad R. L. Rivest Itroductio to Algorithms, 3rd Editio, MIT Press, 2009 Sugkyukwa Uiversity Hyuseug Choo choo@skku.edu Copyright 2000-2018

More information

why study sorting? Sorting is a classic subject in computer science. There are three reasons for studying sorting algorithms.

why study sorting? Sorting is a classic subject in computer science. There are three reasons for studying sorting algorithms. Chapter 5 Sortig IST311 - CIS65/506 Clevelad State Uiversity Prof. Victor Matos Adapted from: Itroductio to Java Programmig: Comprehesive Versio, Eighth Editio by Y. Daiel Liag why study sortig? Sortig

More information

Sorting 9/15/2009. Sorting Problem. Insertion Sort: Soundness. Insertion Sort. Insertion Sort: Running Time. Insertion Sort: Soundness

Sorting 9/15/2009. Sorting Problem. Insertion Sort: Soundness. Insertion Sort. Insertion Sort: Running Time. Insertion Sort: Soundness 9/5/009 Algorithms Sortig 3- Sortig Sortig Problem The Sortig Problem Istace: A sequece of umbers Objective: A permutatio (reorderig) such that a ' K a' a, K,a a ', K, a' of the iput sequece The umbers

More information

Minimum Spanning Trees

Minimum Spanning Trees Miimum Spaig Trees Miimum Spaig Trees Spaig subgraph Subgraph of a graph G cotaiig all the vertices of G Spaig tree Spaig subgraph that is itself a (free) tree Miimum spaig tree (MST) Spaig tree of a weighted

More information

top() Applications of Stacks

top() Applications of Stacks CS22 Algorithms ad Data Structures MW :00 am - 2: pm, MSEC 0 Istructor: Xiao Qi Lecture 6: Stacks ad Queues Aoucemets Quiz results Homework 2 is available Due o September 29 th, 2004 www.cs.mt.edu~xqicoursescs22

More information

COSC 1P03. Ch 7 Recursion. Introduction to Data Structures 8.1

COSC 1P03. Ch 7 Recursion. Introduction to Data Structures 8.1 COSC 1P03 Ch 7 Recursio Itroductio to Data Structures 8.1 COSC 1P03 Recursio Recursio I Mathematics factorial Fiboacci umbers defie ifiite set with fiite defiitio I Computer Sciece sytax rules fiite defiitio,

More information

Announcements TREES II. Comparing Data Structures. Binary Search Trees. Red-Black Trees. Red-Black Trees 3/13/18

Announcements TREES II. Comparing Data Structures. Binary Search Trees. Red-Black Trees. Red-Black Trees 3/13/18 //8 Aoucemets Prelim is Toight, brig your studet ID :PM EXAM OLH: etids startig aa to dh OLH: etids startig di to ji PHL: etids startig jj to ks (Plus studets who switched from the 7: exam) TREES II Lecture

More information

Fundamental Algorithms

Fundamental Algorithms Techische Uiversität Müche Fakultät für Iformatik Lehrstuhl für Effiziete Algorithme Dmytro Chibisov Sadeep Sadaada Witer Semester 2007/08 Solutio Sheet 6 November 30, 2007 Fudametal Algorithms Problem

More information

BST Sequence of Operations

BST Sequence of Operations Splay Trees Problems with BSTs Because the shape of a BST is determied by the order that data is iserted, we ru the risk of trees that are essetially lists 12 21 20 32 24 37 15 40 55 56 77 2 BST Sequece

More information

Data Structures and Algorithms Part 1.4

Data Structures and Algorithms Part 1.4 1 Data Structures ad Algorithms Part 1.4 Werer Nutt 2 DSA, Part 1: Itroductio, syllabus, orgaisatio Algorithms Recursio (priciple, trace, factorial, Fiboacci) Sortig (bubble, isertio, selectio) 3 Sortig

More information

Data Structures Week #9. Sorting

Data Structures Week #9. Sorting Data Structures Week #9 Sortig Outlie Motivatio Types of Sortig Elemetary (O( 2 )) Sortig Techiques Other (O(*log())) Sortig Techiques 21.Aralık.2010 Boraha Tümer, Ph.D. 2 Sortig 21.Aralık.2010 Boraha

More information

Data Structure Lecture#12: Binary Trees 3 (Chapter 5) U Kang Seoul National University

Data Structure Lecture#12: Binary Trees 3 (Chapter 5) U Kang Seoul National University Data Structure Lecture#12: Binary Trees 3 (Chapter 5) U Kang Seoul National University U Kang (2016) 1 In This Lecture Motivation of Priority Queue data structure Main ideas and implementations of Heap

More information

CS211 Fall 2003 Prelim 2 Solutions and Grading Guide

CS211 Fall 2003 Prelim 2 Solutions and Grading Guide CS11 Fall 003 Prelim Solutios ad Gradig Guide Problem 1: (a) obj = obj1; ILLEGAL because type of referece must always be a supertype of type of object (b) obj3 = obj1; ILLEGAL because type of referece

More information

6.854J / J Advanced Algorithms Fall 2008

6.854J / J Advanced Algorithms Fall 2008 MIT OpeCourseWare http://ocw.mit.edu 6.854J / 18.415J Advaced Algorithms Fall 2008 For iformatio about citig these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. 18.415/6.854 Advaced Algorithms

More information

Homework 1 Solutions MA 522 Fall 2017

Homework 1 Solutions MA 522 Fall 2017 Homework 1 Solutios MA 5 Fall 017 1. Cosider the searchig problem: Iput A sequece of umbers A = [a 1,..., a ] ad a value v. Output A idex i such that v = A[i] or the special value NIL if v does ot appear

More information

. Written in factored form it is easy to see that the roots are 2, 2, i,

. Written in factored form it is easy to see that the roots are 2, 2, i, CMPS A Itroductio to Programmig Programmig Assigmet 4 I this assigmet you will write a java program that determies the real roots of a polyomial that lie withi a specified rage. Recall that the roots (or

More information

Chapter 24. Sorting. Objectives. 1. To study and analyze time efficiency of various sorting algorithms

Chapter 24. Sorting. Objectives. 1. To study and analyze time efficiency of various sorting algorithms Chapter 4 Sortig 1 Objectives 1. o study ad aalyze time efficiecy of various sortig algorithms 4. 4.7.. o desig, implemet, ad aalyze bubble sort 4.. 3. o desig, implemet, ad aalyze merge sort 4.3. 4. o

More information

Recursion. Recursion. Mathematical induction: example. Recursion. The sum of the first n odd numbers is n 2 : Informal proof: Principle:

Recursion. Recursion. Mathematical induction: example. Recursion. The sum of the first n odd numbers is n 2 : Informal proof: Principle: Recursio Recursio Jordi Cortadella Departmet of Computer Sciece Priciple: Reduce a complex problem ito a simpler istace of the same problem Recursio Itroductio to Programmig Dept. CS, UPC 2 Mathematical

More information

Design and Analysis of Algorithms Notes

Design and Analysis of Algorithms Notes Desig ad Aalysis of Algorithms Notes Notes by Wist Course taught by Dr. K Amer Course started: Jauary 4, 013 Course eded: December 13, 01 Curret geeratio: December 18, 013 Listigs 1 Array sum pseudocode.................................

More information

Graphs. Minimum Spanning Trees. Slides by Rose Hoberman (CMU)

Graphs. Minimum Spanning Trees. Slides by Rose Hoberman (CMU) Graphs Miimum Spaig Trees Slides by Rose Hoberma (CMU) Problem: Layig Telephoe Wire Cetral office 2 Wirig: Naïve Approach Cetral office Expesive! 3 Wirig: Better Approach Cetral office Miimize the total

More information

DATA STRUCTURES. amortized analysis binomial heaps Fibonacci heaps union-find. Data structures. Appetizer. Appetizer

DATA STRUCTURES. amortized analysis binomial heaps Fibonacci heaps union-find. Data structures. Appetizer. Appetizer Data structures DATA STRUCTURES Static problems. Give a iput, produce a output. Ex. Sortig, FFT, edit distace, shortest paths, MST, max-flow,... amortized aalysis biomial heaps Fiboacci heaps uio-fid Dyamic

More information

binary tree empty root subtrees Node Children Edge Parent Ancestor Descendant Path Depth Height Level Leaf Node Internal Node Subtree

binary tree empty root subtrees Node Children Edge Parent Ancestor Descendant Path Depth Height Level Leaf Node Internal Node Subtree Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees called the left and right subtrees which are disjoint

More information

Major CSL Write your name and entry no on every sheet of the answer script. Time 2 Hrs Max Marks 70

Major CSL Write your name and entry no on every sheet of the answer script. Time 2 Hrs Max Marks 70 NOTE:. Attempt all seve questios. Major CSL 02 2. Write your ame ad etry o o every sheet of the aswer script. Time 2 Hrs Max Marks 70 Q No Q Q 2 Q 3 Q 4 Q 5 Q 6 Q 7 Total MM 6 2 4 0 8 4 6 70 Q. Write a

More information

Basic allocator mechanisms The course that gives CMU its Zip! Memory Management II: Dynamic Storage Allocation Mar 6, 2000.

Basic allocator mechanisms The course that gives CMU its Zip! Memory Management II: Dynamic Storage Allocation Mar 6, 2000. 5-23 The course that gives CM its Zip Memory Maagemet II: Dyamic Storage Allocatio Mar 6, 2000 Topics Segregated lists Buddy system Garbage collectio Mark ad Sweep Copyig eferece coutig Basic allocator

More information

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures Uiversity of Waterloo Departmet of Electrical ad Computer Egieerig ECE 250 Algorithms ad Data Structures Midterm Examiatio ( pages) Istructor: Douglas Harder February 7, 2004 7:30-9:00 Name (last, first)

More information

Array Applications. Sorting. Want to put the contents of an array in order. Selection Sort Bubble Sort Insertion Sort. Quicksort Quickersort

Array Applications. Sorting. Want to put the contents of an array in order. Selection Sort Bubble Sort Insertion Sort. Quicksort Quickersort Sortig Wat to put the cotets of a arra i order Selectio Sort Bubble Sort Isertio Sort Quicksort Quickersort 2 tj Bubble Sort - coceptual Sort a arra of umbers ito ascedig or descedig order Split the list

More information

Lower Bounds for Sorting

Lower Bounds for Sorting Liear Sortig Topics Covered: Lower Bouds for Sortig Coutig Sort Radix Sort Bucket Sort Lower Bouds for Sortig Compariso vs. o-compariso sortig Decisio tree model Worst case lower boud Compariso Sortig

More information

A graphical view of big-o notation. c*g(n) f(n) f(n) = O(g(n))

A graphical view of big-o notation. c*g(n) f(n) f(n) = O(g(n)) ca see that time required to search/sort grows with size of We How do space/time eeds of program grow with iput size? iput. time: cout umber of operatios as fuctio of iput Executio size operatio Assigmet:

More information

quality/quantity peak time/ratio

quality/quantity peak time/ratio Semi-Heap ad Its Applicatios i Touramet Rakig Jie Wu Departmet of omputer Sciece ad Egieerig Florida Atlatic Uiversity oca Rato, FL 3343 jie@cse.fau.edu September, 00 . Itroductio ad Motivatio. relimiaries

More information

Computer Science Foundation Exam. August 12, Computer Science. Section 1A. No Calculators! KEY. Solutions and Grading Criteria.

Computer Science Foundation Exam. August 12, Computer Science. Section 1A. No Calculators! KEY. Solutions and Grading Criteria. Computer Sciece Foudatio Exam August, 005 Computer Sciece Sectio A No Calculators! Name: SSN: KEY Solutios ad Gradig Criteria Score: 50 I this sectio of the exam, there are four (4) problems. You must

More information

EFFICIENT MULTIPLE SEARCH TREE STRUCTURE

EFFICIENT MULTIPLE SEARCH TREE STRUCTURE EFFICIENT MULTIPLE SEARCH TREE STRUCTURE Mohammad Reza Ghaeii 1 ad Mohammad Reza Mirzababaei 1 Departmet of Computer Egieerig ad Iformatio Techology, Amirkabir Uiversity of Techology, Tehra, Ira mr.ghaeii@aut.ac.ir

More information

! Given the following Structure: ! We can define a pointer to a structure. ! Now studentptr points to the s1 structure.

! Given the following Structure: ! We can define a pointer to a structure. ! Now studentptr points to the s1 structure. Liked Lists Uit 5 Sectios 11.9 & 18.1-2 CS 2308 Fall 2018 Jill Seama 11.9: Poiters to Structures! Give the followig Structure: struct Studet { strig ame; // Studet s ame it idnum; // Studet ID umber it

More information

Sorting in Linear Time. Data Structures and Algorithms Andrei Bulatov

Sorting in Linear Time. Data Structures and Algorithms Andrei Bulatov Sortig i Liear Time Data Structures ad Algorithms Adrei Bulatov Algorithms Sortig i Liear Time 7-2 Compariso Sorts The oly test that all the algorithms we have cosidered so far is compariso The oly iformatio

More information

CSE 2320 Notes 8: Sorting. (Last updated 10/3/18 7:16 PM) Idea: Take an unsorted (sub)array and partition into two subarrays such that.

CSE 2320 Notes 8: Sorting. (Last updated 10/3/18 7:16 PM) Idea: Take an unsorted (sub)array and partition into two subarrays such that. CSE Notes 8: Sortig (Last updated //8 7:6 PM) CLRS 7.-7., 9., 8.-8. 8.A. QUICKSORT Cocepts Idea: Take a usorted (sub)array ad partitio ito two subarrays such that p q r x y z x y y z Pivot Customarily,

More information

Recursion. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Review: Method Frames

Recursion. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Review: Method Frames Uit 4, Part 3 Recursio Computer Sciece S-111 Harvard Uiversity David G. Sulliva, Ph.D. Review: Method Frames Whe you make a method call, the Java rutime sets aside a block of memory kow as the frame of

More information

CS200: Hash Tables. Prichard Ch CS200 - Hash Tables 1

CS200: Hash Tables. Prichard Ch CS200 - Hash Tables 1 CS200: Hash Tables Prichard Ch. 13.2 CS200 - Hash Tables 1 Table Implemetatios: average cases Search Add Remove Sorted array-based Usorted array-based Balaced Search Trees O(log ) O() O() O() O(1) O()

More information

Algorithm Design Techniques. Divide and conquer Problem

Algorithm Design Techniques. Divide and conquer Problem Algorithm Desig Techiques Divide ad coquer Problem Divide ad Coquer Algorithms Divide ad Coquer algorithm desig works o the priciple of dividig the give problem ito smaller sub problems which are similar

More information

Examples and Applications of Binary Search

Examples and Applications of Binary Search Toy Gog ITEE Uiersity of Queeslad I the secod lecture last week we studied the biary search algorithm that soles the problem of determiig if a particular alue appears i a sorted list of iteger or ot. We

More information

CSE 417: Algorithms and Computational Complexity

CSE 417: Algorithms and Computational Complexity Time CSE 47: Algorithms ad Computatioal Readig assigmet Read Chapter of The ALGORITHM Desig Maual Aalysis & Sortig Autum 00 Paul Beame aalysis Problem size Worst-case complexity: max # steps algorithm

More information

CMSC Computer Architecture Lecture 3: ISA and Introduction to Microarchitecture. Prof. Yanjing Li University of Chicago

CMSC Computer Architecture Lecture 3: ISA and Introduction to Microarchitecture. Prof. Yanjing Li University of Chicago CMSC 22200 Computer Architecture Lecture 3: ISA ad Itroductio to Microarchitecture Prof. Yajig Li Uiversity of Chicago Lecture Outlie ISA uarch (hardware implemetatio of a ISA) Logic desig basics Sigle-cycle

More information

Chapter 11. Friends, Overloaded Operators, and Arrays in Classes. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 11. Friends, Overloaded Operators, and Arrays in Classes. Copyright 2014 Pearson Addison-Wesley. All rights reserved. Chapter 11 Frieds, Overloaded Operators, ad Arrays i Classes Copyright 2014 Pearso Addiso-Wesley. All rights reserved. Overview 11.1 Fried Fuctios 11.2 Overloadig Operators 11.3 Arrays ad Classes 11.4

More information

Ones Assignment Method for Solving Traveling Salesman Problem

Ones Assignment Method for Solving Traveling Salesman Problem Joural of mathematics ad computer sciece 0 (0), 58-65 Oes Assigmet Method for Solvig Travelig Salesma Problem Hadi Basirzadeh Departmet of Mathematics, Shahid Chamra Uiversity, Ahvaz, Ira Article history:

More information

15-859E: Advanced Algorithms CMU, Spring 2015 Lecture #2: Randomized MST and MST Verification January 14, 2015

15-859E: Advanced Algorithms CMU, Spring 2015 Lecture #2: Randomized MST and MST Verification January 14, 2015 15-859E: Advaced Algorithms CMU, Sprig 2015 Lecture #2: Radomized MST ad MST Verificatio Jauary 14, 2015 Lecturer: Aupam Gupta Scribe: Yu Zhao 1 Prelimiaries I this lecture we are talkig about two cotets:

More information

Linked Lists 11/16/18. Preliminaries. Java References. Objects and references. Self references. Linking self-referential nodes

Linked Lists 11/16/18. Preliminaries. Java References. Objects and references. Self references. Linking self-referential nodes Prelimiaries Liked Lists public class StrageObject { Strig ame; StrageObject other; Arrays are ot always the optimal data structure: A array has fixed size eeds to be copied to expad its capacity Addig

More information

Computers and Scientific Thinking

Computers and Scientific Thinking Computers ad Scietific Thikig David Reed, Creighto Uiversity Chapter 15 JavaScript Strigs 1 Strigs as Objects so far, your iteractive Web pages have maipulated strigs i simple ways use text box to iput

More information

Informed Search. Russell and Norvig Chap. 3

Informed Search. Russell and Norvig Chap. 3 Iformed Search Russell ad Norvig Chap. 3 Not all search directios are equally promisig Outlie Iformed: use problem-specific kowledge Add a sese of directio to search: work toward the goal Heuristic fuctios:

More information

Computational Geometry

Computational Geometry Computatioal Geometry Chapter 4 Liear programmig Duality Smallest eclosig disk O the Ageda Liear Programmig Slides courtesy of Craig Gotsma 4. 4. Liear Programmig - Example Defie: (amout amout cosumed

More information

Chapter 9. Pointers and Dynamic Arrays. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Chapter 9. Pointers and Dynamic Arrays. Copyright 2015 Pearson Education, Ltd.. All rights reserved. Chapter 9 Poiters ad Dyamic Arrays Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Overview 9.1 Poiters 9.2 Dyamic Arrays Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 9-3

More information

n The C++ template facility provides the ability to define n A generic facility allows code to be written once then

n The C++ template facility provides the ability to define n A generic facility allows code to be written once then UCLA PIC 10 B Problem Solvig usig C++ Programmig Ivo Diov, Asst. Prof. i Mathematics, Neurology, Statistics Istructor: Teachig Assistat: Suzae Nezzar, Mathematics Chapter 13 Templates for More Abstractio

More information

Chapter 10. Defining Classes. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Chapter 10. Defining Classes. Copyright 2015 Pearson Education, Ltd.. All rights reserved. Chapter 10 Defiig Classes Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Overview 10.1 Structures 10.2 Classes 10.3 Abstract Data Types 10.4 Itroductio to Iheritace Copyright 2015 Pearso Educatio,

More information

ECE4050 Data Structures and Algorithms. Lecture 6: Searching

ECE4050 Data Structures and Algorithms. Lecture 6: Searching ECE4050 Data Structures ad Algorithms Lecture 6: Searchig 1 Search Give: Distict keys k 1, k 2,, k ad collectio L of records of the form (k 1, I 1 ), (k 2, I 2 ),, (k, I ) where I j is the iformatio associated

More information

Thompson s Group F (p + 1) is not Minimally Almost Convex

Thompson s Group F (p + 1) is not Minimally Almost Convex Thompso s Group F (p + ) is ot Miimally Almost Covex Claire Wladis Thompso s Group F (p + ). A Descriptio of F (p + ) Thompso s group F (p + ) ca be defied as the group of piecewiseliear orietatio-preservig

More information

Alpha Individual Solutions MAΘ National Convention 2013

Alpha Individual Solutions MAΘ National Convention 2013 Alpha Idividual Solutios MAΘ Natioal Covetio 0 Aswers:. D. A. C 4. D 5. C 6. B 7. A 8. C 9. D 0. B. B. A. D 4. C 5. A 6. C 7. B 8. A 9. A 0. C. E. B. D 4. C 5. A 6. D 7. B 8. C 9. D 0. B TB. 570 TB. 5

More information

Lecture Notes 6 Introduction to algorithm analysis CSS 501 Data Structures and Object-Oriented Programming

Lecture Notes 6 Introduction to algorithm analysis CSS 501 Data Structures and Object-Oriented Programming Lecture Notes 6 Itroductio to algorithm aalysis CSS 501 Data Structures ad Object-Orieted Programmig Readig for this lecture: Carrao, Chapter 10 To be covered i this lecture: Itroductio to algorithm aalysis

More information

6.851: Advanced Data Structures Spring Lecture 17 April 24

6.851: Advanced Data Structures Spring Lecture 17 April 24 6.851: Advaced Data Structures Sprig 2012 Prof. Erik Demaie Lecture 17 April 24 Scribes: David Bejami(2012), Li Fei(2012), Yuzhi Zheg(2012),Morteza Zadimoghaddam(2010), Aaro Berstei(2007) 1 Overview Up

More information

9.1. Sequences and Series. Sequences. What you should learn. Why you should learn it. Definition of Sequence

9.1. Sequences and Series. Sequences. What you should learn. Why you should learn it. Definition of Sequence _9.qxd // : AM Page Chapter 9 Sequeces, Series, ad Probability 9. Sequeces ad Series What you should lear Use sequece otatio to write the terms of sequeces. Use factorial otatio. Use summatio otatio to

More information

Overview. Chapter 18 Vectors and Arrays. Reminder. vector. Bjarne Stroustrup

Overview. Chapter 18 Vectors and Arrays. Reminder. vector. Bjarne Stroustrup Chapter 18 Vectors ad Arrays Bjare Stroustrup Vector revisited How are they implemeted? Poiters ad free store Destructors Iitializatio Copy ad move Arrays Array ad poiter problems Chagig size Templates

More information

Elementary Data Structures. The Stack ADT ( 2.1.1) Applications of Stacks. Stacks, Queues, Vectors, Lists & Sequences Trees

Elementary Data Structures. The Stack ADT ( 2.1.1) Applications of Stacks. Stacks, Queues, Vectors, Lists & Sequences Trees Elemetary Data Structures Stacks, Queues, ectors, Lists & Sequeces Trees The Stack ADT ( 2..) The Stack ADT stores arbitrary objects Isertios ad deletios follow the last-i first-out scheme Thik of a sprig-loaded

More information

The isoperimetric problem on the hypercube

The isoperimetric problem on the hypercube The isoperimetric problem o the hypercube Prepared by: Steve Butler November 2, 2005 1 The isoperimetric problem We will cosider the -dimesioal hypercube Q Recall that the hypercube Q is a graph whose

More information

Greedy Algorithms. Interval Scheduling. Greedy Algorithms. Interval scheduling. Greedy Algorithms. Interval Scheduling

Greedy Algorithms. Interval Scheduling. Greedy Algorithms. Interval scheduling. Greedy Algorithms. Interval Scheduling Greedy Algorithms Greedy Algorithms Witer Paul Beame Hard to defie exactly but ca give geeral properties Solutio is built i small steps Decisios o how to build the solutio are made to maximize some criterio

More information

Chapter 5. Binary Trees

Chapter 5. Binary Trees Chapter 5 Binary Trees Definitions and Properties A binary tree is made up of a finite set of elements called nodes It consists of a root and two subtrees There is an edge from the root to its children

More information

What are we going to learn? CSC Data Structures Analysis of Algorithms. Overview. Algorithm, and Inputs

What are we going to learn? CSC Data Structures Analysis of Algorithms. Overview. Algorithm, and Inputs What are we goig to lear? CSC316-003 Data Structures Aalysis of Algorithms Computer Sciece North Carolia State Uiversity Need to say that some algorithms are better tha others Criteria for evaluatio Structure

More information

Lecture 1: Introduction and Strassen s Algorithm

Lecture 1: Introduction and Strassen s Algorithm 5-750: Graduate Algorithms Jauary 7, 08 Lecture : Itroductio ad Strasse s Algorithm Lecturer: Gary Miller Scribe: Robert Parker Itroductio Machie models I this class, we will primarily use the Radom Access

More information

Analysis of Algorithms

Analysis of Algorithms Aalysis of Algorithms Ruig Time of a algorithm Ruig Time Upper Bouds Lower Bouds Examples Mathematical facts Iput Algorithm Output A algorithm is a step-by-step procedure for solvig a problem i a fiite

More information

A New Morphological 3D Shape Decomposition: Grayscale Interframe Interpolation Method

A New Morphological 3D Shape Decomposition: Grayscale Interframe Interpolation Method A ew Morphological 3D Shape Decompositio: Grayscale Iterframe Iterpolatio Method D.. Vizireau Politehica Uiversity Bucharest, Romaia ae@comm.pub.ro R. M. Udrea Politehica Uiversity Bucharest, Romaia mihea@comm.pub.ro

More information

Minimum Spanning Trees

Minimum Spanning Trees Presetatio for use with the textbook, lgorithm esig ad pplicatios, by M. T. Goodrich ad R. Tamassia, Wiley, 0 Miimum Spaig Trees 0 Goodrich ad Tamassia Miimum Spaig Trees pplicatio: oectig a Network Suppose

More information

Threads and Concurrency in Java: Part 1

Threads and Concurrency in Java: Part 1 Cocurrecy Threads ad Cocurrecy i Java: Part 1 What every computer egieer eeds to kow about cocurrecy: Cocurrecy is to utraied programmers as matches are to small childre. It is all too easy to get bured.

More information

How do we evaluate algorithms?

How do we evaluate algorithms? F2 Readig referece: chapter 2 + slides Algorithm complexity Big O ad big Ω To calculate ruig time Aalysis of recursive Algorithms Next time: Litterature: slides mostly The first Algorithm desig methods:

More information

Threads and Concurrency in Java: Part 1

Threads and Concurrency in Java: Part 1 Threads ad Cocurrecy i Java: Part 1 1 Cocurrecy What every computer egieer eeds to kow about cocurrecy: Cocurrecy is to utraied programmers as matches are to small childre. It is all too easy to get bured.

More information

Minimum Spanning Trees. Application: Connecting a Network

Minimum Spanning Trees. Application: Connecting a Network Miimum Spaig Tree // : Presetatio for use with the textbook, lgorithm esig ad pplicatios, by M. T. oodrich ad R. Tamassia, Wiley, Miimum Spaig Trees oodrich ad Tamassia Miimum Spaig Trees pplicatio: oectig

More information

Python Programming: An Introduction to Computer Science

Python Programming: An Introduction to Computer Science Pytho Programmig: A Itroductio to Computer Sciece Chapter 6 Defiig Fuctios Pytho Programmig, 2/e 1 Objectives To uderstad why programmers divide programs up ito sets of cooperatig fuctios. To be able to

More information

CMPT 125 Assignment 2 Solutions

CMPT 125 Assignment 2 Solutions CMPT 25 Assigmet 2 Solutios Questio (20 marks total) a) Let s cosider a iteger array of size 0. (0 marks, each part is 2 marks) it a[0]; I. How would you assig a poiter, called pa, to store the address

More information

Combination Labelings Of Graphs

Combination Labelings Of Graphs Applied Mathematics E-Notes, (0), - c ISSN 0-0 Available free at mirror sites of http://wwwmaththuedutw/ame/ Combiatio Labeligs Of Graphs Pak Chig Li y Received February 0 Abstract Suppose G = (V; E) is

More information

From last week. Lecture 5. Outline. Principles of programming languages

From last week. Lecture 5. Outline. Principles of programming languages Priciples of programmig laguages From last week Lecture 5 http://few.vu.l/~silvis/ppl/2007 Natalia Silvis-Cividjia e-mail: silvis@few.vu.l ML has o assigmet. Explai how to access a old bidig? Is & for

More information

Classes and Objects. Again: Distance between points within the first quadrant. José Valente de Oliveira 4-1

Classes and Objects. Again: Distance between points within the first quadrant. José Valente de Oliveira 4-1 Classes ad Objects jvo@ualg.pt José Valete de Oliveira 4-1 Agai: Distace betwee poits withi the first quadrat Sample iput Sample output 1 1 3 4 2 jvo@ualg.pt José Valete de Oliveira 4-2 1 The simplest

More information

WORKED EXAMPLE 7.1. Producing a Mass Mailing. We want to automate the process of producing mass mailings. A typical letter might look as follows:

WORKED EXAMPLE 7.1. Producing a Mass Mailing. We want to automate the process of producing mass mailings. A typical letter might look as follows: Worked Example 7.1 Producig a Mass Mailig 1 WORKED EXAMPLE 7.1 Producig a Mass Mailig We wat to automate the process of producig mass mailigs. A typical letter might look as follows: To: Ms. Sally Smith

More information

Pseudocode ( 1.1) Analysis of Algorithms. Primitive Operations. Pseudocode Details. Running Time ( 1.1) Estimating performance

Pseudocode ( 1.1) Analysis of Algorithms. Primitive Operations. Pseudocode Details. Running Time ( 1.1) Estimating performance Aalysis of Algorithms Iput Algorithm Output A algorithm is a step-by-step procedure for solvig a problem i a fiite amout of time. Pseudocode ( 1.1) High-level descriptio of a algorithm More structured

More information

EVALUATION OF TRIGONOMETRIC FUNCTIONS

EVALUATION OF TRIGONOMETRIC FUNCTIONS EVALUATION OF TRIGONOMETRIC FUNCTIONS Whe first exposed to trigoometric fuctios i high school studets are expected to memorize the values of the trigoometric fuctios of sie cosie taget for the special

More information

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe CHAPTER 18 Strategies for Query Processig Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Itroductio DBMS techiques to process a query Scaer idetifies

More information

5.3 Recursive definitions and structural induction

5.3 Recursive definitions and structural induction /8/05 5.3 Recursive defiitios ad structural iductio CSE03 Discrete Computatioal Structures Lecture 6 A recursively defied picture Recursive defiitios e sequece of powers of is give by a = for =0,,, Ca

More information

condition w i B i S maximum u i

condition w i B i S maximum u i ecture 10 Dyamic Programmig 10.1 Kapsack Problem November 1, 2004 ecturer: Kamal Jai Notes: Tobias Holgers We are give a set of items U = {a 1, a 2,..., a }. Each item has a weight w i Z + ad a utility

More information

Wavelet Transform. CSE 490 G Introduction to Data Compression Winter Wavelet Transformed Barbara (Enhanced) Wavelet Transformed Barbara (Actual)

Wavelet Transform. CSE 490 G Introduction to Data Compression Winter Wavelet Transformed Barbara (Enhanced) Wavelet Transformed Barbara (Actual) Wavelet Trasform CSE 49 G Itroductio to Data Compressio Witer 6 Wavelet Trasform Codig PACW Wavelet Trasform A family of atios that filters the data ito low resolutio data plus detail data high pass filter

More information

CHAPTER IV: GRAPH THEORY. Section 1: Introduction to Graphs

CHAPTER IV: GRAPH THEORY. Section 1: Introduction to Graphs CHAPTER IV: GRAPH THEORY Sectio : Itroductio to Graphs Sice this class is called Number-Theoretic ad Discrete Structures, it would be a crime to oly focus o umber theory regardless how woderful those topics

More information

Lecturers: Sanjam Garg and Prasad Raghavendra Feb 21, Midterm 1 Solutions

Lecturers: Sanjam Garg and Prasad Raghavendra Feb 21, Midterm 1 Solutions U.C. Berkeley CS170 : Algorithms Midterm 1 Solutios Lecturers: Sajam Garg ad Prasad Raghavedra Feb 1, 017 Midterm 1 Solutios 1. (4 poits) For the directed graph below, fid all the strogly coected compoets

More information

Overview. Common tasks. Observation. Chapter 20 The STL (containers, iterators, and algorithms) 8/13/18. Bjarne Stroustrup

Overview. Common tasks. Observation. Chapter 20 The STL (containers, iterators, and algorithms) 8/13/18. Bjarne Stroustrup Overview Chapter 20 The STL (cotaiers, iterators, ad algorithms) Bjare Stroustrup www.stroustrup.com/programmig Commo tasks ad ideals Geeric programmig Cotaiers, algorithms, ad iterators The simplest algorithm:

More information

1/27/12. Vectors: Outline and Reading. Chapter 6: Vectors, Lists and Sequences. The Vector ADT. Applications of Vectors. Array based Vector: Insertion

1/27/12. Vectors: Outline and Reading. Chapter 6: Vectors, Lists and Sequences. The Vector ADT. Applications of Vectors. Array based Vector: Insertion Chater 6: ectors, Lists ad Sequeces ectors: Outlie ad Readig The ector ADT ( 6.1.1) Array-based imlemetatio ( 6.1.2) Nacy Amato Parasol Lab, Det. CSE, Texas A&M Uiversity Ackowledgemet: These slides are

More information

CSC 220: Computer Organization Unit 11 Basic Computer Organization and Design

CSC 220: Computer Organization Unit 11 Basic Computer Organization and Design College of Computer ad Iformatio Scieces Departmet of Computer Sciece CSC 220: Computer Orgaizatio Uit 11 Basic Computer Orgaizatio ad Desig 1 For the rest of the semester, we ll focus o computer architecture:

More information

Analysis Metrics. Intro to Algorithm Analysis. Slides. 12. Alg Analysis. 12. Alg Analysis

Analysis Metrics. Intro to Algorithm Analysis. Slides. 12. Alg Analysis. 12. Alg Analysis Itro to Algorithm Aalysis Aalysis Metrics Slides. Table of Cotets. Aalysis Metrics 3. Exact Aalysis Rules 4. Simple Summatio 5. Summatio Formulas 6. Order of Magitude 7. Big-O otatio 8. Big-O Theorems

More information

Intermediate Statistics

Intermediate Statistics Gait Learig Guides Itermediate Statistics Data processig & display, Cetral tedecy Author: Raghu M.D. STATISTICS DATA PROCESSING AND DISPLAY Statistics is the study of data or umerical facts of differet

More information

Protected points in ordered trees

Protected points in ordered trees Applied Mathematics Letters 008 56 50 www.elsevier.com/locate/aml Protected poits i ordered trees Gi-Sag Cheo a, Louis W. Shapiro b, a Departmet of Mathematics, Sugkyukwa Uiversity, Suwo 440-746, Republic

More information