Sorted Array

Post on 23-Jan-2016

48 views 0 download

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

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)– Removal : O(N)

LinkedList:– Search : O(N)– Insertion : O(N)– Removal : O(N)

Binary Trees & BSTs

Binary Tree

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

Binary Tree

• Can represent any tree as binary

Binary Tree

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

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

Binary Search Tree Use

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

* Actual mileage may vary…

Binary Search Tree Use

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

• But…– No random access

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

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

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)

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

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

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)

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

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)

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)

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)

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)

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)

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)

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)

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)

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

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)

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)

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)

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)

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)

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)

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)

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)

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)

CharBST

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

CharBST

• Print– Recursive inorder print