Algorithms and Data Structures

Size: px
Start display at page:

Download "Algorithms and Data Structures"

Transcription

1 Algorithms and Data Structures Markus Würzl, Vienna University of Technology Solving Interesting Problems. Create data structures & algorithms to solve problems. 2. Prove algorithms work. Buggy algorithms are worthless! 3. Examine properties of algorithms. Simplicity, running time, space needed, When Does an Algorithm Solve a Problem? A problem instance is a specific task to be solved on a specific input. A problem is a collection of similar problem instances. An algorithm is correct for a given problem instance if it produces the desired output for this instance. An algorithm is correct for a problem P (solves problem P) if it is correct for all instances of P.

2 What Kinds of Problems? Decision problems Given input, does it have a certain property? Is an integer prime? Is a graph planar? (Can be drawn on a plane without overlapping edges?) What Kinds of Problems? Function problems Given input, compute the unique solution. Compute the product of two matrices. Sort an array. What Kinds of Problems? Search problems Given input, compute any one of a set of solutions. Find the factors of an integer. Find shortest path between two points in graph. Find shortest path between each pair of points in graph. Find longest path between two points in graph.

3 Properties of Algorithms Time efficiency As a function of its input size, how long does it take? Space efficiency As a function of its input size, how much additional space does it use? Analysis of Computational Resources Computational resources = CPU time (running time), memory usage (space), messages sent along the network, Resource consumption differs depending on the size of the input (length of text, number of records to be sorted, ) Specify resource consumption as a function of the inputs size. Resource consumption may even differ greatly for inputs of the same size, depending on their structure (highly unsorted input, almost sorted input, ) Properties of Algorithms Simplicity Informal notion Pragmatic benefits How easy to code correctly? Possibly low constant factors in efficiency Idealistic benefit Mathematical elegance

4 Best, Average, and Worst Case The best-case resource consumption is the minimum resource consumption over all possible inputs of a given size. The worst-case resource consumption is the maximum resource consumption over all inputs of a given size. The average-case resource consumption is the average resource consumption over all possible inputs of a given size. Things to Remember Algorithm: Description of a procedure to solve a given problem Model of computation: Specifies permissible operations and their costs An algorithm is correct = produces correct answer for every possible instance of the problem Resource consumption = function of input (size) Best-case, worst-case, average-case analysis Extended Example: Multiplication Illustrates some of course s interesting ideas. Just an overview not too much detail yet.? How to multiply two integers: x y. Common & familiar. Simple?? Suggested algorithms & efficiency??

5 Multiplication Algorithm It s a single basic machine operation, O() time.? Problem with this answer?? Realistic only if we have bounded-sized integers. Instead, assume unbounded-sized integers. E.g., as in Scheme. Explicitly include this assumption. Multiplication Algorithm 2 Repeated addition: x y = x+ +x = y+ +y Back to multiplication: O(n max(x,y)) = O(n 2 n ) y copies x copies How long for addition? Grade-school algorithm takes time number length. Define: n = log 2 (max(x,y)) Logarithm base irrelevant, since O(log c x) = O(log c x) O(n) Multiplication Algorithm 3 Grade-school long-hand algorithm. n multiplications of (x by bit of y) + n additions of the resulting products.? Total? O(n n + n n) = O(n 2 ) Much better. Also approximates hardware algorithms.? x= 38 y=

6 Multiplication Algorithm 4: Part Karatsuba s algorithm, 962 Break the bitstrings of x,y into halves. x = a 2 n/2 + b = a << n/2 + b y = c 2 n/2 + d = c << n/2 + d x=45 y=28 xy = ac << n + (ad+bc) << n/2 + bd Compute the 4 subproducts recursively. Example of a "divide-and-conquer" algorithm. a=5 b= c=3 d=4 Multiplication Algorithm 4: Part Form recurrence equation: How long does this take? k n = T( n) = n 4 T + k n n > 2 Solve recurrence equation. How? Discussed next week. T(n) = O(n 2 ) k = arbitrary constant to fill in for the details 4 recursive subproducts + additions & shifts No better than previous algorithm. Multiplication Algorithm 4: Part 2 Previous: xy = ac << n + (ad+bc) << n/2 + bd Regroup: u = (a+b) (c+d) v = ac w = bd xy = v << n + (u-v-w) << n/2 + w x=45 y=28 a=5 b= c=3 d=4 Only 3 subproducts!

7 Multiplication Algorithm 4: Part 2 How long does this take? k n = T ( n) = n 3 T + k n n > 2 k = a new, larger constant log 2 3 log 3.59 ( n) = 3 k n 2 k n = O( n ) O( n ) T 2 More complicated, but asymptotically faster. Multiplication Algorithm 4: Part 2 Previous: u = (a+b) (c+d) v = ac w = bd xy = v << n + (u-v-w) << n/2 + w An overlooked detail: a+b and c+d can be n/2+ bit numbers. Middle term can overflow into high term. Solution: a+b = a << n/2 + b c+d = c << n/2 + d Break sums into (n/2+) th bit and rest. (a+b) (c+d) = (a c ) << n + (a d + b c ) << n/2 + b d O() time O(n/2) time Multiplication Algorithms 5 & 6 Karp s FFT-based algorithm: O(n log 2 n) Schoenhage & Strassen s FFT-based algorithm, 97: O(n log n log log n) Example of log-shaving slightly better runningtimes with lower logarithmic terms. Best known.

8 Multiplication Algorithms 7,... Use parallelism. Even serial processors use some bit-level parallelism. Divide-&-conquer & FFT algorithms easily parallelized. Beyond the scope of this course. Multiplication Algorithms Summary Higher constant factors more complicated, asymptotically-faster algorithms are slower for typical input sizes. Karatsuba s algorithm somewhat practical: Used on large numbers in cryptography. Generalizes to matrix multiplication Strassen s algorithm, [CLRS] 28.2 Algorithm Analysis Summary. State problem. 2. Characterize the input size. 3. State algorithm. Often difficult, of course. We almost missed an important 4. Prove algorithm correctness. detail that would have produced Necessary! incorrect result. 5. Determine its resource usage. Often via recurrence equations. Allows comparison of algorithms. Can decide which algorithm suitable for given application. Can guide the search for better algorithms.

9 Lists Abstract Data Structures Abstractly, many data structures are simply collections of (key, value) pairs. Typically, interested in some of the following operations Basic Operations Almost always include these. Create : Insert : void collection collection kv void Search : collection key kv A (key,value) pair or a pointer to a pair. Alternatively, just a value.

10 Additional Operations Often include some of these. Min : Max : Pred : Succ : collection kv collection kv collection kv kv collection kv kv Requires keys from a totally ordered set. Union : Delete : collection collection void collection kv void Dictionaries A particularly common set of operations: Create Insert Delete Search Concrete Data Structures Will be looking at concrete data structures & their costs to implement those operations. First, variations of lists: Singly-linked 3,x 6,a,b 2,k Singly-linked & sorted,b 2,k 3,x 6,a Doubly-linked Doubly-linked & sorted 3,x 6,a,b 2,k,b 2,k 3,x 6,a

11 Mutability Reminder Unlike earlier courses, this course emphasizes mutable data structures. Why? Largely tradition. Immutable data structures sometimes less asymptotically efficient. Immutable data structures much less studied. I recommend Purely Functional Data Structures, Okasaki 998. Worst-Case Time Complexities Singly Singly, sorted Doubly Doubly, sorted Let n = collection size. Create Creation almost always O(). Insert n n Linear time to maintain sortedness. Search n n n n Linear search is only option. Singly Singly, sorted Doubly Doubly, sorted Min Max Pred Succ n n n or n n or n n n n or n n or n n or n Sortedness helps. Tail ptr helps if sorted. Must search, unless given ptr to kv as arg & previous ptr points to pred. Must search, unless given ptr to kv as arg.

12 Singly Singly, sorted Doubly Doubly, sorted Union Delete or n n or n n n n or n or n Unsorted append. Sorted merge. Tail ptr helps append. Must search, unless given ptr to kv as arg & have previous ptrs in list. Lessons There are many kinds of lists. To get lower complexities, generally need more space (double-linkage and/or tail pointers) and larger constant factors (maintaining double-linkage). Extra invariants sometimes help, sometimes hurt. Which is best depends on the operations needed. I.e., there are always tradeoffs. Other Concrete Data Structures? What about arrays?? Some advantages, but imposes maximum collection size. But, can occasionally enlargen array efficiently. Requires amortized analysis discussed later. Can combine arrays + lists into hash tables. Discussed later.

13 Other Concrete Data Structures? What about trees?? Again, can be singly- or doubly-linked, unsorted or sorted. Worst-case bounds same as lists. Balancing helps discussed soon Other Concrete Data Structures? What about heaps?? Some operations logarithmic: Insert, either Min or Max, either DeleteMin or DeleteMax. But Union is linear. Will discuss extensions to heaps to improve on this.

