From 7bd5250ed586d10c98a5a54e2b1ff14bb4515c82 Mon Sep 17 00:00:00 2001 From: Michael Lee <michael.lee.0x2a@gmail.com> Date: Mon, 1 Feb 2016 11:12:08 -0800 Subject: [PATCH] Modify BST insert to correctly return old value --- src/cse332/datastructures/trees/BinarySearchTree.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/cse332/datastructures/trees/BinarySearchTree.java b/src/cse332/datastructures/trees/BinarySearchTree.java index 947e233..e5da906 100644 --- a/src/cse332/datastructures/trees/BinarySearchTree.java +++ b/src/cse332/datastructures/trees/BinarySearchTree.java @@ -58,9 +58,6 @@ public class BinarySearchTree<K extends Comparable<K>, V> // We found the key! if (direction == 0) { - if (value != null) { - current.value = value; - } return current; } else { @@ -73,7 +70,7 @@ public class BinarySearchTree<K extends Comparable<K>, V> // If value is null, we need to actually add in the new value if (value != null) { - current = new BSTNode(key, value); + current = new BSTNode(key, null); if (this.root == null) { this.root = current; } @@ -99,7 +96,10 @@ public class BinarySearchTree<K extends Comparable<K>, V> @Override public V insert(K key, V value) { - return find(key, value).value; + BSTNode current = find(key, value); + V oldValue = current.value; + current.value = value; + return oldValue; } @Override -- GitLab