I am supposed to create a Binary Search Tree or B-Tree with a maximum of three nodes. Any one "parent" or "child" node can hold 1 or 2 values making it so that it can either have 2 or 3 "child" pointers on it.
For example: This is okay because there is only a maximum of 2 values in each set
23 30
/ | \
12 25 [35 38]
Where as this is not: because the root has 3 values in it and 4 "child" nodes
12 15 18
/ | | \
4 14 16 20
I was able to write my own Binary Search Tree, but I am having a lot of trouble figuring out how to turn that into a 2node or 3node Binary Search tree.
Could someone help me implement a 2node or 3node binary search tree code with the regular binary tree.
public class TreeNode<K extends Comparable<K>, V>
{
public K key;
public V value;
public TreeNode<K,V> left;
public TreeNode<K,V> right;
public TreeNode(K key, V value)
{
this.key = key;
this.value = value;
this.left = null;
this.right = null;
}
}
The tough part comes to when I need to change the actual BST Class. I know the put is going to change quite a bit because we have to check and see if it is a 2 node or a 3 node.
Here is what I have so far:
public class BST<K extends Comparable<K>, V>
{
private TreeNode<K, V> root;
public BST()
{
this.root = null;
}
public void put(K key, V value)
{
if (root == null)
root = new TreeNode<>(key, value);
else
put(root, key, value);
}
private void put(TreeNode<K, V> current, K key, V value)
{
int n = key.compareTo(current.key);
if (n == 0)
{
current.value = value;
return;
}
else if (n < 0)
{
if (current.left == null)
{
current.left = new TreeNode<>(key, value);
return;
}
else
put(current.left, key, value);
}
else
{
if (current.right == null)
{
current.right = new TreeNode<>(key, value);
return;
}
else
put(current.right, key, value);
}
}
public V get(K key)
{
return get(root, key);
}
private V get(TreeNode<K,V> current, K key)
{
if (current == null)
return null;
else
{
int n = key.compareTo(current.key);
if (n==0)
return current.value;
else if (n < 0)
return get(current.left, key);
else
return get(current.right, key);
}
}
public int size()
{
return size(root);
}
private int size(TreeNode<K,V> current)
{
if(current == null)
return 0;
else
return size(current.left) + size(current.right) + 1;
}
public int depth()
{
return depth(root) - 1;
}
private int depth(TreeNode<K,V> current)
{
if(current == null)
return 0;
else
return Math.max(depth(current.left), depth(current.right)) + 1;
}
}
Related
I made my own implementation of Java's HashMap after looking at the source code and watching a fiew videos on it. I only added the key methods, and this is what I came up with:
public class MyHashMap {
private Node[] arr;
private int n;
public MyHashMap() {
n = 16;
arr = new Node[n];
}
public void put(int key, int value) {
int index = key & (n - 1);
if (arr[index] == null) {
arr[index] = new Node(key, value);
} else {
Node node = arr[index];
if(node.key == key) {
node.value = value;
return;
}
while (node.next != null) {
if(node.key == key) {
node.value = value;
return;
}
node = node.next;
}
node.next = new Node(key, value);
}
}
public int get(int key) {
int index = key & (n - 1);
if(arr[index] == null) {
return -1;
} else {
Node node = arr[index];
while(node != null) {
if(node.key == key) {
return node.value;
}
}
return -1;
}
}
public void remove(int key) {
int index = key & (n - 1);
if(arr[index] == null) {
return;
} else {
Node node = arr[index];
if(node.next == null && node.key == key) {
arr[index] = null;
return;
}
while(node.next != null) {
if(node.key == key) {
node.next = node.next.next;
return;
}
}
}
}
private class Node {
int key, value;
Node next;
Node(int key, int value) {
this.key = key;
this.value = value;
}
}
}
I can do relatively small commands without it taking to long, like this:
MyHashMap hashMap = new MyHashMap();
hashMap.put(1, 1);
hashMap.put(2, 2);
System.out.println(hashMap.get(1)); // returns 1
System.out.println(hashMap.get(3)); // returns -1 (not found)
hashMap.put(2, 1); // update the existing value
System.out.println(hashMap.get(2)); // returns 1
hashMap.remove(2); // remove the mapping for 2
System.out.println(hashMap.get(2)); // returns -1 (not found)
But as soon as I do more things, it takes a much longer time. I tried to see if Java's HashMap was as slow, but it took barely any time. What makes my implementation so much slower? It's not just a few milliseconds, it's about 5 seconds slower when my commands aren't even that much.
I build a simple AVL tree as following, each node has key and value. Now I want to implement a method that could return the key of node which has the largest value. For example, if I have a tree like:
(7,1)
/ \
(4,3) (13,8)
/ \ / \
(2,4) (6,3) (11,8) (15,2)
/ \ / / \ / \
(1,9)(3,0)(5,16)(9,2)(12,3)(14,3)(16,5)
/ \
(8,19)(10,4)
The method would return 8, as the node (8,19) has the largest value. Following is my avl tree and node constructor. I do try to implement this method by hand but somehow it doesn't work. I'd be grateful if someone coule help me.
public class AVLTreeImp<T extends Comparable<? super T>,V> implements AVLTree<T,V>{
private Node<T, V> root;
public class Node<T extends Comparable<? super T>,V> implements AVLTree.Node{
T key;
V value;
Node<T,V> left;
Node<T,V> right;
Node<T,V> parent;
int height;
public Node(){
this.key = null;
this.left = null;
this.right = null;
this.parent = null;
this.height = 0;
this.value = null;
}
public Node(T key, V value, Node<T,V> left, Node<T,V> right){
this.key = key;
this.left = left;
this.right = right;
this.parent = null;
this.height = 0;
this.value = value;
}
}
public AVLTreeImp(){
this.root = null;
}
#Override
public void insert(T key, V value){
root = insert(root,key,value);
}
private Node<T,V> insert(Node<T,V> node, T key, V value){
if (node == null){
node = new Node<T,V>(key, value,null,null);
}else{
if (key.compareTo(node.key) < 0){
node.left = insert(node.left, key, value);
if (!(isBalanced(node))) {
if (key.compareTo(node.left.key) < 0) {
node = leftLeftRotation(node);
} else {
node = leftRightRotation(node);
}
}
}else if (key.compareTo(node.key) > 0){
node.right = insert(node.right,key,value);
if (!(isBalanced(node))){
if (key.compareTo(node.right.key) > 0){
node = rightRightRotation(node);
}else{
node = rightLeftRotation(node);
}
}
}
}
regenerateHeight(node);
return node;
}
Below is my implementation of this method, I'm not sure what's wrong with this.
public Integer findMax(){
Node<Integer,Integer> result = (Node<Integer,Integer>)root;
result.value = 0;
return findMax((Node<Integer, Integer>) root,result);
}
private Integer findMax(Node<Integer,Integer> node,Node<Integer,Integer> result){
if (node == null){
return result.key;
}
if (node.value > result.value ||
(node.value == result.value && node.key.compareTo(result.key) < 0)){
result = node;
}
findMax(node.left,result);
findMax(node.right,result);
return result.key;
}
You have a balanced BST! That means operations like the following are efficient,
Insert/Remove
Max/Min key
Membership Query
But turns out, as comment suggested, you’d have to traverse the entire tree to find an element matching your criteria, which is a O(N) op, not optimal. Worse, your structure is recursive!
You can,
Maintain a priority queue keyed by your “value”
Build another tree keyed by your “value”
They are both far more efficient than a full tree look up.
However, without further context, I find you usage of the tree questionable? Why is your tree keyed by something you’re not operating on?
Your recursive findMax method is incorrect. You are assigning result = node;
but this is only local assignment not updating result when calling findMax(node.left,result); and findMax(node.right,result);
. This should work:
public Integer findMax(){
Node<Integer,Integer> result = (Node<Integer,Integer>)root;
result = findMax((Node<Integer, Integer>) root,result);
return result.key;
}
private Node<Integer,Integer> findMax(Node<Integer,Integer> node,Node<Integer,Integer> result){
if (node == null){
return result;
}
if (node.value > result.value ||
(node.value == result.value && node.key.compareTo(result.key) < 0)){
result = node;
}
result = findMax(node.left,result);
result = findMax(node.right,result);
return result;
}
More about passing java parameters here Is Java "pass-by-reference" or "pass-by-value"?
I'm using a BST. Given a key, how will I find the successor key? This is the code I have so far. I've managed to insert a new key and retrieve a value given the key. Now, I need to finish the next method. How would I approach this?
class BST<K extends Comparable<K>, V> implements RangeMap<K,V> {
class Node {
Node left;
Node right;
Node parent;
KVPair<K,V> kv;
K key;
V value;
public Node(K key, V value) {
this.key = key;
this.value = value;
parent = left = right = sentinel;
}
}
private Node root;
public void add(K key, V value) {
// TODO: Implement me(basic score)
root = add (root, key, value);
}
private Node add(Node x, K key, V value){
if (x == null){
return new Node(key, value); }
int cmp = key.compareTo(x.key);
if (cmp < 0){
x.left = add(x.left, key, value);}
else if (cmp > 0 ){
x.right = add(x.right, key, value);}
else if (cmp == 0){
x.value = value;}
return x;
}
public V get(K key) {
Node x = root;
while (x != null){
int cmp = key.compareTo(x.key);
if (cmp < 0){
x = x.left;}
else if (cmp > 0 ){
x = x.right;}
else if (cmp == 0){
return x.value;}
}
return null;
}
public K next(K key) {
You would need a private method to getNode of key
Then you get successor of that node and return its value
You should also update your "V get(K key)" method to use the getNode(K Key) method to avoid code duplication
I've written all 3 methods below
private Node getNode(K key) {
Node x = root;
while (x != null){
int cmp = key.compareTo(x.key);
if (cmp < 0){
x = x.left;
} else if (cmp > 0 ) {
x = x.right;
} else if (cmp == 0){
return x;
}
}
return null;
}
public K next(K key) {
Node x = getNode(key);
if (x.right != null) {
x = x.right;
while (x.left != null) {
x = x.left;
}
return x.key;
}
Node p = x.parent;
while (p != null && p.right == x) {
p = p.parent;
x = x.parent;
}
return p.key;
}
public V get(K key) {
Node x = getNode(key);
return x==null?null:x.value;
}
When I use MapEntry[] entry = (MapEntry[]) new Object[capacity],
it tells me java.lang.ClassCastException.
How can this happen? I am confused about this. (Since it seems I should do the casting because it's generic)
I found some tutorial and they were using like this:
table = new Entry[capacity];
(http://www.javamadesoeasy.com/2015/02/hashmap-custom-implementation.html)
it did not even do the casting.
My codes are below.
public class MyHashMap<K, V> {
private class MapEntry {
K key;
V value;
MapEntry next;
MapEntry(K key, V value) {
this.key = key;
this.value = value;
}
}
private int size = 0;
private int capacity;
MapEntry[] entry;
#SuppressWarnings("unchecked")
MyHashMap() {
capacity = 10;
entry = (MapEntry[]) new Object[capacity];
}
#SuppressWarnings("unchecked")
MyHashMap(int capacity) {
entry = (MapEntry[]) new Object[capacity];
}
public void put(K key, V value) {
int hash = hashCode(key);
MapEntry newNode = new MapEntry(key, value);
if (entry[hash % capacity] == null) {
entry[hash % capacity] = newNode;
} else {
if (key == entry[hash % capacity].key) {
entry[hash % capacity].value = value;
} else {
MapEntry nextNode = entry[hash % capacity].next;
while (nextNode != null) {
if (key == nextNode.key) {
nextNode.value = value;
return;
}
nextNode = nextNode.next;
}
nextNode = newNode;
}
}
}
public V get(K key) {
int hash = hashCode(key);
MapEntry node = entry[hash % capacity];
if (node == null) {
return null;
}
if (node.key == key) {
return node.value;
}
while (key != node.key) {
node = node.next;
if (node.key == key) {
return node.value;
}
}
return null;
}
public boolean contains(K key) {
return get(key) != null;
}
public int size() {
return size;
}
public void remove(K key) {
int hash = hashCode(key);
MapEntry node = entry[hash % capacity];
if (node == null) return;
if (key == node.key) {
entry[hash % capacity] = node.next;
}
MapEntry pre = node;
while (key != node.key) {
node = node.next;
if (key == node.key) {
pre.next = node.next;
return;
}
pre = pre.next;
}
}
private int hashCode(K key) {
return Math.abs(key.hashCode());
}
public void display(){
for(int i = 0; i < capacity; i++){
if(entry[i] != null){
MapEntry node = entry[i];
while(node != null){
System.out.print("{" + node.key + "=" + node.value + "}" + " ");
node = node.next;
}
}
}
}
public static void main(String[] args) {
MyHashMap<Integer, Integer> hashMapCustom = new MyHashMap<Integer, Integer>();
hashMapCustom.put(21, 12);
hashMapCustom.put(25, 121);
hashMapCustom.put(30, 151);
hashMapCustom.put(33, 15);
hashMapCustom.put(35, 89);
System.out.println("value corresponding to key 21="
+ hashMapCustom.get(21));
System.out.println("value corresponding to key 51="
+ hashMapCustom.get(51));
System.out.print("Displaying : ");
hashMapCustom.display();
System.out.print("Displaying : ");
hashMapCustom.display();
}
}
You can't convert a class of an array by just casting that's yhe reason you get ClassCastException. You should use
`Arrays.copyof ().`
CustomType[]ca=Arrays.copyOf(array,array.length,CustomType[].class);
I have figured out how this work.
(Creation of array whose component type is either a type parameter, a concrete parameterized type or a bounded wildcard parameterized type, is type-unsafe.)
entry = (MapEntry[]) Array.newInstance(MapEntry.class, capacity);
In this way, there can be no errors.
There is another question with good solution.
How to create a generic array in Java?
I was able to write my own Binary Search Tree, but I am having a lot of trouble figuring out how to turn that into a Balanced Binary Search tree.
Could someone help me implement a Balanced binary search tree code with the regular binary tree.
I think I was successful in changing my TreeNode class to have the necessary changes.
I added another key and another value along with another TreeNode middle to hold the middle pointer when you get to a 3 node in the tree.
I then added another constructor to hold the case if it was a 3 node. I believe I did this right.
public class TreeNode<V>
{
public int key;
public int key1;
public V value;
public V value1;
public TreeNode<V> left;
public TreeNode<V> right;
public TreeNode<V> middle;
public TreeNode(int key, V value)
{
this.key = key;
this.value = value;
this.left = null;
this.right = null;
}
public TreeNode(int key, V value, int key1, V value1)
{
this.key = key;
this.key1 = key1;
this.value = value;
this.value1 = value1;
this.left = null;
this.right = null;
this.middle = null;
}
The tough part comes to when I need to change the actual BST Class. I know the put is going to change quite a bit because we have to check and see if it is a 2 node or a 3 node, as well as check for what the parent node is.
Here is what I have so far:
public class BST<V>
{
private TreeNode<V> root;
public BST()
{
this.root = null;
}
public V get(int key)
{
return get(root, key);
}
private V get(TreeNode<V> current, int key)
{
if (current == null)
return null;
else if (key == current.key)
return current.value;
else if (key < current.key)
return get(current.left, key);
else
return get(current.right, key);
}
public void put(int key, V value)
{
if (root == null)
root = new TreeNode<>(key, value);
else
put(root, key, value);
}
private void put(TreeNode<V> current, int key, V value)
{
if (key == current.key)
{
current.value = value;
return;
}
else if (key < current.key)
{
if (current.left == null)
{
current.left = new TreeNode<>(key, value);
return;
}
else
put(current.left, key, value);
}
else
{
if (current.right == null)
{
current.right = new TreeNode<>(key, value);
return;
}
else
put(current.right, key, value);
}
}
}
My difficultly comes most with the recursion. I understand how basic recursion works, but using it to implement a balanced binary search tree is seeming a much more difficult talk than originally thought.
You only want a binary search tree, correct? If so, there isn't really a need for keys (Which are used for M-ary trees).
This isn't exactly an answer, but hopefully this will help simplify your code at least a little bit.