14 Elementary Data Structures Stacks, Queues, & Lists Amortized analysis Trees The Stack ADT ( 4.2.) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in first-out scheme Think of a spring-loaded plate dispenser Main stack operations: push(object o): inserts element o pop(): removes and returns the last inserted element Auxiliary stack operations: top(): returns the last inserted element without removing it size(): returns the number of elements stored isempty(): a Boolean value indicating whether no elements are stored Elementary Data Structures 2 Applications of Stacks Direct applications Page-visited history in a Web browser Undo sequence in a text editor Chain of method calls in the Java Virtual Machine or C++ runtime environment Indirect applications Auxiliary data structure for algorithms Component of other data structures Elementary Data Structures 3 Array-based Stack ( 4.2.2) A simple way of implementing the Stack ADT uses an array We add elements from left to right A variable t keeps track of the index of the top element (size is t+) S Algorithm pop(): if isempty() then throw EmptyStackException else t t return S[t +] Algorithm push(o) if t = S.length then throw FullStackException else t t + S[t] o 0 2 t Elementary Data Structures 4 Growable Array-based Stack In a push operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one How large should the new array be? incremental strategy: increase the size by a constant c doubling strategy: double the size Algorithm push(o) if t = S.length then A new array of size for i 0 to t do A[i] S[i] S A t t + S[t] o Elementary Data Structures 5 Comparison of the Strategies We compare the incremental strategy and the doubling strategy by analyzing the total time T(n) needed to perform a series of n push operations We assume that we start with an empty stack represented by an array of size We call amortized time of a push operation the average time taken by a push over the series of operations, i.e., T(n)/n Elementary Data Structures 6

