1 / 36

Sorted Array

Sorted Array. What is BigO for sorted list implemented as: ArrayList : Search : Insert(value) : Remove(value) : LinkedList : Search : Insert(value) : Remove(value) :. Sorted Array. What is BigO for sorted list implemented as: ArrayList : Search : O( logN ) Insertion : O(N)

ozzy
Télécharger la présentation

Sorted Array

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. Sorted Array • What is BigO for sorted list implemented as: ArrayList: • Search : • Insert(value) : • Remove(value) : LinkedList: • Search : • Insert(value) : • Remove(value) :

  2. Sorted Array • What is BigO for sorted list implemented as: ArrayList: • Search : O(logN) • Insertion : O(N) • Removal : O(N) LinkedList: • Search : O(N) • Insertion : O(N) • Removal : O(N)

  3. Binary Trees & BSTs

  4. Binary Tree • Binary Tree • Each node has 0-2 children (left/right)

  5. Binary Tree • Can represent any tree as binary

  6. Binary Tree • Can represent any tree as binary • Left = first child • Right = next sibling

  7. Binary Search Tree • Binary Search Tree • Enforce ordering of children relative to parent • Anything on left subtree is smaller • Anything on right subtree is larger

  8. Binary Search Tree Use • BST maintains sorted collection with: • Search : O(logN)* • Insertion : O(logN)* • Removal : O(logN)* * Actual mileage may vary…

  9. Binary Search Tree Use • BST maintains sorted collection with: • Search : O(logN)* • Insertion : O(logN)* • Removal : O(logN)* • But… • No random access

  10. Duplicates • Duplicate value approaches: • No duplicates : left < current < right • left < current <= right(duplicates on right) • left <= current < right (duplicates on left) • Duplicates stored in list in node : left < current < right • 2 & 3 more complicated, especiallyfor balanced trees

  11. BST Search • Searching for a value • Descend tree looking for value • Use ordering to guide search http://www.cse.hut.fi/en/research/SVG/TRAKLA2/exercises/BST_search-28.html

  12. BST Search Recursive search: Search(value) return SearchHelp(root, value)SearchHelp(node , value) if node == null return false if node == value return true if value < node return SearchHelp(leftChild, value) else return SearchHelp(rightChild, value)

  13. BST Search Iterative search: ItSerach(value) current = root while current != null if current == value return true if value < node current = leftChild else current = rightChild end while return false

  14. BST Insertion • Inserting a value • New values are always leaves • Descend tree as if searching • Insert wherever you hit null http://www.cse.hut.fi/en/research/SVG/TRAKLA2/exercises/BST_Insert-5.html

  15. BST Insertion IterativeInsert Insert(value) if root == null root = new Node(value) else parent = root current = root do parent = current if value < node current = leftChild else current = rightChild while current != nullptr if value < parent parent->left = new Node(value) else parent->right = new Node(value)

  16. BST Insertion IterativeInsert Insert(value) if root == null root = new Node(value) else parent = root current = root do parent = current if value < node current = leftChild else current = rightChild while current != nullptr if value < parent parent->left = new Node(value) else parent->right = new Node(value) Empty BST = Special

  17. BST Insertion IterativeInsert Insert(value) if root == null root = new Node(value)elseparent = rootcurrent = root do parent = current if value < node current = leftChild else current = rightChild while current != nullptr if value < parent parent->left = new Node(value) else parent->right = new Node(value) Insert(9)

  18. BST Insertion IterativeInsert Insert(value) if root == null root = new Node(value)elseparent = rootcurrent = root doparent = current if value < node current = leftChild else current = rightChild while current != nullptr if value < parent parent->left = new Node(value) else parent->right = new Node(value) Insert(9)

  19. BST Insertion IterativeInsert Insert(value) if root == null root = new Node(value)elseparent = rootcurrent = root do parent = currentif value < node current = leftChild else current = rightChild while current != nullptr if value < parent parent->left = new Node(value) else parent->right = new Node(value) Insert(9)

  20. BST Insertion IterativeInsert Insert(value) if root == null root = new Node(value)elseparent = rootcurrent = root doparent = current if value < node current = leftChild else current = rightChild while current != nullptr if value < parent parent->left = new Node(value) else parent->right = new Node(value) Insert(9)

  21. BST Insertion IterativeInsert Insert(value) if root == null root = new Node(value)elseparent = rootcurrent = root do parent = currentif value < node current = leftChild else current = rightChild while current != nullptr if value < parent parent->left = new Node(value) else parent->right = new Node(value) Insert(9)

  22. BST Insertion IterativeInsert Insert(value) if root == null root = new Node(value)elseparent = rootcurrent = root doparent = current if value < node current = leftChild else current = rightChild while current != nullptr if value < parent parent->left = new Node(value) else parent->right = new Node(value) Insert(9)

  23. BST Insertion IterativeInsert Insert(value) if root == null root = new Node(value)elseparent = rootcurrent = root do parent = current if value < node current = leftChildelse current = rightChild while current != nullptr if value < parent parent->left = new Node(value) else parent->right = new Node(value) Insert(9)

  24. BST Insertion Iterative Insert Insert(value) if root == null root = new Node(value)elseparent = rootcurrent = root do parent = current if value < node current = leftChild else current = rightChild while current != nullptr if value < parent parent->left = new Node(value) else parent->right = new Node(value) Insert(9)

  25. BST Insertion Recursive Insert Insert(value)root = InsertHelper(root, value)InsertHelper(curNode, value) if curNode == null return new Node(value) if value < curNodeleftChild = InsertHelper(leftChild, value) elserightChild = InsertHelper(rightChild, value) return curNode

  26. BST Insertion Recursive Insert Insert(value)root = InsertHelper(root, value)InsertHelper(curNode, value) if curNode == null return new Node(value) if value < curNodeleftChild = InsertHelper(leftChild, value) elserightChild = InsertHelper(rightChild, value) return curNode Insert(9)

  27. BST Insertion Recursive Insert Insert(value)root = InsertHelper(root, value)InsertHelper(curNode, value) if curNode == null return new Node(value)if value < curNodeleftChild = InsertHelper(leftChild, value) elserightChild = InsertHelper(rightChild, value) return curNode Insert(9) Insert(root, 9)

  28. BST Insertion Recursive Insert Insert(value)root = InsertHelper(root, value)InsertHelper(curNode, value) if curNode == null return new Node(value)if value < curNodeleftChild = InsertHelper(leftChild, value) elserightChild = InsertHelper(rightChild, value) return curNode Insert(9) Insert(root, 9)Insert({12}, 9)

  29. BST Insertion Recursive Insert Insert(value)root = InsertHelper(root, value)InsertHelper(curNode, value) if curNode == null return new Node(value) if value < curNodeleftChild = InsertHelper(leftChild, value) elserightChild = InsertHelper(rightChild, value) return curNode Insert(9) Insert(root, 9)Insert({12}, 9)Insert({6}, 9)

  30. BST Insertion Recursive Insert Insert(value)root = InsertHelper(root, value)InsertHelper(curNode, value)if curNode == nullreturn new Node(value) if value < curNodeleftChild = InsertHelper(leftChild, value) elserightChild = InsertHelper(rightChild, value) return curNode Insert(9) Insert(root, 9)Insert({12}, 9)Insert({6}, 9)Insert(null, 9)

  31. BST Insertion Recursive Insert Insert(value)root = InsertHelper(root, value)InsertHelper(curNode, value) if curNode == nullreturn new Node(value) if value < curNodeleftChild = InsertHelper(leftChild, value) elserightChild= InsertHelper(rightChild, value) return curNode Insert(9) Insert(root, 9)Insert({12}, 9)Insert({6}, 9)

  32. BST Insertion Recursive Insert Insert(value)root = InsertHelper(root, value)InsertHelper(curNode, value) if curNode == null return new Node(value) if value < curNodeleftChild= InsertHelper(leftChild, value) elserightChild= InsertHelper(rightChild, value) return curNode Insert(9) Insert(root, 9)Insert({12}, 9)

  33. BST Insertion Recursive Insert Insert(value)root = InsertHelper(root, value)InsertHelper(curNode, value) if curNode == null return new Node(value) if value < curNodeleftChild= InsertHelper(leftChild, value) elserightChild= InsertHelper(rightChild, value) return curNode Insert(9) Insert(root, 9)

  34. BST Insertion Recursive Insert Insert(value)root = InsertHelper(root, value)InsertHelper(curNode, value) if curNode == null return new Node(value) if value < curNodeleftChild = InsertHelper(leftChild, value) elserightChild= InsertHelper(rightChild, value) return curNode Insert(9)

  35. CharBST • BST of Chars • Allow dupes to right • Built from BSTNode<char>

  36. CharBST • Print • Recursive inorder print

More Related