Hi all!
I want to create a bst which contains an object (box) with 2 parameters (x and y), when the x is the parameter that the bst is based on, and the y is a parameter associated to the specific leaf in the tree via linked list.
For example, if I have 3 objects: 1- (x=4, y=6), 2- (x=4, y=7), 3- (x=5, y=8).
The tree will hold only the x values (4 and 5), and the 4 leaf will have a reference to a linked list with nodes 6 and 7. The leaf with the value 5 will have a reference to another linked list with only one node- 8.
I have properties x and y in the box class and the tree gets the whole box as a generic object.
I have built the tree and the linked list classes but can't think of a way to connect them both. Ideas?
class Box : IComparable<Box>
{
public double X { get; set; }
public double Y { get; set; }
public int CompareTo(Box otherBox)
{
int result = this.X.CompareTo(otherBox.X);
if (result == 0)
{
result = this.Y.CompareTo(otherBox.Y);
}
return result;
}
}
class Tree<T> where T : IComparable<T>
{
Node root = null;
public void Add(T newData)
{
if (root == null)
{
root = new Node<T>(newData);
return;
}
Node<T> tmp = root;
Node<T> tmpParent = null;
while (tmp != null)
{
tmpParent = tmp;
if (newData.CompareTo(tmp.data) < 0)
{
tmp = tmp.left;
}
else
{
tmp = tmp.right;
}
}
Node<T> newNode = new Node<T>(newData);
if (newData.CompareTo(tmpParent.data) < 0)
{
tmpParent.left = newNode;
}
else
{
tmpParent.right = newNode;
}
}
private class Node<T>
{
public T data;
public Node<T> left;
public Node<T> right;
public Node(T data)
{
this.data = data;
left = right = null;
}
}
}
public class MyLinkedList<T>
{
Node<T> start;
Node<T> end;
public void AddToStart(T dataToAdd)
{
Node<T> temp = new Node<T>(dataToAdd);
if (start == null)
{
start = temp;
}
else if (end == null)
{
end = start;
start = temp;
start.next = end;
end.prev = start;
}
else
{
start.prev = temp;
temp.next = start;
start = temp;
}
}
public void AddLast(T dataToAdd)
{
Node<T> temp = new Node<T>(dataToAdd);
if (start == null)
{
start = temp;
}
else if (end == null)
{
start.next = temp;
end = start.next;
end.prev = start;
}
else
{
end.next = temp;
temp.prev = end;
end = temp;
}
}
private class Node<T>
{
public T data;
public Node<T> next;
public Node<T> prev;
public Node(T newData)
{
data = newData;
next = null;
prev = null;
}
}
}
}
What I have tried:
I know I need to add the reference within the tree class, but not sure how to split the x and the y values.