15 Analysis of the Incremental Strategy We replace the array k = n/c times The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + + kc = n + c( k) = n + ck(k + )/2 Since c is a constant, T(n) is O(n + k 2 ),i.e., O(n 2 ) The amortized time of a push operation is O(n) Direct Analysis of the Doubling Strategy We replace the array k = log 2 n times The total time T(n) of a series of n push operations is proportional to n k = n + 2 k + = 2n T(n) is O(n) The amortized time of a push operation is O() geometric series Elementary Data Structures 7 Elementary Data Structures 8 Accounting Method Analysis of the Doubling Strategy The accounting method determines the amortized running time with a system of credits and debits We view a computer as a coin-operated device requiring cyber-dollar for a constant amount of computing. We set up a scheme for charging operations. This is known as an amortization scheme. The scheme must give us always enough money to pay for the actual cost of the operation. The total cost of the series of operations is no more than the total amount charged. (amortized time) (total $ charged) / (# operations) Elementary Data Structures 9 Amortization Scheme for the Doubling Strategy Consider again the k phases, where each phase consisting of twice as many pushes as the one before. At the end of a phase we must have saved enough to pay for the array-growing push of the next phase. At the end of phase i we want to have saved i cyber-dollars, to pay for the array growth for the beginning of the next phase. $ $ $ $ $ $ $ $ We charge $3 for a push. The $2 saved for a regular push are stored in the second half of the array. Thus, we will have 2(i/2)=i cyber-dollars saved at then end of phase i. Therefore, each push runs in O() amortized time; n pushes run in O(n) time. Elementary Data Structures 0 $ $ The Queue ADT ( 4.3.) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front of the queue Main queue operations: enqueue(object o): inserts element o at the end of the queue dequeue(): removes and returns the element at the front of the queue Auxiliary queue operations: front(): returns the element at the front without removing it size(): returns the number of elements stored isempty(): returns a Boolean value indicating whether no elements are stored Exceptions Attempting the execution of dequeue or front on an empty queue throws an EmptyQueueException Applications of Queues Direct applications Waiting lines Access to shared resources (e.g., printer) Multiprogramming Indirect applications Auxiliary data structure for algorithms Component of other data structures Elementary Data Structures Elementary Data Structures 2

16 Singly Linked List Queue with a Singly Linked List A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores element link to the next node elem next node We can implement a queue with a singly linked list The front element is stored at the first node The rear element is stored at the last node The space used is O(n) and each operation of the Queue ADT takes O() time r nodes f A B C D Elementary Data Structures 3 elements Elementary Data Structures 4 List ADT ( 5.2.2) The List ADT models a sequence of positions storing arbitrary objects It allows for insertion and removal in the middle Query methods: isfirst(p), islast(p) Accessor methods: first(), last() before(p), after(p) Update methods: replaceelement(p, o), swapelements(p, q) insertbefore(p, o), insertafter(p, o), insertfirst(o), insertlast(o) remove(p) Elementary Data Structures 5 Doubly Linked List A doubly linked list provides a natural implementation of the List ADT Nodes implement Position and store: element link to the previous node link to the next node Special trailer and header nodes header prev elem nodes/positions elements next node trailer Elementary Data Structures 6 Trees ( 6.) In computer science, a tree is an abstract model Computers R Us of a hierarchical structure A tree consists of nodes with a parent-child relation Sales Manufacturing Applications: Organization charts US International Laptops Desktops File systems Europe Asia Canada Programming environments R&D Tree ADT ( 6..2) We use positions to abstract nodes Generic methods: integer size() boolean isempty() objectiterator elements() positioniterator positions() Accessor methods: position root() position parent(p) positioniterator children(p) Query methods: boolean isinternal(p) boolean isexternal(p) boolean isroot(p) Update methods: swapelements(p, q) object replaceelement(p, o) Additional update methods may be defined by data structures implementing the Tree ADT Elementary Data Structures 7 Elementary Data Structures 8

17 Preorder Traversal ( 6.2.3) A traversal visits the nodes of a tree in a systematic manner In a preorder traversal, a node is visited before its descendants Application: print a structured document Make Money Fast! Motivations 2. Methods References 3 4. Greed.2 Avidity Stock Fraud Algorithm preorder(v) visit(v) for each child w of v preorder (w) 2.2 Ponzi Scheme 2.3 Bank Robbery Elementary Data Structures 9 Postorder Traversal ( 6.2.4) In a postorder traversal, a node is visited after its descendants Application: compute space used by files in a directory and its subdirectories 9 cs6/ hc.doc 3K 3 homeworks/ hnc.doc 2K DDR.java 0K Algorithm postorder(v) for each child w of v postorder (w) visit(v) 7 programs/ Stocks.java 25K Robot.java 20K todo.txt K Elementary Data Structures 20 8 Amortized Analysis of Tree Traversal Time taken in preorder or postorder traversal of an n-node tree is proportional to the sum, taken over each node v in the tree, of the time needed for the recursive call for v. The call for v costs $(c v + ), where c v is the number of children of v For the call for v, charge one cyber-dollar to v and charge one cyber-dollar to each child of v. Each node (except the root) gets charged twice: once for its own call and once for its parent s call. Therefore, traversal time is O(n). Elementary Data Structures 2 Binary Trees ( 6.3) A binary tree is a tree with the following properties: Each internal node has two children The children of a node are an ordered pair We call the children of an internal node left child and right child Alternative recursive definition: a binary tree is either a tree consisting of a single node, or a tree whose root has an ordered pair of children, each of which is a binary tree D Applications: arithmetic expressions decision processes searching Elementary Data Structures 22 B H E I A F C G Arithmetic Expression Tree Binary tree associated with an arithmetic expression internal nodes: operators external nodes: operands Example: arithmetic expression tree for the expression (2 (a ) + (3 b)) 2 a + 3 b Decision Tree Binary tree associated with a decision process internal nodes: questions with yes/no answer external nodes: decisions Example: dining decision Yes How about coffee? Want a fast meal? On expense account? Starbucks In N Out Antoine's Denny s No Yes No Yes No Elementary Data Structures 23 Elementary Data Structures 24

18 Properties of Binary Trees Notation n number of nodes e number of external nodes i number of internal nodes h height Properties: e = i + n = 2e h i h (n )/2 e 2 h h log 2 e h log 2 (n + ) Inorder Traversal In an inorder traversal a node is visited after its left subtree and before its right subtree Application: draw a binary tree x(v) = inorder rank of v y(v) = depth of v Algorithm inorder(v) if isinternal (v) inorder (leftchild (v)) visit(v) if isinternal (v) inorder (rightchild (v)) Elementary Data Structures 25 Elementary Data Structures 26 Euler Tour Traversal Generic traversal of a binary tree Includes a special cases the preorder, postorder and inorder traversals Walk around the tree and visit each node three times: on the left (preorder) from below (inorder) on the right (postorder) + L R B Elementary Data Structures 27 Printing Arithmetic Expressions 2 Specialization of an inorder traversal print operand or operator when visiting node print ( before traversing left subtree print ) after traversing right subtree a + 3 b Algorithm printexpression(v) if isinternal (v) print( ( ) inorder (leftchild (v)) print(v.element ()) if isinternal (v) inorder (rightchild (v)) print ( ) ) ((2 (a )) + (3 b)) Elementary Data Structures 28 Linked Data Structure for Representing Trees ( 6.4.3) A node is represented by an object storing Element Parent node Sequence of children nodes Node objects implement the Position ADT A C B D E F B A D F C E Elementary Data Structures 29 Linked Data Structure for Binary Trees ( 6.4.2) A node is represented by an object storing Element Parent node Left child node Right child node Node objects implement the Position ADT A B C D E A B C Elementary Data Structures 30 D E

19 Array-Based Representation of Binary Trees ( 6.4.) nodes are stored in an array A 2 3 let rank(node) be defined as follows: rank(root) = if node is the left child of parent(node), rank(node) = 2*rank(parent(node)) if node is the right child of parent(node), E F C J rank(node) = 2*rank(parent(node))+ 0 Elementary Data Structures 3 B G H D

20 Introduction to complexity Complexity: a measure of the performance of an algorithm An algorithm s performance depends on internal and external factors Internal The algorithm s efficiency, in terms of: Time required to run Space (memory storage) required to run External Size of the input to the algorithm Speed of the computer on which it is run Quality of the compiler Complexity measures the internal factors (usually more interested in time than space) CSM Complexity 2 Growth rates and big-o notation Growth rates capture the essence of an algorithm s performance Big-O notation indicates the growth rate. It is the class of mathematical formula that best describes an algorithm s performance, and is discovered by looking inside the algorithm Big-O is a function with parameter N, where N is usually the size of the input to the algorithm For example, if an algorithm depending on the value n has performance an 2 + bn + c (for constants a, b, c) then we say the algorithm has performance O(N 2 ) For large N, the N 2 term dominates. Only the dominant term is included in big-o CSM Complexity 3

21 Time Common growth rates Time complexity O() constant O(log N) log O(N) linear O(N log N) n-log-n O(N 2 ) quadratic O(N 3 ) cubic O(2 N ) exponential Example Adding to the front of a linked list Finding an entry in a sorted array Finding an entry in an unsorted array Sorting n items by divide-and-conquer Shortest path between two nodes in a graph Simultaneous linear equations The Towers of Hanoi problem CSM Complexity 4 Growth rates O(N 2 ) O(Nlog N) For a short time N 2 is better than NlogN Number of Inputs CSM Complexity 5 Calculating the actual time taken by a program (example) A program takes 0ms to process one data item (i.e. to do one operation on the data item) How long would the program take to process 000 data items, if time is proportional to: log 0 N N N log 0 N N 2 N 3 (time for item) x (big-o( ) time complexity of N items) CSM Complexity 6 2

22 Best, average, worst-case complexity In some cases, it is important to consider the best, worst and/or average (or typical) performance of an algorithm: Worst, O(N) or o(n): or > true function * Typical, Θ(N): true function * Best, Ω(N): true function * E.g., when sorting a list into order, if it is already in order then the algorithm may have very little work to do The worst-case analysis gives a bound for all possible input (and may be easier to calculate than the average case) * These approximations are true only after N has passed some value CSM Complexity 7 How do we calculate big-o? Five guidelines for finding out the time complexity of a piece of code Loops 2 Nested loops 3 Consecutive statements 4 If-then-else statements 5 Logarithmic complexity CSM Complexity 8 Guideline : Loops The running time of a loop is, at most, the running time of the statements inside the loop (including tests) multiplied by the number of iterations. executed n times for (i=; i<=n; i++) { m = m + 2; } constant time Total time = a constant c * n = cn = O(N) CSM Complexity 9 3

23 Guideline 2: Nested loops Analyse inside out. Total running time is the product of the sizes of all the loops. outer loop executed n times for (i=; i<=n; i++) { for (j=; j<=n; j++) { k = k+; } } constant time inner loop executed n times Total time = c * n * n * = cn 2 = O(N 2 ) CSM Complexity 0 Guideline 3: Consecutive statements Add the time complexities of each statement. constant time constant time outer loop executed n times x = x +; for (i=; i<=n; i++) { m = m + 2; } for (i=; i<=n; i++) { for (j=; j<=n; j++) { k = k+; } constant time } Total time = c 0 + c n + c 2 n 2 = O(N 2 ) executed n times inner loop executed n times CSM Complexity Guideline 4: If-then-else statements Worst-case running time: the test, plus either the then part or the else part (whichever is the larger). test: constant another if : constant + constant (no else part) if (depth( )!= otherstack.depth( ) ) { return false; } else { for (int n = 0; n < depth( ); n++) { if (!list[n].equals(otherstack.list[n])) return false; } } then part: constant else part: (constant + constant) * n Total time = c 0 + c + (c 2 + c 3 ) * n = O(N) CSM Complexity 2 4

24 Guideline 5: Logarithmic complexity An algorithm is O(log N) if it takes a constant time to cut the problem size by a fraction (usually by ½) Example algorithm (binary search): finding a word in a dictionary of n pages Look at the centre point in the dictionary Is word to left or right of centre? Repeat process with left or right part of dictionary until the word is found CSM Complexity 3 Performance isn t everything! There can be a tradeoff between: Ease of understanding, writing and debugging Efficient use of time and space So, maximum performance is not always desirable However, it is still useful to compare the performance of different algorithms, even if the optimal algorithm may not be adopted CSM Complexity 4 5

25 Binary Search Trees The Dictionary Data Structure A dictionary stores a dynamically changing set S and supports the following operations: Insert(x): Insert element x into S Delete(x): Removes element x from S Find(x): Decides whether x is in S and, if so, reports x If the elements in S can be compared, the following operations are also desirable: Successor(x) / Predecessor(x): Reports the next larger / smaller element from x in S Minimum / Maximum: Reports the smallest / largest element in S Range-Search(x, y): Reports all elements z in S such that x z y Binary Search Tree Property A binary search tree is a binary tree with the following binary search tree property: For any node v, the elements stored in the left subtree of v are not greater than the element stored at v; the elements in the right subtree are not less than the element stored at v

26 The Find Operation Searching for an Element Goal: Identify the node storing a given element x, if such a node exists x = 25 The Find Operation Searching for an Element Goal: Identify the node storing a given element x, if such a node exists Find(x, T) v root(t) 2 while v nil and x key(v) 3 do if x < key(v) 4 then v left_child(v) 5 else v right_child(v) 6 return v x = 25 The Find Operation Searching for an Element Goal: Identify the node storing a given element x, if such a node exists Lemma: If element x is stored in T, procedure Find finds the node storing x Lemma: The running time of procedure Find is O(height(T)). x = 25 2

27 The Minimum Operation Finding the Smallest Element Goal: Find the smallest element in S The Minimum Operation Finding the Smallest Element Goal: Find the smallest element in S The Minimum Operation Finding the Smallest Element Goal: Find the smallest element in S

28 The Minimum Operation Finding the Smallest Element Goal: Find the smallest element in S Minimum(T) v root(t) 2 if v nil 3 then return Subtree-Minimum(v) 4 else return nil Subtree-Minimum(v) while left_child(v) nil 2 do v left_child(v) 3 return v Lemma: The running time of procedure Minimum is O(height(T)). The Successor Operation Finding the Next Element Goal: Given an element x S, find the next larger element y S. Successor(v) if right_child(v) nil 2 then return Subtree-Minimum(right_child(v)) 3 w parent(v) 4 while w nil and v = right(w) 5 do v w 6 w parent(w) 3 7 return w The Successor Operation Finding the Next Element Goal: Given an element x S, find the next larger element y S. Successor(v) if right_child(v) nil 2 then return Subtree-Minimum(right_child(v)) 3 w parent(v) 4 while w nil and v = right(w) 5 do v w 6 w parent(w) 7 return w Lemma: The running time of procedure Successor is O(height(T)). 4

29 The Insert Operation Inserting a New Element Goal: Insert a new element x into the dictionary The Insert Operation Inserting a New Element Goal: Insert a new element x into the dictionary Insert(x, T) v root(t) 2 w nil 3 while v nil 4 do w v 5 if x < key(v) 6 then v left_child(v) 7 else v right_child(v) 8 u new node with key x 9 if w = nil 0 then root(t) u else parent(u) w 2 if x < key(w) 3 then left_child(w) u 4 else right_child(w) u The Insert Operation Inserting a New Element Goal: Insert a new element x into the dictionary. Lemma: Procedure Insert preserves the binary search tree property. Lemma: The running time of procedure Insert is O(height(T)). Insert(x, T) v root(t) 2 w nil 3 while v nil 4 do w v 5 if x < key(v) 6 then v left_child(v) 7 else v right_child(v) 8 u new node with key x 9 if w = nil 0 then root(t) u else parent(u) w 2 if x < key(w) 3 then left_child(w) u 4 else right_child(w) u 5

30 The Delete Operation Deleting Element Goal: Delete an element x from the dictionary The Delete Operation Deleting Element Goal: Delete an element x from the dictionary The Delete Operation Deleting Element Delete(v, T) if left_child(v) = nil or right_child(v) = nil 2 then w v 3 else w Successor(v) 4 if left_child(w) nil 5 then u left_child(w) 6 else u right_child(w) 7 if u nil 8 then parent(u) parent(w) 9 if parent(w) = nil 0 then root(t) u else if w = left_child(parent(w)) 2 then left_child(parent(w)) u 3 else right_child(parent(w)) u 4 if w v 5 then key(v) key(w) 6 Delete node w Goal: Delete an element x from the dictionary

31 The Delete Operation Deleting Element Delete(v, T) if left_child(v) = nil or right_child(v) = nil 2 then w v 3 else w Successor(v) 4 if left_child(w) nil 5 then u left_child(w) 6 else u right_child(w) 7 if u nil 8 then parent(u) parent(w) 9 if parent(w) = nil 0 then root(t) u else if w = left_child(parent(w)) 2 then left_child(parent(w)) u 3 else right_child(parent(w)) u 4 if w v 5 then key(v) key(w) 6 Delete node w Goal: Delete an element x from the dictionary. Lemma: The running time of procedure Delete is O(height(T)). Lemma: Procedure Delete preserves the binary search tree property. Binary Search Tree Operations Summary Theorem: A binary search tree supports operations Insert, Delete, Find, Minimum, Maximum, Predecessor, and Successor in O(height(T)) time. Best case: Tree is complete Height is Θ(lg n) Worst case: Tree is a linked list Height is Θ(n) 7

32 AVL Trees (See extra AVL notes too) AVL Trees A type of binary search tree which is nearly as good as a balanced tree for time complexity of the operations, and whose structure we can maintain as insertions and deletions proceed, is the AVL tree AVL trees are named after the Russian mathematicians G.M. Adelson-Velskii and E.M. Landis, who discovered them in 962 An AVL tree is a binary search tree in which the heights of the left and right subtrees of the root differ by at most, and in which the left and right subtrees of the root are again AVL trees Cx220 - Other Trees 2 Examples of AVL Trees AVL Non-Legal AVL AVL and balanced Legal AVL, but not balanced Cx220 - Other Trees 3

33 Implementing AVL Trees To implement algorithms for inserting and deleting from an AVL tree, we associate a balance factor with each node This is left high, right high or equal depending on whether the left subtree of the node has height greater than, less than or equal to that of the right subtree Thus the node may be represented by: public class AVLNode { Comparable data; int balancefactor; AVLNode left; AVLNode right; } public static final int LEFT_HIGH = -; public static final int RIGHT_HIGH = ; public static final int EQUAL_HIGH = 0; Cx220 - Other Trees 4 Examples of AVL Trees with Balance Factors R L E At L, the left subtree is higher than the right At R, the right subtree is higher than the left At E, the subtrees are equal (in fact, they are empty) E R R R E R R R E E E R (AVL trees can be quite skewed) E Cx220 - Other Trees 5 A Non-AVL Tree (height difference is more than one in places) L LL E RR R * Note that no AVL tree will ever have two double imbalances, like this, at the same time - we ll see why later. This is to illustrate how a tree can be unbalanced somewhere other than the root E E LL means that the left subtree is too high RR means that the right subtree is too high Cx220 - Other Trees 6 2 2

34 Insertion into an AVL Tree (the simple case) Consider constructing a binary search tree in the usual way from the sequence of keys 4, 7, 2, 8, 6,, 5, making each node with its balance factor as we go: 4:E 4:R 7:E 4:E 2:E 7:E 4:R 2:E 7:R 4:R 2:E 7:E 8:E 6:E 8:E Cx220 - Other Trees 7 Insertion contd (inserting, 5) 4:E 2:L 7:E :E 6:E 8:E :E 4:R 2:L 7:L 6:L 8:E 5:E This example shows how the balance factors change as new nodes are added - notice the changes are only on the path from new node to root node Cx220 - Other Trees 8 The Insertion Algorithm (for more complex cases) Insertion starts as for a binary search tree The only case in which we need to do anything special is if the new node is added to a subtree of the root that is higher than the other subtree and the new node increases the height of the subtree In this case we have to carry out some balancing operations in the neighbourhood of the root We consider the case of inserting into the right subtree (left is similar) There are two cases to consider: Cx220 - Other Trees 9 3 3

35 right hand subtree of root is right high after insertion 4:R 2:E 7:E 6:E R 8:E R 9:E Inserting 9 causes 4:R -> 4:RR Perform a left rotation 7:E 4:E 8:R 2:E 6:E 9:E The 6 has been relocated as a child of 4. The nodes , which comprise the outside edge of the tree, have been rotated one node to the left Cx220 - Other Trees 0 2 right hand subtree of root is left high after insertion 4:R 2:E 2 7:E 5 6:E L 8:E Inserting 5 Perform a double rotation right rotation of right subtree ( in this example) tree now like previous slide left rotation of whole tree, as 6:E before 4:E 7:R 2:E 5:E 8:E left hand child of right hand child of root becomes root left hand child of new root becomes right hand child of old root Cx220 - Other Trees The balancing rule: When you have found the node that is out of balance: If that node and the two nodes immediately below it are in a straight line then a single rotation is needed If the nodes lie in a dog-leg pattern, then you need a double rotation to restore the balance Although the algorithm seems complicated, it still gives insertion in O(log n) time Note: there is no LR or RL imbalance, only LL and RR. The LL means the subtree to the left of the node is too deep (the same with RR by symmetry). It does not refer to the pattern of the imbalance at all! Cx220 - Other Trees 2 4 4

36 Another example From "An introduction to data structures & algorithms with Java, G.W. Rowe Build an AVL tree using the following data: 0, 20, 30, 25, 27, 7, 4 0:E 0:R 0:RR 20:E 20:R 30:E The problem lies on a straight line, so apply single rotation Cx220 - Other Trees 3 Another example (contd.) 20:E 20: 0:E 30:E 0:E 30:LL AVL structure is now restored 25:R 27:E After inserting 25 and 27, node at 30 becomes unbalanced Cx220 - Other Trees 4 Rebalance by a double rotation 20: 20:R 0:E 30:LL 0:E 27:E 27:L 25:E 30:E 25:E First part of double rotation rotates 27 to replace 25 Second part of rotation moves 27 up to replace 30 Cx220 - Other Trees 5 5 5

37 Inserting 7, 4 20:E 0:L 27:E 7:E 25:E 30:E Inserting 7 causes no problem 4:E 7:L 0:LL 20: 27:E 25:E 30:E Inserting 4 causes a problem - it s a straight-line single rotation Cx220 - Other Trees 6 Rebalance by a single rotation 20:E 7:E 27:E 4:E 0:E 25:E 30:E Everything is nicely balanced again Cx220 - Other Trees 7 Applications of Binary Trees Sorting: We can sort data by reading it in, item by item, and constructing a binary search tree as we go When we have read all the data, we output it by doing in-order traversal of the tree If there is any possibility that the data items are nearly sorted already, or nearly in reverse order, then it is important to use an AVL tree Otherwise you end up with a very unbalanced tree! The time complexity of this method of sorting is, in general, O(nlog 2 n + n) i.e., O(nlog n) Cx220 - Other Trees 8 6 6

38 Searching maintaining an order Consider the problem of counting how many times each word appears in a piece of text (a concordance): Read the text and build a binary search tree in which the key is a word (order is alphabetical) and the data is the number of times it occurs As we meet each word, we check to see whether it's already in the tree If it is, we add one to the data field; otherwise we insert a new node with the word as key and with the data field set to one (No need to use an AVL tree because the words will not occur in alphabetical order) When we've finished, an in-order traversal allows us to print the words, with their frequencies, in alphabetical order Cx220 - Other Trees 9 Compiling arithmetic expressions We can represent an arithmetic expression such as (A + B) * (C + D) * 2 X / Y as a binary tree, in which the leaves are constants or variables and the nodes are operations: A + 3 * B * / 2 X Y + 2 A post order traversal then gives us the order in which the operations have C D to be carried out Cx220 - Other Trees

39 Elementary Graph Algorithms Paths and Cycles A path is a sequence of vertices P = (v 0, v,, v k ) such that, for i k, edge (v i, v i ) E. Path P is simple if no vertex appears more than once in P. A cycle is a sequence of vertices C = (v 0, v,, v k ) such that, for 0 i < k, edge (v i, v (i + ) mod k ) E. Cycle C is simple if the path (v 0, v, v k ) is simple. g f d i e a b c h j Paths and Cycles A path is a sequence of vertices P = (v 0, v,, v k ) such that, for i k, edge (v i, v i ) E. Path P is simple if no vertex appears more than once in P. A cycle is a sequence of vertices C = (v 0, v,, v k ) such that, for 0 i < k, edge (v i, v (i + ) mod k ) E. Cycle C is simple if the path (v 0, v, v k ) is simple. P = (g, d, e, b, d, a, h) is not simple. g f d i P 2 = (f, i, c, j) is simple. e a b c h j

40 Paths and Cycles A path is a sequence of vertices P = (v 0, v,, v k ) such that, for i k, edge (v i, v i ) E. Path P is simple if no vertex appears more than once in P. A cycle is a sequence of vertices C = (v 0, v,, v k ) such that, for 0 i < k, edge (v i, v (i + ) mod k ) E. Cycle C is simple if the path (v 0, v, v k ) is simple. C = (a, h, j, c, i, f, g, d) is simple. g f d i e a b c h j Paths and Cycles A path is a sequence of vertices P = (v 0, v,, v k ) such that, for i k, edge (v i, v i ) E. Path P is simple if no vertex appears more than once in P. A cycle is a sequence of vertices C = (v 0, v,, v k ) such that, for 0 i < k, edge (v i, v (i + ) mod k ) E. Cycle C is simple if the path (v 0, v, v k ) is simple. C = (a, h, j, c, i, f, g, d) is simple. g f d i C 2 = (g, d, b, h, j, c, i, e, b) is not simple. e a b c h j Subgraphs A graph H = (W, F) is a subgraph of a graph G = (V, E) if W V and F E. 2

41 Subgraphs A graph H = (W, F) is a subgraph of a graph G = (V, E) if W V and F E. Spanning Graphs A spanning graph of G is a subgraph of G that contains all vertices of G. Spanning Graphs A spanning graph of G is a subgraph of G that contains all vertices of G. 3

42 Connectivity A graph G is connected if there is a path between any two vertices in G. The connected components of a graph are its maximal connected subgraphs. Trees A tree is a graph that is connected and contains no cycles. Spanning Trees A spanning tree of a graph is a spanning graph that is a tree. 4

43 Adjacency List Representation of a Graph List of vertices Pointer to head of adjacency list List of edges Pointers to adjacency list entries Adjacency list entry Pointer to edge y Pointers to endpoints a y d c w v u c e u x a b x z b w e v z d Graph Exploration Goal: Visit all the vertices in the graph of G Count them Number them Identify the connected components of G Compute a spanning tree of G Graph Exploration ExploreGraph(G) Label every vertex and edge of G as unexplored 2 for every vertex v of G 3 do if v is unexplored 4 then mark v as visited 5 S Adj(v) 6 while S is not empty 7 do remove an edge (u, w) from S 8 if (u, w) is unexplored 9 then if w is unexplored 0 then mark edge (u, w) as tree edge mark vertex w as visited 2 S S Adj(w) 3 else mark edge (u, w) as a non-tree edge 5

44 Properties of ExploreGraph Theorem: Procedure ExploreGraph visits every vertex of G. Theorem: Every iteration of the for-loop that does not immediately terminate completely explores a connected component of G. Theorem: The graph defined by the tree edges is a spanning forest of G. Connected Components ConnectedComponents(G) Label every vertex and edge of G as unexplored 2 c 0 3 for every vertex v of G 4 do if v is unexplored 5 then c c + 6 assign component label c to v 7 S Adj(v) 8 while S is not empty 9 do remove an edge (u, w) from S 0 if (u, w) is unexplored then if w is unexplored 2 then mark edge (u, w) as tree edge 3 assign component label c to w 4 S S Adj(w) 5 else mark edge (u, w) as a non-tree edge Breadth-First Search BFS(G) Label every vertex and edge of G as unexplored 2 for every vertex v of G 3 do if v is unexplored 4 then mark v as visited Running time: O(n + m) 5 S Adj(v) 6 while S is not empty 7 do (u, w) Dequeue(S) 8 if (u, w) is unexplored 9 then if w is unexplored 0 then mark edge (u, w) as tree edge mark vertex w as visited 2 Enqueue(S, Adj(w)) 3 else mark edge (u, w) as a non-tree edge 6

45 Properties of Breadth-First Search Theorem: Breadth-first search visits the vertices of G by increasing distance from the root. Theorem: For Levery 0 edge (v, w) in G such that L 4 v L i and w L j, i j. L L 3 L 2 Depth-First Search DFS(G) Label every vertex and edge of G as unexplored 2 for every vertex v of G 3 do if v is unexplored 4 then mark v as visited Running time: O(n + m) 5 S Adj(v) 6 while S is not empty 7 do (u, w) Pop(S) 8 if (u, w) is unexplored 9 then if w is unexplored 0 then mark edge (u, w) as tree edge mark vertex w as visited 2 Push(S, Adj(w)) 3 else mark edge (u, w) as a non-tree edge An Important Property of Depth-First Search Theorem: For every non-tree edge (v, w) in a depth-first spanning tree of an undirected graph, v is an ancestor of w or vice versa. 7

46 A Recursive Depth-First Search Algorithm DFS(G) Label every vertex and every edge of G as unexplored 2 for every vertex v of G 3 do if v is unexplored 4 then DFS(G, v) DFS(G, v) mark vertex v as visited 2 for every edge (v, w) in Adj(v) 3 do if (v, w) is unexplored 4 then if w is unexplored 5 then mark edge (v, w) as tree edge 6 DFS(G, w) 7 else mark edge (v, w) as a non-tree edge 8

Elementary Data Structures. Stacks, Queues, & Lists Amortized analysis Trees

Elementary Data Structures. Stacks, Queues, & Lists Amortized analysis Trees Elementary Data Structures Stacks, Queues, & Lists Amortized analysis Trees The Stack ADT ( 2.1.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in first-out scheme Think

More information

Trees. 9/21/2007 8:11 AM Trees 1

Trees. 9/21/2007 8:11 AM Trees 1 Trees 9/21/2007 8:11 AM Trees 1 Outline and Reading Tree ADT ( 6.1) Preorder and postorder traversals ( 6.2.3) BinaryTree ADT ( 6.3.1) Inorder traversal ( 6.3.4) Euler Tour traversal ( 6.3.4) Template

More information

Data Structures. Trees, Binary trees & Binary Search Trees

Data Structures. Trees, Binary trees & Binary Search Trees Data Structures Trees, Binary trees & Binary Search Trees Tree In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parentchild relation Applications:

More information

Trees. Make Money Fast! Bank Robbery. Stock Fraud. Ponzi Scheme Goodrich, Tamassia. Trees 1

Trees. Make Money Fast! Bank Robbery. Stock Fraud. Ponzi Scheme Goodrich, Tamassia. Trees 1 Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery Trees 1 What is a Tree In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child

More information

Trees. Make Money Fast! Stock Fraud. Bank Robbery. Ponzi Scheme. Trees Goodrich, Tamassia

Trees. Make Money Fast! Stock Fraud. Bank Robbery. Ponzi Scheme. Trees Goodrich, Tamassia Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery Trees 1 What is a Tree In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child

More information

Trees. Make Money Fast! Bank Robbery. Ponzi Scheme. Stock Fraud. 02/06/2006 Trees 1

Trees. Make Money Fast! Bank Robbery. Ponzi Scheme. Stock Fraud. 02/06/2006 Trees 1 Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery 02/06/2006 Trees 1 Outline and Reading Tree ADT ( 7.1.2) Preorder and postorder traversals ( 7.2.2-3) BinaryTree ADT ( 7.3.1) Inorder traversal

More information

Trees. Make Money Fast! Bank Robbery. Ponzi Scheme. Stock Fraud Goodrich, Tamassia, Goldwasser Trees

Trees. Make Money Fast! Bank Robbery. Ponzi Scheme. Stock Fraud Goodrich, Tamassia, Goldwasser Trees Part 4: Trees Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery 2013 Goodrich, Tamassia, Goldwasser Trees 2 Example: Family Tree 2013 Goodrich, Tamassia, Goldwasser Trees 3 Example: Unix File

More information

Trees and Binary Trees

Trees and Binary Trees Trees and Binary Trees Become Rich Force Others to be Poor Rob Banks Stock Fraud The class notes are a compilation and edition from many sources. The instructor does not claim intellectual property or

More information

Trees. Make Money Fast! Bank Robbery. Stock Fraud. Ponzi Scheme. Trees Goodrich, Tamassia

Trees. Make Money Fast! Bank Robbery. Stock Fraud. Ponzi Scheme. Trees Goodrich, Tamassia Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery Trees 1 What is a Tree In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child

More information

Trees. Make Money Fast! Bank Robbery. Ponzi Scheme. Stock Fraud. 1/18/2005 4:17 AM Trees 1

Trees. Make Money Fast! Bank Robbery. Ponzi Scheme. Stock Fraud. 1/18/2005 4:17 AM Trees 1 Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery 1/18/2005 4:17 AM Trees 1 Outline and Reading Tree ADT ( 2.3.1) Preorder and postorder traversals ( 2.3.2) BinaryTree ADT ( 2.3.3) Inorder traversal

More information

Data Structures and Algorithms. Trees

Data Structures and Algorithms. Trees Data Structures and Algorithms Trees Tree Example Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery 2 What is a Tree In computer science, a tree is an abstract model of a hierarchical structure A

More information

Trees. Trees. CSE 2011 Winter 2007

Trees. Trees. CSE 2011 Winter 2007 Trees CSE 2011 Winter 2007 2/5/2007 10:00 PM 1 Trees Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

More information

Unit 1 (9Hrs) Trees. Mahesh Sanghavi & Deepali Pawar Department of Computer Engineering, SNJB s KBJ College of Engineering, Chandwad, India

Unit 1 (9Hrs) Trees. Mahesh Sanghavi & Deepali Pawar Department of Computer Engineering, SNJB s KBJ College of Engineering, Chandwad, India Unit 1 (9Hrs) Trees Mahesh Sanghavi & Deepali Pawar Department of Computer Engineering, SNJB s KBJ College of Engineering, Chandwad, India The class notes are a compilation and edition from many sources.

More information

Stock Fraud. Nancy Amato. Parasol Lab, Dept. CSE, Texas A&M University

Stock Fraud. Nancy Amato. Parasol Lab, Dept. CSE, Texas A&M University Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery Chapter 7: Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data Structures

More information

Outline. Definitions Traversing trees Binary trees

Outline. Definitions Traversing trees Binary trees Trees Chapter 8 Outline Definitions Traversing trees Binary trees Outline Definitions Traversing trees Binary trees Graph a b Node ~ city or computer Edge ~ road or data cable c Undirected or Directed

More information

Trees 3/19/14. Mammal. Dog Pig Cat

Trees 3/19/14. Mammal. Dog Pig Cat Presentation for use with the textbook ata Structures and lgorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 014 Trees Mammal og Pig at Trees 1 What is a Tree

More information

OUTLINE. General Trees (Ch. 7.1) Binary Trees (Ch. 7.3) Tree Traversals (Ch. 7.2)

OUTLINE. General Trees (Ch. 7.1) Binary Trees (Ch. 7.3) Tree Traversals (Ch. 7.2) CH 7 : TREE ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM NANCY M. AMATO 1 OUTLINE

More information

Ch02 Data Structures

Ch02 Data Structures Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 Ch02 Data Structures xkcd Seven http://xkcd.com/1417/ Used with permission under

More information

Trees. Data structures and Algorithms. Make Money Fast! Bank Robbery. Stock Fraud. Ponzi Scheme

Trees. Data structures and Algorithms. Make Money Fast! Bank Robbery. Stock Fraud. Ponzi Scheme Make Money Fast! Trees Stock Fraud Ponzi Scheme Bank Robbery Data structures and Algorithms Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++ Goodrich,

More information

Ch02 Data Structures

Ch02 Data Structures Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 Ch02 Data Structures xkcd Seven http://xkcd.com/1417/ Used with permission under

More information

Data Structures Lecture 6

Data Structures Lecture 6 Fall 2017 Fang Yu Software Security Lab. Dept. Management Information Systems, National Chengchi University Data Structures Lecture 6 Announcement Project Proposal due on Nov. 9 Remember to bring a hardcopy

More information

Topics on the Midterm

Topics on the Midterm Midterm Review Topics on the Midterm Data Structures & Object-Oriented Design Run-Time Analysis Linear Data Structures The Java Collections Framework Recursion Trees Priority Queues & Heaps Maps, Hash

More information

Course Review for Finals. Cpt S 223 Fall 2008

Course Review for Finals. Cpt S 223 Fall 2008 Course Review for Finals Cpt S 223 Fall 2008 1 Course Overview Introduction to advanced data structures Algorithmic asymptotic analysis Programming data structures Program design based on performance i.e.,

More information

In a postorder traversal, a node is visited after its descendants Application: compute space used by files in a directory and its subdirectories 9 1

In a postorder traversal, a node is visited after its descendants Application: compute space used by files in a directory and its subdirectories 9 1 What is a Tree Trees Stock Fraud Make Money Fast! Winning Lotto / Bank Robbery In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes ith a parent-child relation

More information

What is a Tree. Trees. Tree ADT ( 6.1.2) Tree Terminology. subtree. In computer science, a tree is an abstract model

What is a Tree. Trees. Tree ADT ( 6.1.2) Tree Terminology. subtree. In computer science, a tree is an abstract model What is a Tree Trees Stock raud Make Money ast! Ponzi Scheme ank Robbery In computer science, a tree is an abstract model omputers R Us of a hierarchical structure tree consists of with a parent-child

More information

Lecture Summary CSC 263H. August 5, 2016

Lecture Summary CSC 263H. August 5, 2016 Lecture Summary CSC 263H August 5, 2016 This document is a very brief overview of what we did in each lecture, it is by no means a replacement for attending lecture or doing the readings. 1. Week 1 2.

More information

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Chapter 5: Stacks, ueues and Deques Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,

More information

Course Review. Cpt S 223 Fall 2009

Course Review. Cpt S 223 Fall 2009 Course Review Cpt S 223 Fall 2009 1 Final Exam When: Tuesday (12/15) 8-10am Where: in class Closed book, closed notes Comprehensive Material for preparation: Lecture slides & class notes Homeworks & program

More information

INSTITUTE OF AERONAUTICAL ENGINEERING

INSTITUTE OF AERONAUTICAL ENGINEERING INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad - 500 043 COMPUTER SCIENCE AND ENGINEERING TUTORIAL QUESTION BANK Course Name Course Code Class Branch DATA STRUCTURES ACS002 B. Tech

More information

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications Trees 1 Outline Preliminaries What is Tree? Implementation of Trees using C++ Tree traversals and applications Binary Trees Binary Search Trees Structure and operations Analysis 2 What is a Tree? A tree

More information

Course Review. Cpt S 223 Fall 2010

Course Review. Cpt S 223 Fall 2010 Course Review Cpt S 223 Fall 2010 1 Final Exam When: Thursday (12/16) 8-10am Where: in class Closed book, closed notes Comprehensive Material for preparation: Lecture slides & class notes Homeworks & program

More information

The tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo

The tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo The tree data structure 1 Trees COL 106 Amit Kumar Shweta Agrawal Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo 1 Trees The tree data structure 3 A rooted tree data structure stores

More information

Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Cpt S 223 Fall Cpt S 223. School of EECS, WSU Course Review Cpt S 223 Fall 2012 1 Final Exam When: Monday (December 10) 8 10 AM Where: in class (Sloan 150) Closed book, closed notes Comprehensive Material for preparation: Lecture slides & class notes

More information

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES CH4.2-4.3. ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER

More information

CS301 - Data Structures Glossary By

CS301 - Data Structures Glossary By CS301 - Data Structures Glossary By Abstract Data Type : A set of data values and associated operations that are precisely specified independent of any particular implementation. Also known as ADT Algorithm

More information

Data Structures Question Bank Multiple Choice

Data Structures Question Bank Multiple Choice Section 1. Fundamentals: Complexity, Algorthm Analysis 1. An algorithm solves A single problem or function Multiple problems or functions Has a single programming language implementation 2. A solution

More information

Chapter 2: Basic Data Structures

Chapter 2: Basic Data Structures Chapter 2: Basic Data Structures Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority Queues and Heaps Dictionaries and Hash Tables Spring 2014 CS 315 2 Two

More information

CMSC th Lecture: Graph Theory: Trees.

CMSC th Lecture: Graph Theory: Trees. CMSC 27100 26th Lecture: Graph Theory: Trees. Lecturer: Janos Simon December 2, 2018 1 Trees Definition 1. A tree is an acyclic connected graph. Trees have many nice properties. Theorem 2. The following

More information

HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS

HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS SYLLABUS ABSTRACT DATA TYPES (ADTS) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations

More information

CS8391-DATA STRUCTURES QUESTION BANK UNIT I

CS8391-DATA STRUCTURES QUESTION BANK UNIT I CS8391-DATA STRUCTURES QUESTION BANK UNIT I 2MARKS 1.Define data structure. The data structure can be defined as the collection of elements and all the possible operations which are required for those

More information

CS 8391 DATA STRUCTURES

CS 8391 DATA STRUCTURES DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING QUESTION BANK CS 8391 DATA STRUCTURES UNIT- I PART A 1. Define: data structure. A data structure is a way of storing and organizing data in the memory for

More information

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305 Q.1 If h is any hashing function and is used to hash n keys in to a table of size m, where n

More information

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures COMP 3170 - Analysis of Algorithms & Data Structures Shahin Kamali Binary Search Trees CLRS 12.2, 12.3, 13.2, read problem 13-3 University of Manitoba COMP 3170 - Analysis of Algorithms & Data Structures

More information

Outline and Reading. The Stack ADT ( 2.1.1) Applications of Stacks ( 2.1.1) Array-based implementation ( 2.1.1) Growable array-based stack ( 1.

Outline and Reading. The Stack ADT ( 2.1.1) Applications of Stacks ( 2.1.1) Array-based implementation ( 2.1.1) Growable array-based stack ( 1. Stacks Outline and Reading The Stack ADT ( 2.1.1) Applications of Stacks ( 2.1.1) Array-based implementation ( 2.1.1) Growable array-based stack ( 1.5) Stacks 2 Abstract Data Types (ADTs) An abstract data

More information

Department of Computer Science and Technology

Department of Computer Science and Technology UNIT : Stack & Queue Short Questions 1 1 1 1 1 1 1 1 20) 2 What is the difference between Data and Information? Define Data, Information, and Data Structure. List the primitive data structure. List the

More information

CS8391-DATA STRUCTURES

CS8391-DATA STRUCTURES ST.JOSEPH COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERI NG CS8391-DATA STRUCTURES QUESTION BANK UNIT I 2MARKS 1.Explain the term data structure. The data structure can be defined

More information

E.G.S. PILLAY ENGINEERING COLLEGE (An Autonomous Institution, Affiliated to Anna University, Chennai) Nagore Post, Nagapattinam , Tamilnadu.

E.G.S. PILLAY ENGINEERING COLLEGE (An Autonomous Institution, Affiliated to Anna University, Chennai) Nagore Post, Nagapattinam , Tamilnadu. 17CA 104DATA STRUCTURES Academic Year : 018-019 Programme : MCA Year / Semester : I / I Question Bank Course Coordinator: Mrs. C.Mallika Course Objectives The student should be able to 1. To understand

More information

DDS Dynamic Search Trees

DDS Dynamic Search Trees DDS Dynamic Search Trees 1 Data structures l A data structure models some abstract object. It implements a number of operations on this object, which usually can be classified into l creation and deletion

More information

Lecture 6: Analysis of Algorithms (CS )

Lecture 6: Analysis of Algorithms (CS ) Lecture 6: Analysis of Algorithms (CS583-002) Amarda Shehu October 08, 2014 1 Outline of Today s Class 2 Traversals Querying Insertion and Deletion Sorting with BSTs 3 Red-black Trees Height of a Red-black

More information

CSCI2100B Data Structures Trees

CSCI2100B Data Structures Trees CSCI2100B Data Structures Trees Irwin King king@cse.cuhk.edu.hk http://www.cse.cuhk.edu.hk/~king Department of Computer Science & Engineering The Chinese University of Hong Kong Introduction General Tree

More information

CS 270 Algorithms. Oliver Kullmann. Binary search. Lists. Background: Pointers. Trees. Implementing rooted trees. Tutorial

CS 270 Algorithms. Oliver Kullmann. Binary search. Lists. Background: Pointers. Trees. Implementing rooted trees. Tutorial Week 7 General remarks Arrays, lists, pointers and 1 2 3 We conclude elementary data structures by discussing and implementing arrays, lists, and trees. Background information on pointers is provided (for

More information

CSci 231 Final Review

CSci 231 Final Review CSci 231 Final Review Here is a list of topics for the final. Generally you are responsible for anything discussed in class (except topics that appear italicized), and anything appearing on the homeworks.

More information

Course goals. exposure to another language. knowledge of specific data structures. impact of DS design & implementation on program performance

Course goals. exposure to another language. knowledge of specific data structures. impact of DS design & implementation on program performance Course goals exposure to another language C++ Object-oriented principles knowledge of specific data structures lists, stacks & queues, priority queues, dynamic dictionaries, graphs impact of DS design

More information

Table of Contents. Chapter 1: Introduction to Data Structures... 1

Table of Contents. Chapter 1: Introduction to Data Structures... 1 Table of Contents Chapter 1: Introduction to Data Structures... 1 1.1 Data Types in C++... 2 Integer Types... 2 Character Types... 3 Floating-point Types... 3 Variables Names... 4 1.2 Arrays... 4 Extraction

More information

Section 05: Solutions

Section 05: Solutions Section 05: Solutions 1. Memory and B-Tree (a) Based on your understanding of how computers access and store memory, why might it be faster to access all the elements of an array-based queue than to access

More information

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 The data of the problem is of 2GB and the hard disk is of 1GB capacity, to solve this problem we should Use better data structures

More information

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology Introduction Chapter 4 Trees for large input, even linear access time may be prohibitive we need data structures that exhibit average running times closer to O(log N) binary search tree 2 Terminology recursive

More information

Summer Final Exam Review Session August 5, 2009

Summer Final Exam Review Session August 5, 2009 15-111 Summer 2 2009 Final Exam Review Session August 5, 2009 Exam Notes The exam is from 10:30 to 1:30 PM in Wean Hall 5419A. The exam will be primarily conceptual. The major emphasis is on understanding

More information

Lecture 3 Linear Data Structures: Arrays, Array Lists, Stacks, Queues and Linked Lists

Lecture 3 Linear Data Structures: Arrays, Array Lists, Stacks, Queues and Linked Lists Lecture 3 Linear Data Structures: Arrays, Array Lists, Stacks, Queues and Linked Lists Chapters 3.1-3.3, 5.1-5.2, 6.1-1 - Core Collection Interfaces - 2 - The Java Collections Framework Interface Abstract

More information

Section 05: Solutions

Section 05: Solutions Section 05: Solutions 1. Asymptotic Analysis (a) Applying definitions For each of the following, choose a c and n 0 which show f(n) O(g(n)). Explain why your values of c and n 0 work. (i) f(n) = 5000n

More information

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May www.jwjobs.net R10 SET - 1 II B. Tech I Semester, Supplementary Examinations, May - 2012 (Com. to CSE, IT, ECC ) Time: 3 hours Max Marks: 75 *******-****** 1. a) Which of the given options provides the

More information

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n) CSE 0 Test Your name as it appears on your UTA ID Card Fall 0 Multiple Choice:. Write the letter of your answer on the line ) to the LEFT of each problem.. CIRCLED ANSWERS DO NOT COUNT.. points each. The

More information

Lists and Sequences. 1/18/2005 4:12 AM Sequences 1

Lists and Sequences. 1/18/2005 4:12 AM Sequences 1 Lists and Sequences /8/2005 4:2 AM Sequences Outline and Reading Singly linked list Position ADT and List ADT ( 2.2.2) Doubly linked list ( 2.2.2) Sequence ADT ( 2.2.3) Implementations of the sequence

More information

Fundamentals of Data Structure

Fundamentals of Data Structure Fundamentals of Data Structure Set-1 1. Which if the following is/are the levels of implementation of data structure A) Abstract level B) Application level C) Implementation level D) All of the above 2.

