1 / 145

CSE233 Course Review

CSE233 Course Review. CSE, POSTECH. What we studied in the 1 st half. Week 1 : Overview of C++, Program performance Week 2 : Array-based and linked representations of Lists Week 3 : Arrays and Matrices Week 4 : Performance Measurement Week 5 : Stacks Week 6 : Queues

adkinsd
Download Presentation

CSE233 Course Review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE233 Course Review CSE, POSTECH

  2. What we studied in the 1st half Week 1 : Overview of C++, Program performance Week 2 : Array-based and linked representations of Lists Week 3 : Arrays and Matrices Week 4 : Performance Measurement Week 5 : Stacks Week 6 : Queues Week 7 : Skip lists and Hashing Week 8 : Review and Midterm Exam

  3. What we studied in the 2nd half Week 9 : Binary and other trees Week 10 : Priority queues, Heaps, Leftist trees Week 11 : Tournament trees and Bin packing Week 12 : Binary search trees Week 13 : AVL trees Week 14 : Graphs Week 15 : Graph Search Methods Week 16 : Review and Final exam

  4. Trees

  5. Tree Terminology • A treet is a finite nonempty set of elements • The element at the top is called the root • The remaining elements, if any, are partitioned into trees, which are called the subtrees of t. • Elements next in the hierarchy are the children of the root. • Elements next in the hierarchy are the grandchildren of the root, and so on. • Elements at the lowest level of the hierarchy are the leaves.

  6. Leaves, Parent, Grandparent, Siblings, Ancestors, Descendents Leaves = {Mike,AI,Sue,Chris} Parent(Mary) = Joe Grandparent(Sue) = Mary Siblings(Mary) = {Ann,John} Ancestors(Mike) = {Ann,Joe} Descendents(Mary)={Mark,Sue}

  7. level 1 level 2 level 3 level 4 Levels and Height • Root is at level 1 and its children are at level 2. • Height = depth = number of levels

  8. Node Degree • Node degree is the number of children it has

  9. Tree Degree • Tree degree is the maximum of node degrees tree degree = 3

  10. Binary Trees

  11. Binary Tree • A finite (possibly empty) collection of elements • A nonempty binary tree has a root element and the remaining elements (if any) are partitioned into two binary trees • They are called the left and right subtrees of the binary tree

  12. - different when viewed as a binary tree a a - same when viewed as a tree b c c b Difference Between a Tree & a Binary Tree • A binary tree may be empty; a tree cannot be empty. • No node in a binary tree may have a degree more than 2, whereas there is no limit on the degree of a node in a tree. • The subtrees of a binary tree are ordered; those of a tree are not ordered.

  13. Binary Tree for Mathematical Expressions Figure 11.5 Expression Trees

  14. Full Binary Tree • A full binary tree of height h has exactly 2h-1 nodes. • Numbering the nodes in a full binary tree • Number the nodes 1 through 2h-1 • Number by levels from top to bottom • Within a level, number from left to right

  15. Complete Binary Tree with N Nodes • Start with a full binary tree that has at least nnodes • Number the nodes as described earlier. • The binary tree defined by the nodes numbered 1 through n is the n-node complete binary tree. • A full binary tree is a special case of a complete binary tree

  16. Example of Complete Binary Tree • Complete binary tree with 10 nodes.

  17. Fig. 11.8 Incomplete binary trees Incomplete Binary Trees • Complete binary tree with some missing elements

  18. Binary Tree Representation • Array representation • Linked representation

  19. Array Representation of Binary Tree • The binary tree is represented in an array by storing each element at the array position corresponding to the number assigned to it.

  20. Right-Skewed Binary Tree • An n node binary tree needs an array whose length is between n+1 and 2n. • Right-skewed binary tree wastes the most space • What about left-skewed binary tree?

  21. Linked Representation of Binary Tree • The most popular way to present a binary tree • Each element is represented by a node that has two link fields (leftChild and rightChild) and an element field

  22. Priority Queues

  23. Priority Queues • A priority queue is a collection of zero or more elements  each element has a priority or value • Unlike the FIFO queues, the order of deletion from a priority queue (e.g., who gets served next) is determined by the element priority • Elements are deleted by increasing or decreasing order of priority rather than by the order in which they arrived in the queue

  24. Priority Queues • Operations performed on priority queues 1) Find an element, 2) insert a new element, 3) delete an element, etc. • In a Min priority queue, find/delete operation finds/deletes the element with minimum priority • In a Max priority queue, find/delete operation finds/deletes the element with maximum priority • Two or more elements can have the same priority

  25. Implementation of Priority Queues • Implemented using heaps and leftist trees • Heap is a complete binary tree that is efficiently stored using the array-based representation • Leftist tree is a linked data structure suitable for the implementation of a priority queue

  26. Min (Max) Trees

  27. Max (Min) Tree • A max tree (min tree) is a tree in which the value in each node is greater (less)than or equal to those in its children (if any) • Nodes of a max or min tree may have more than two children (i.e., may not be binary tree)

  28. Max Tree Example

  29. Min Tree Example

  30. Heaps

  31. Heaps - Definitions • A max heap (min heap) is a max (min) tree that is also a complete binary tree

  32. Max Heap with 9 Nodes

  33. Min Heap with 9 Nodes

  34. Array Representation of Heap • A heap is efficiently represented as an array.

  35. 9 8 7 6 7 2 6 5 1 20 Insertion into a Max Heap • New element is 20 • Are we finished?

  36. 9 8 7 6 20 2 6 5 1 7 Insertion into a Max Heap • Exchange the positions with 7 • Are we finished?

  37. 9 20 7 6 8 2 6 5 1 7 Insertion into a Max Heap • Exchange the positions with 8 • Are we finished?

  38. 20 9 7 6 8 2 6 5 1 7 Insertion into a Max Heap • Exchange the positions with 9 • Are we finished?

  39. 20 15 7 6 9 2 6 5 1 7 8 Deletion from a Max Heap • Max element is in the root • What happens when we • delete an element?

  40. 15 7 6 9 2 6 5 1 7 8 Deletion from a Max Heap • After the max element is removed. • Are we finished?

  41. 15 7 6 9 2 6 5 1 7 8 Deletion from a Max Heap • Heap with 10 nodes. • Reinsert 8 into the heap.

  42. 8 15 7 6 9 2 6 5 1 7 Deletion from a Max Heap • Reinsert 8 into the heap. • Are we finished?

  43. 15 8 7 6 9 2 6 5 1 7 Deletion from a Max Heap • Exchange the position with 15 • Are we finished?

  44. 15 9 7 6 8 2 6 5 1 7 Deletion from a Max Heap • Exchange the position with 9 • Are we finished?

  45. Max Heap Initialization • Heap initialization means to construct a heap by adjusting the tree if necessary • Example: input array = [-,1,2,3,4,5,6,7,8,9,10,11]

  46. Max Heap Initialization - Start at rightmost array position that has a child.

  47. Max Heap Initialization

  48. Max Heap Initialization

  49. Max Heap Initialization

  50. Max Heap Initialization

More Related