Create an AVL Tree Map. That is, create an AVLTree which implements a Map interf
ID: 3845605 • Letter: C
Question
Create an AVL Tree Map. That is, create an AVLTree which implements a Map interface. The file which needs to be worked on is AVLTreeMap.java (the functions which need to be filled out are marked by \FIX). I have included all relevant files which are: Main.java (runs a test case), Map.java (the map interface) and AVLTreeMap.java (the file which needs to be worked on).
\AVLTreeMap.java
public class AVLTreeMap implements Map {
class Node {
String key;
String value;
int height;
Node parent;
Node left;
Node right;
public Node(String key, String value) {
this.key = key;
this.value = value;
this.height = 1;
this.parent = null;
this.left = null;
this.right = null;
}
public int balance() {
// FIX
return -1;
}
}
private int size;
private Node root;
public AVLTreeMap() {
// FIX
}
public int size() {
// FIX
return -1;
}
public void put(String key, String value) {
// FIX
}
public String get(String key) {
// FIX
return null;
}
public void print() {
this.print(this.root, "", 0);
}
\Map.java
public interface Map {
public int size();
public void put(String key, String value);
public String get(String key);
}
\Main.java
public class Main {
public static void main(String[] args) {
Map map = new AVLTreeMap();
map.put("dog", "a domesticated canid");
map.put("cat", "a small domesticated carnivore");
System.out.println(map.size()); // 2
System.out.println(map.get("dog")); // "a domesticated canid"
}
}
Explanation / Answer
// A utility function to print preorder traversal
// of the tree.
// The function also prints height of every node
void preOrder(struct Node *root)
{
if(root != NULL)
{
printf("%d ", root->key);
preOrder(root->left);
preOrder(root->right);
}
}
/* Drier program to test above function*/
int main()
{
struct Node *root = NULL;
/* Constructing tree given in the above figure */
root = insert(root, 10);
root = insert(root, 20);
root = insert(root, 30);
root = insert(root, 40);
root = insert(root, 50);
root = insert(root, 25);
/* The constructed AVL Tree would be
30
/
20 40
/
10 25 50
*/
printf("Preorder traversal of the constructed AVL"
" tree is ");
preOrder(root);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.