More information

Lecture 13: AVL Trees and Binary Heaps

Lecture 13: AVL Trees and Binary Heaps Data Structures Brett Bernstein Lecture 13: AVL Trees and Binary Heaps Review Exercises 1. ( ) Interview question: Given an array show how to shue it randomly so that any possible reordering is equally

More information

Design and Analysis of Algorithms Lecture- 9: Binary Search Trees

Design and Analysis of Algorithms Lecture- 9: Binary Search Trees Design and Analysis of Algorithms Lecture- 9: Binary Search Trees Dr. Chung- Wen Albert Tsao 1 Binary Search Trees Data structures that can support dynamic set operations. Search, Minimum, Maximum, Predecessor,

More information

Data Structure. IBPS SO (IT- Officer) Exam 2017

Data Structure. IBPS SO (IT- Officer) Exam 2017 Data Structure IBPS SO (IT- Officer) Exam 2017 Data Structure: In computer science, a data structure is a way of storing and organizing data in a computer s memory so that it can be used efficiently. Data

More information

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap DATA STRUCTURES AND ALGORITHMS Hierarchical data structures: AVL tree, Bayer tree, Heap Summary of the previous lecture TREE is hierarchical (non linear) data structure Binary trees Definitions Full tree,

More information

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer) COSC 2007 Data Structures II Final Exam Thursday, April 13 th, 2006 This is a closed book and closed notes exam. There are total 3 parts. Please answer the questions in the provided space and use back

