Title: Binary Search Tree (Homework) - INF

Exercise05:BinarySearchTree
Title:BinarySearchTree(Homework)
SubmissionDeadline:December9,2016/12:00pm
05-01: Binary Search Tree
We wish to build a Binary Search Tree, which is able to store data in a LinkedList. Therefore, make yourself familiar with the Java implementation of java.util.LinkedList .
Please write a public class LinkedTree <T> containing the Inner Node Class private
class Node<E>:
//the key field
//list of data associated with a key
//reference to the left child
//reference to the right child
left
private Node(int key, E element,
Node<E> leftChild, Node<E> rightChild) {
this.key
= key;
key
this.data = new LinkedList<E>();
this.left = leftChild;
this.right = rightChild;
}
private Node(int key, E element) {
this(key, data, null, null);
}
right
private class Node<E> {
private int
key;
private LinkedList<E> data;
private Node<E>
left;
private Node<E>
right;
data
}
Please implement the following function to insert the specified (key, element) pair in the
tree so that the tree remains a binary tree.
public void insert(int key, E element){ … }
insert(k,e):
search for a node whose key is k.
if (a node with the key k exist)
add element e to the linkedList data associated with the key k
else
insert a new node(key k, element e) in the binary tree
03 Point
Exercise05:BinarySearchTree
Please implement the following function to search for the specific key in the binary
tree.
02
Point
public LinkedList<E> search(int key){ … }
search(k):
returns the list of data associated with the key k
Please implement the following functions to print an element of the LinkedList
data associated with the keys of the tree in the order given by a preorder, postorder
and inorder traversal.
public void preorder(int ind){ … }
public void postorder (int ind) { … }
public void inorder (int ind) { … }
preorder(ind):
perform the preorder traversal for the root of the tree
for each node
if ( size of linkedList data is greater than index ind)
return the element of the linkedList data having the index ind
postorder(ind):perform the postorder traversal for the root of the tree
for each node
if ( size of linkedList data is greater than index ind)
return the element of the linkedList data having the index ind
inorder(ind):
perform the inorder traversal for the root of the tree
for each node
if ( size of linkedList data is greater than index ind)
return the element of the linkedList data having the index ind
04
Point
Exercise05:BinarySearchTree
Please implement the following function to delete the node containing the (key,
element) pair with the specified key
03
Points public LinkedList<E> delete(int key){ … }
delete(k):
search for a node whose key is k.
delete the node with the key k from the binary tree
return the list of data associated with a key k
Within the main function, please create the binary search tree as depicted below.
01 Point key:16
key:8
key:23
ind 0
R
key:4
2
T
key:42
key:15
ind 0
A
ind 0
D
1
D
1 2
R M
1
C
2
E
ind 0 1
H O
ind 0 1
M I
2
P
2
L
ind 0
A
1
H
2
E
What is the output of the following Java code for the binary tree :
tree.inorderPrint(0);
tree.postorderPrint(1);
tree.preorderPrint(2);
Total 13
Points