From b3e786da4197534cee61f858463b19fd29c3e03e Mon Sep 17 00:00:00 2001 From: Vladyslava Kanivets Date: Mon, 18 Sep 2023 17:12:52 +0300 Subject: [PATCH 1/3] +add + remove + contains --- .gitignore | 3 + .../ua/javarush/map/SimplifiedTreeMap.java | 163 ++++++++++++++++++ 2 files changed, 166 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f83e8cf --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +target +*.iml diff --git a/src/main/java/ua/javarush/map/SimplifiedTreeMap.java b/src/main/java/ua/javarush/map/SimplifiedTreeMap.java index 457a8ff..67069eb 100644 --- a/src/main/java/ua/javarush/map/SimplifiedTreeMap.java +++ b/src/main/java/ua/javarush/map/SimplifiedTreeMap.java @@ -1,5 +1,168 @@ package ua.javarush.map; +import java.util.Comparator; + public class SimplifiedTreeMap { + private final Comparator comparator; + private Entry root; + private int size; + + public SimplifiedTreeMap() { + comparator = null; + } + + public SimplifiedTreeMap(Comparator comparator) { + this.comparator = comparator; + } + + public boolean add(K key, V value) { + if (size == 0) { + root = new Entry<>(key, value, null); + size++; + return true; + } + + Entry temp = root; + Entry parent; + int diff; + + do { + parent = temp; + diff = compare(key, temp.key); + if (diff < 0) { + temp = temp.left; + } else if (diff > 0) { + temp = temp.right; + } else { + temp.value = value; + return false; + } + } while (temp != null); + + Entry newEntry = new Entry<>(key, value, parent); + + if (diff > 0) { + parent.right = newEntry; + } else { + parent.left = newEntry; + } + + size++; + return true; + } + + public Entry getEntry(K key) { + Entry temp = root; + + do { + int diff = compare(key, temp.key); + if (diff < 0) { + temp = temp.left; + } else if (diff > 0) { + temp = temp.right; + } else { + return temp; + } + } while (temp != null); + return null; + } + + public boolean remove(K key) { + Entry forRemove = getEntry(key); + if (forRemove == null) { + return false; + } + + size--; + + if (size == 1) { + if (root.key == key) { + root = null; + } + return true; + } + + if (forRemove.left == null && forRemove.right == null) { + return removeLeaf(forRemove); + } + + if (forRemove.right != null && forRemove.left == null) { + if (compare(forRemove.parent.key, forRemove.key) > 0) { + forRemove.parent.left = forRemove.right; + } else { + forRemove.parent.right = forRemove.right; + } + return true; + } else if (forRemove.right == null && forRemove.left != null) { + if (compare(forRemove.parent.key, forRemove.key) > 0) { + forRemove.parent.left = forRemove.left; + } else { + forRemove.parent.right = forRemove.left; + } + return true; + } + + removeWithTwoChildren(forRemove); + return true; + } + + private void removeWithTwoChildren(Entry forRemove) { + Entry lowestLeftChild; + Entry temp = forRemove.right; + + do { + lowestLeftChild = temp; + temp = temp.left; + } while (temp != null); + + lowestLeftChild.right = forRemove.right; + lowestLeftChild.left = forRemove.left; + + if (compare((forRemove.parent).key, forRemove.key) > 0) { + (forRemove.parent).left = lowestLeftChild; + } else { + (forRemove.parent).right = lowestLeftChild; + } + } + + private boolean removeLeaf(Entry forRemove) { + if (compare(forRemove.parent.key, forRemove.key) > 0) { + forRemove.parent.left = null; + } else { + forRemove.parent.right = null; + } + return true; + } + + public int size() { + return size; + } + + public boolean contains(K key) { + return getEntry(key) != null; + } + + private int compare(K k1, K k2) { + return comparator == null ? ((Comparable) k1).compareTo(k2) + : comparator.compare(k1, k2); + } + + private class Entry { + private final K key; + private V value; + private Entry left; + private Entry right; + private Entry parent; + + private Entry(K key, V value, Entry parent) { + this.key = key; + this.value = value; + this.parent = parent; + } + @Override + public String toString() { + return "key = " + key + ", value = " + value; + } + } } From 4ff4ada34e42f040c31a479bb571939f38dbaea6 Mon Sep 17 00:00:00 2001 From: Vladyslava Kanivets Date: Mon, 18 Sep 2023 17:23:00 +0300 Subject: [PATCH 2/3] k limitation --- src/main/java/ua/javarush/map/SimplifiedTreeMap.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/ua/javarush/map/SimplifiedTreeMap.java b/src/main/java/ua/javarush/map/SimplifiedTreeMap.java index 67069eb..03a8f48 100644 --- a/src/main/java/ua/javarush/map/SimplifiedTreeMap.java +++ b/src/main/java/ua/javarush/map/SimplifiedTreeMap.java @@ -2,7 +2,7 @@ import java.util.Comparator; -public class SimplifiedTreeMap { +public class SimplifiedTreeMap, V> { private final Comparator comparator; private Entry root; private int size; @@ -143,7 +143,7 @@ public boolean contains(K key) { } private int compare(K k1, K k2) { - return comparator == null ? ((Comparable) k1).compareTo(k2) + return comparator == null ? (k1).compareTo(k2) : comparator.compare(k1, k2); } From 077cdfa3bf2ddd8b72d80675653bd68dda930d0f Mon Sep 17 00:00:00 2001 From: Vladyslava Kanivets Date: Tue, 19 Sep 2023 16:58:32 +0300 Subject: [PATCH 3/3] add interface --- src/main/java/ua/javarush/map/SimplifiedMap.java | 8 ++++++++ src/main/java/ua/javarush/map/SimplifiedTreeMap.java | 6 +++++- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 src/main/java/ua/javarush/map/SimplifiedMap.java diff --git a/src/main/java/ua/javarush/map/SimplifiedMap.java b/src/main/java/ua/javarush/map/SimplifiedMap.java new file mode 100644 index 0000000..100c18b --- /dev/null +++ b/src/main/java/ua/javarush/map/SimplifiedMap.java @@ -0,0 +1,8 @@ +package ua.javarush.map; + +public interface SimplifiedMap { + boolean add(K key, V value); + boolean remove(K key); + boolean contains(K key); + int size(); +} diff --git a/src/main/java/ua/javarush/map/SimplifiedTreeMap.java b/src/main/java/ua/javarush/map/SimplifiedTreeMap.java index 03a8f48..8c7d104 100644 --- a/src/main/java/ua/javarush/map/SimplifiedTreeMap.java +++ b/src/main/java/ua/javarush/map/SimplifiedTreeMap.java @@ -2,7 +2,7 @@ import java.util.Comparator; -public class SimplifiedTreeMap, V> { +public class SimplifiedTreeMap, V> implements SimplifiedMap { private final Comparator comparator; private Entry root; private int size; @@ -15,6 +15,7 @@ public SimplifiedTreeMap(Comparator comparator) { this.comparator = comparator; } + @Override public boolean add(K key, V value) { if (size == 0) { root = new Entry<>(key, value, null); @@ -67,6 +68,7 @@ public Entry getEntry(K key) { return null; } + @Override public boolean remove(K key) { Entry forRemove = getEntry(key); if (forRemove == null) { @@ -134,10 +136,12 @@ private boolean removeLeaf(Entry forRemove) { return true; } + @Override public int size() { return size; } + @Override public boolean contains(K key) { return getEntry(key) != null; }