More information

Todays Lecture. Assignment 2 deadline: You have 5 Calendar days to complete.

Todays Lecture. Assignment 2 deadline: You have 5 Calendar days to complete. Trees! Todays Lecture Assignment 2 deadline: 11 pm Monday 2/17 President s day. You have 5 Calendar days to complete. Trees What are trees in computer science? Data Structures for Trees Algorithms useful

More information

Analysis of Algorithms

Analysis of Algorithms Algorithm An algorithm is a procedure or formula for solving a problem, based on conducting a sequence of specified actions. A computer program can be viewed as an elaborate algorithm. In mathematics and

More information

COMP : Trees. COMP20012 Trees 219

COMP : Trees. COMP20012 Trees 219 COMP20012 3: Trees COMP20012 Trees 219 Trees Seen lots of examples. Parse Trees Decision Trees Search Trees Family Trees Hierarchical Structures Management Directories COMP20012 Trees 220 Trees have natural

More information

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am Announcements Midterm exam 2, Thursday, May 18 Closed book/notes but one sheet of paper allowed Covers up to stacks and queues Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps

More information

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination University of Illinois at Urbana-Champaign Department of Computer Science Final Examination CS 225 Data Structures and Software Principles Spring 2010 7-10p, Wednesday, May 12 Name: NetID: Lab Section

