Sorted Array

36
Sorted Array • What is BigO for sorted list implemented as: ArrayList: – Search : – Insert(value) : – Remove(value) : LinkedList: – Search : – Insert(value) : – Remove(value) :

description

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) - PowerPoint PPT Presentation

Transcript of Sorted Array

Page 1: Sorted Array

Sorted Array

• What is BigO for sorted list implemented as:

ArrayList:– Search : – Insert(value) : – Remove(value) :

LinkedList:– Search : – Insert(value) : – Remove(value) :

Page 2: Sorted Array

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)

Page 3: Sorted Array

Binary Trees & BSTs

Page 4: Sorted Array

Binary Tree

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

Page 5: Sorted Array

Binary Tree

• Can represent any tree as binary

Page 6: Sorted Array

Binary Tree

• Can represent any tree as binary– Left = first child– Right = next sibling

Page 7: Sorted Array

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

Page 8: Sorted Array

Binary Search Tree Use

• BST maintains sorted collection with:– Search : O(logN)*– Insertion : O(logN)*– Removal : O(logN)*

* Actual mileage may vary…

Page 9: Sorted Array

Binary Search Tree Use

• BST maintains sorted collection with:– Search : O(logN)*– Insertion : O(logN)*– Removal : O(logN)*

• But…– No random access

Page 10: Sorted Array

Duplicates

• Duplicate value approaches:1. No duplicates : left < current < right2. left < current <= right (duplicates on right)3. left <= current < right (duplicates on left)4. Duplicates stored in list in node : left < current < right

• 2 & 3 more complicated, especiallyfor balanced trees

Page 11: Sorted Array

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

Page 12: Sorted Array

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)

Page 13: Sorted Array

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

Page 14: Sorted Array

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

Page 15: Sorted Array

BST Insertion

IterativeInsertInsert(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)

Page 16: Sorted Array

BST Insertion

IterativeInsertInsert(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

Page 17: Sorted Array

BST Insertion

IterativeInsertInsert(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)

Insert(9)

Page 18: Sorted Array

BST Insertion

IterativeInsertInsert(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)

Insert(9)

Page 19: Sorted Array

BST Insertion

IterativeInsertInsert(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)

Insert(9)

Page 20: Sorted Array

BST Insertion

IterativeInsertInsert(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)

Insert(9)

Page 21: Sorted Array

BST Insertion

IterativeInsertInsert(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)

Insert(9)

Page 22: Sorted Array

BST Insertion

IterativeInsertInsert(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)

Insert(9)

Page 23: Sorted Array

BST Insertion

IterativeInsertInsert(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)

Insert(9)

Page 24: Sorted Array

BST Insertion

Iterative InsertInsert(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)

Insert(9)

Page 25: Sorted Array

BST Insertion

Recursive InsertInsert(value)

root = InsertHelper(root, value)

InsertHelper(curNode, value) if curNode == null return new Node(value)

if value < curNode leftChild = InsertHelper(leftChild, value) else rightChild = InsertHelper(rightChild, value)

return curNode

Page 26: Sorted Array

BST Insertion

Recursive InsertInsert(value) root = InsertHelper(root, value)

InsertHelper(curNode, value) if curNode == null return new Node(value)

if value < curNode leftChild = InsertHelper(leftChild, value) else rightChild = InsertHelper(rightChild, value)

return curNode

Insert(9)

Page 27: Sorted Array

BST Insertion

Recursive InsertInsert(value) root = InsertHelper(root, value)

InsertHelper(curNode, value) if curNode == null return new Node(value)

if value < curNode leftChild = InsertHelper(leftChild, value) else rightChild = InsertHelper(rightChild, value)

return curNode

Insert(9)Insert(root, 9)

Page 28: Sorted Array

BST Insertion

Recursive InsertInsert(value) root = InsertHelper(root, value)

InsertHelper(curNode, value) if curNode == null return new Node(value)

if value < curNode leftChild = InsertHelper(leftChild, value) else rightChild = InsertHelper(rightChild, value)

return curNode

Insert(9)Insert(root, 9)Insert({12}, 9)

Page 29: Sorted Array

BST Insertion

Recursive InsertInsert(value) root = InsertHelper(root, value)

InsertHelper(curNode, value) if curNode == null return new Node(value)

if value < curNode leftChild = InsertHelper(leftChild, value) else rightChild = InsertHelper(rightChild, value)

return curNode

Insert(9)Insert(root, 9)Insert({12}, 9)Insert({6}, 9)

Page 30: Sorted Array

BST Insertion

Recursive InsertInsert(value) root = InsertHelper(root, value)

InsertHelper(curNode, value) if curNode == null return new Node(value)

if value < curNode leftChild = InsertHelper(leftChild, value) else rightChild = InsertHelper(rightChild, value)

return curNode

Insert(9)Insert(root, 9)Insert({12}, 9)Insert({6}, 9)Insert(null, 9)

Page 31: Sorted Array

BST Insertion

Recursive InsertInsert(value) root = InsertHelper(root, value)

InsertHelper(curNode, value) if curNode == null return new Node(value)

if value < curNode leftChild = InsertHelper(leftChild, value) else rightChild = InsertHelper(rightChild, value)

return curNode

Insert(9)Insert(root, 9)Insert({12}, 9)Insert({6}, 9)

Page 32: Sorted Array

BST Insertion

Recursive InsertInsert(value) root = InsertHelper(root, value)

InsertHelper(curNode, value) if curNode == null return new Node(value)

if value < curNode leftChild = InsertHelper(leftChild, value) else rightChild = InsertHelper(rightChild, value)

return curNode

Insert(9)Insert(root, 9)Insert({12}, 9)

Page 33: Sorted Array

BST Insertion

Recursive InsertInsert(value) root = InsertHelper(root, value)

InsertHelper(curNode, value) if curNode == null return new Node(value)

if value < curNode leftChild = InsertHelper(leftChild, value) else rightChild = InsertHelper(rightChild, value)

return curNode

Insert(9)Insert(root, 9)

Page 34: Sorted Array

BST Insertion

Recursive InsertInsert(value) root = InsertHelper(root, value)

InsertHelper(curNode, value) if curNode == null return new Node(value)

if value < curNode leftChild = InsertHelper(leftChild, value) else rightChild = InsertHelper(rightChild, value)

return curNode

Insert(9)

Page 35: Sorted Array

CharBST

• BST of Chars– Allow dupes to right– Built from BSTNode<char>

Page 36: Sorted Array

CharBST

• Print– Recursive inorder print