1 / 35

Data Structure II

Data Structure II. So Pak Yeung 26-2-2011. Outline. Review Array Sorted Array Linked List Binary Search Tree Heap Hash Table. Review. Operation Find an element Find the min/max element Insert an element Remove an element Time complexity? O(1)? O(lg N)? O(N)?. Array.

Download Presentation

Data Structure II

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. Data Structure II So Pak Yeung 26-2-2011

  2. Outline • Review • Array • Sorted Array • Linked List • Binary Search Tree • Heap • Hash Table

  3. Review • Operation • Find an element • Find the min/max element • Insert an element • Remove an element • Time complexity? • O(1)? • O(lg N)? • O(N)?

  4. Array • Find an element • O(N) • Find the smallest element • O(N) • Insert an element • O(1) • Remove an element • O(1)

  5. Sorted Array • Find an element • O(lg N) • Find the smallest element • O(1) • Insert an element • O(N) • Remove an element • O(N)

  6. Linked List • Find an element • O(N) • Find the smallest element • O(N) • Insert an element • O(1) • Remove an element • O(1)

  7. Binary Search Tree • Binary Tree • At most 2 children for each node • For each Node i, • Node j <= Node i, for all Node j in left subtree of Node i • Node j > Node i, for all Node j in right subtree of Node i

  8. Binary Search Tree 8 2 21 5 13 34

  9. Binary Search Tree • Find 13 8 2 21 5 13 34

  10. Binary Search Tree • Find 3 8 2 21 5 13 34 ???

  11. Binary Search Tree • Insert 1 8 2 21 1 5 13 34

  12. Binary Search Tree • Insert 3 8 2 21 1 5 13 34 3

  13. Binary Search Tree • Find an element • Seems O(lg N)? • Find min/max • Seems O(lg N)? • Insert an element • Seems O(lg N)?

  14. Binary Search Tree • Worst Case: O(N)!!! 2 5 8 13 21 34

  15. Binary Search Tree • Remove a leaf node • Just Do it! • Remove a node with single child • Replace the node by its child • Remove a node with 2 children • Replace the node by the max node of left subtree / min node of right subtree • Lazy Deletion • Mark the node as deleted instead of deleting it

  16. Binary Search Tree • Again, seems O(lg N), worst Case: O(N)!!! 2 5 8 13 21 34

  17. Heap • Priority Queue • Binary Tree • For each node, it is no greater/less than all nodes of its subtree • Operation • Extract min/max • Insert

  18. Heap 1 2 13 8 5 21 34

  19. Heap • Extract min/max • Get the value from the root (O(1) to find min) • Replace the root by the last node • Shift down • Time complexity • O(lg N)

  20. Heap • Get 1 1 2 13 8 5 21 34

  21. Heap 34 2 13 8 5 21

  22. Heap 2 34 13 8 5 21

  23. Heap 2 5 13 8 34 21

  24. Heap • Insert an element • Add the node at the end • Shift up • Time complexity • O(lg N)

  25. Heap • Add 1 2 5 13 8 34 21 1

  26. Heap 2 5 1 8 34 21 13

  27. Heap 1 5 2 8 34 21 13

  28. Heap • Build heap • Insert each node • O(N lg N) • There exists a faster way!! • Only need to perform shift down process from the bottom • O(N)

  29. Heap • Find an element • Not support • O(N), if implement by array • Remove an element • Consider that subtree • O(lg N)

  30. Hash Table • Using limited memory, storing data of unlimited range • Convert data to integers that are in a limited range by a hash function

  31. Hash Table • Mark 5 number • Each number is between [1,10000000] • Not a good idea to use an array of size of 10000000 • A[n%5]=n

  32. Hash Table • Insert an element • O(1) • Find an element • Using the same Hash function! • O(1) • Delete an element • Lazy Deletion • O(1)

  33. Hash Table • Collision? • E.g. 56 and 111 • Open Hashing • Close Hashing

  34. Hash Table Opening Hashing 0 1 56 111 2 127 3 4

  35. Hash Table • Close hashing • Insert 1 • If the cell is occupied, find the next empty cell

More Related