More information

) $ f ( n) " %( g( n)

) $ f ( n)  %( g( n) CSE 0 Name Test Spring 008 Last Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to compute the sum of the n elements of an integer array is: # A.

More information

DIT960 Datastrukturer

DIT960 Datastrukturer DIT90 Datastrukturer suggested solutions for exam 07-0-0. The following code takes as input two arrays with elements of the same type. The arrays are called a and b. The code returns a dynamic array which

More information

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU Course Review for Midterm Exam 1 Cpt S 223 Fall 2011 1 Midterm Exam 1 When: Friday (10/14) 1:10-2pm Where: in class Closed book, closed notes Comprehensive Material for preparation: Lecture slides & in-class

More information

12 Abstract Data Types

12 Abstract Data Types 12 Abstract Data Types 12.1 Foundations of Computer Science Cengage Learning Objectives After studying this chapter, the student should be able to: Define the concept of an abstract data type (ADT). Define

More information

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs Algorithms in Systems Engineering ISE 172 Lecture 16 Dr. Ted Ralphs ISE 172 Lecture 16 1 References for Today s Lecture Required reading Sections 6.5-6.7 References CLRS Chapter 22 R. Sedgewick, Algorithms

More information

Trees Algorhyme by Radia Perlman

Trees Algorhyme by Radia Perlman Algorhyme by Radia Perlman I think that I shall never see A graph more lovely than a tree. A tree whose crucial property Is loop-free connectivity. A tree which must be sure to span. So packets can reach

More information

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

& ( D.  mnp ' ( ) n 3. n 2. ( ) C.  n CSE Name Test Summer Last Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to multiply two n " n matrices is: A. " n C. "% n B. " max( m,n, p). The

More information

Algorithm Design and Analysis

Algorithm Design and Analysis Algorithm Design and Analysis LECTURE 3 Data Structures Graphs Traversals Strongly connected components Sofya Raskhodnikova L3.1 Measuring Running Time Focus on scalability: parameterize the running time

More information

Lists and Sequences. 1/22/ :32 PM Sequences 1

Lists and Sequences. 1/22/ :32 PM Sequences 1 Lists and Sequences 1/22/2018 10:32 PM Sequences 1 Outline Singly linked list Position ADT and List ADT Doubly linked list Sequence ADT Implementations of the sequence ADT Iterators 1/22/2018 10:32 PM

More information

Vectors. 9/14/2007 2:55 PM Vectors 1

Vectors. 9/14/2007 2:55 PM Vectors 1 Vectors 9/4/2007 2:55 PM Vectors Outline and Reading The Vector ADT ( 5..) Array-based implementation ( 5..2) 9/4/2007 2:55 PM Vectors 2 The Vector ADT The Vector ADT stores objects to which it provides

More information

Ch04 Balanced Search Trees

Ch04 Balanced Search Trees Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 05 Ch0 Balanced Search Trees v 3 8 z Why care about advanced implementations? Same entries,

More information

The questions will be short answer, similar to the problems you have done on the homework

The questions will be short answer, similar to the problems you have done on the homework Introduction The following highlights are provided to give you an indication of the topics that you should be knowledgeable about for the midterm. This sheet is not a substitute for the homework and the

More information

Lecture 7. Transform-and-Conquer

Lecture 7. Transform-and-Conquer Lecture 7 Transform-and-Conquer 6-1 Transform and Conquer This group of techniques solves a problem by a transformation to a simpler/more convenient instance of the same problem (instance simplification)

More information

CMSC351 - Fall 2014, Homework #2

CMSC351 - Fall 2014, Homework #2 CMSC351 - Fall 2014, Homework #2 Due: October 8th at the start of class Name: Section: Grades depend on neatness and clarity. Write your answers with enough detail about your approach and concepts used,

More information

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May Code No: R21051 R10 SET - 1 II B. Tech I Semester, Supplementary Examinations, May - 2012 DATA STRUCTURES (Com. to CSE, IT, ECC ) Time: 3 hours Max Marks: 75 Answer any FIVE Questions All Questions carry

More information

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g) Introduction to Algorithms March 11, 2009 Massachusetts Institute of Technology 6.006 Spring 2009 Professors Sivan Toledo and Alan Edelman Quiz 1 Solutions Problem 1. Quiz 1 Solutions Asymptotic orders

More information

( ). Which of ( ) ( ) " #& ( ) " # g( n) ( ) " # f ( n) Test 1

( ). Which of ( ) ( )  #& ( )  # g( n) ( )  # f ( n) Test 1 CSE 0 Name Test Summer 006 Last Digits of Student ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to multiply two n x n matrices is: A. "( n) B. "( nlogn) # C.

More information

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# #

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# # 1. Prove that n log n n is Ω(n). York University EECS 11Z Winter 1 Problem Set 3 Instructor: James Elder Solutions log n n. Thus n log n n n n n log n n Ω(n).. Show that n is Ω (n log n). We seek a c >,

More information

x-fast and y-fast Tries

x-fast and y-fast Tries x-fast and y-fast Tries Problem Problem Set Set 7 due due in in the the box box up up front. front. That's That's the the last last problem problem set set of of the the quarter! quarter! Outline for Today

More information

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC) SET - 1 II B. Tech I Semester Supplementary Examinations, May/June - 2016 PART A 1. a) Write a procedure for the Tower of Hanoi problem? b) What you mean by enqueue and dequeue operations in a queue? c)

