Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures and Algorithms

Similar presentations


Presentation on theme: "Data Structures and Algorithms"— Presentation transcript:

1 Data Structures and Algorithms
UML Diagrams Data Structures and Algorithms CSE 373 SP 18 - Kasey Champion

2 Warm Up Given the following UML diagrams of a Hash Map implementation of a Dictionary write the pseudo code to implement the set(key, value) function HashMap<K, V> put() pair into array based on hash - Resize when appropriate Pair<K, V>[] LinkedList<E>[] size state behavior Data[] get() value from array index based given key’s hash set() update value in pair for given key’s hash to array index remove() take data out of array LinkedList<E> Add() add a new node that stores Key and Value to list ListNode<K, V> front state behavior get() return value from node with given key remove() deletes node with given key from list set() changes value in node with given key Contains() is the given key stored in list iterator() returns an iterator to move over list ListNode<K, V> Construct a new Node ListNode<K, V> next state behavior K key V value CSE 373 SP 18 - Kasey Champion

3 Implementing Set HashMap<K, V> LinkedList<E>
size state behavior void put(key, value) value get(key) void set(key, value) void remove(key) LinkedList<E> void add(key, value) ListNode<K, V> front state behavior value get(key) void remove(key) void set(key, value) boolean contains(key) iterator<E> iterator() void set(k key, v value) { bucketAddress = get hash for key % table size bucketList = data[bucketAddress] loop(bucketList) if (this node’s key is what I am trying to add) replace this node with new pair stop work } ListNode<K, V> Construct a new Node ListNode<K, V> next state behavior K key V value CSE 373 SP 18 - Kasey Champion

4 Implementing a Dictionary
Dictionary ADT Add pair to collection Count of data pairs state behavior Set of Key, Value pairs Keys must be unique! No required order Get value for given key Change value for given key Remove data pair from collection public interface Dictionary { void put(key, value) unspecified state behavior value get(key) void set(key, value) void remove(key) HashMap<K, V> put() pair into array based on hash - Resize when appropriate Pair<K, V>[] LinkedList<E>[] size state behavior Data[] get() value from array index based given key’s hash set() update value in pair for given key’s hash to array index remove() take data out of array TreeMap<K, V> put() add node for new pair in correct location - Balance when appropriate state behavior overallRoot<K,V> get() value based on node location in tree set() update value in pair for given key remove() delete given node - replace with appropriate existing node CSE 373 SP 18 - Kasey Champion

5 Implementing Tree Map TreeMap<K, V> state behavior
put() add node for new pair in correct location - Balance when appropriate state behavior overallRoot<K,V> get() value based on node location in tree set() update value in pair for given key remove() delete given node - replace with appropriate existing node ListNode<K, V> Construct a new Node ListNode<K, V> left state behavior K key V value ListNode<K, V> right int height CSE 373 SP 18 - Kasey Champion

6 Implementing Tree Map TreeMap<K, V> ListNode<K, V> state
behavior overallRoot<K,V> Implementing Tree Map v get(k key) { start at top of tree } ListNode<K, V> getHelper(key, Node) { if(node is null) data isn’t in collection if data at current node > what I’m looking for go left if data at current node < what I’m looking for go right else found it! void put(key, value) value get(key) void set(key, value) void remove(key) ListNode<K, V> Construct a new Node ListNode<K, V> left state behavior K key V value ListNode<K, V> right int height CSE 373 SP 18 - Kasey Champion

7 Implementing Tree Map TreeMap<K, V> ListNode<K, V> state
behavior overallRoot<K,V> Implementing Tree Map void put(key, value) void put(key, value) { overallroot = putHelper(key, value, overallroot) } Node putHelper(K key, V value, Node node, int height) { if(node is null) return new node if data at current node > what I’m looking for go left if data at current node < what I’m looking for go right else return replacement node balance the tree if needed value get(key) void set(key, value) void remove(key) ListNode<K, V> Construct a new Node ListNode<K, V> left state behavior K key V value ListNode<K, V> right int height CSE 373 SP 18 - Kasey Champion

8 Two AVL Cases Kink Case Solve with 2 rotations Line Case
3 1 1 3 2 2 3 1 1 3 2 2 Rotate Right Parent’s left becomes child’s right Child’s right becomes its parent Rotate Left Parent’s right becomes child’s left Child’s left becomes its parent Rotate subtree left Rotate root tree right Rotate subtree right Rotate root tree left CSE 373 SP 18 - Kasey Champion

9 Rotations! Balanced! Unbalanced! a b a Insert ‘c’ X b X b Z a Y Z Y Z
CSE 373 SP 18 - Kasey Champion

10 Double Rotations 1 Unbalanced! a a a Insert ‘c’ X e W d W e d Z Y e d
CSE 373 SP 18 - Kasey Champion

11 Double Rotations 2 d Unbalanced! a e a W d Y X Z W Y e X Z c c
CSE 373 SP 18 - Kasey Champion

12 Implementing Tree Map TreeMap<K, V> ListNode<K, V> state
behavior overallRoot<K,V> Implementing Tree Map void put(key, value) void put(key, value) { overallroot = putHelper(key, value, overallroot) } Node balanceTree(Node) { if(node is null) return null else if (left subtree is more than 1 level taller than right subtree) if (if left subtrees left subtree is bigger than the left’s right) Rotate myself with my left child else Double rotate left then right else if (right subtree is more than 1 level taller than right subtree) if (right subtree’s right is taller than it’s left) Rotate myself with my right child Double rotate right then left value get(key) void set(key, value) void remove(key) ListNode<K, V> Construct a new Node ListNode<K, V> left state behavior K key V value ListNode<K, V> right int height CSE 373 SP 18 - Kasey Champion


Download ppt "Data Structures and Algorithms"

Similar presentations


Ads by Google