Using the files below, create an AVL Tree Map in java (fill in the places where
ID: 3829537 • Letter: U
Question
Using the files below, create an AVL Tree Map in java (fill in the places where it says // fix). In other words, create an AVL Tree which implements the Map interface. The Map interface as well as a template AVLTreeMap.java class is given below.
\Map.java Interface
\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);
}
Explanation / Answer
AVL Tree Map in 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() {
return -1;
}
}
private int size;
private Node root;
public AVLTreeMap() {
}
public int size() {
return -1;
}
public void put(String key, String value) {
}
public String get(String key) {
return null;
}
public void print() {
this.print(this.root, "", 0);
}
import java.util.Scanner;
/* category AVLNode */
category AVLNode
creator */
public AVLNode()
/* builder */
public AVLNode(int n)
}
/* category AVLTree */
category AVLTree
personal AVLNode root;
/* builder */
public AVLTree()
/* operate to examine if tree is empty */
public mathematician isEmpty()
come root == null;
}
/* create the tree logically empty */
public void makeEmpty()
/* operate to insert information */
public void insert(int data)
/* operate to urge height of node */
personal int height(AVLNode t )
come t == null ? -1 : t.height;
}
/* operate to GHB of left/right node */
personal int max(int lhs, int rhs)
come lhs > rhs ? lhs : rhs;
}
/* operate to insert information recursively */
personal AVLNode insert(int x, AVLNode t)
else if( x > t.data )
else
; // Duplicate; do nothing
t.height = max( height( t.left ), height( t.right ) ) + 1;
return t;
}
/* Rotate binary tree node with left child */
private AVLNode rotateWithLeftChild(AVLNode k2)
/* Rotate binary tree node with right child */
private AVLNode rotateWithRightChild(AVLNode k1)
/**
* Double rotate binary tree node: first left child
* with its right child; then node k3 with new left child */
private AVLNode doubleWithLeftChild(AVLNode k3)
/**
* Double rotate binary tree node: first right child
* with its left child; then node k1 with new right child */
private AVLNode doubleWithRightChild(AVLNode k1)
/* Functions to count number of nodes */
public int countNodes()
private int countNodes(AVLNode r)
}
/* Functions to search for an element */
public boolean search(int val)
private boolean search(AVLNode r, int val)
found = search(r, val);
}
return found;
}
/* Function for inorder traversal */
public void inorder()
private void inorder(AVLNode r)
private void preorder(AVLNode r)
private void postorder(AVLNode r)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.