More information

UNIT III BALANCED SEARCH TREES AND INDEXING

UNIT III BALANCED SEARCH TREES AND INDEXING UNIT III BALANCED SEARCH TREES AND INDEXING OBJECTIVE The implementation of hash tables is frequently called hashing. Hashing is a technique used for performing insertions, deletions and finds in constant

More information

CS521 \ Notes for the Final Exam

CS521 \ Notes for the Final Exam CS521 \ Notes for final exam 1 Ariel Stolerman Asymptotic Notations: CS521 \ Notes for the Final Exam Notation Definition Limit Big-O ( ) Small-o ( ) Big- ( ) Small- ( ) Big- ( ) Notes: ( ) ( ) ( ) ( )

More information

Augmenting Data Structures

Augmenting Data Structures Augmenting Data Structures [Not in G &T Text. In CLRS chapter 14.] An AVL tree by itself is not very useful. To support more useful queries we need more structure. General Definition: An augmented data

More information

Search Trees. Undirected graph Directed graph Tree Binary search tree

Search Trees. Undirected graph Directed graph Tree Binary search tree Search Trees Undirected graph Directed graph Tree Binary search tree 1 Binary Search Tree Binary search key property: Let x be a node in a binary search tree. If y is a node in the left subtree of x, then

More information

( ) D. Θ ( ) ( ) Ο f ( n) ( ) Ω. C. T n C. Θ. B. n logn Ο

( ) D. Θ ( ) ( ) Ο f ( n) ( ) Ω. C. T n C. Θ. B. n logn Ο CSE 0 Name Test Fall 0 Multiple Choice. Write your answer to the LEFT of each problem. points each. The expected time for insertion sort for n keys is in which set? (All n! input permutations are equally

More information

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct. MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct. In linked list the elements are necessarily to be contiguous In linked list the elements may locate at far positions

More information