Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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)//empty tree
        {
            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) //go left
            {
                tmp = tmp.left;
            }
            else
            {
                tmp = tmp.right;
            }
        }
        Node<T> newNode = new Node<T>(newData);
        if (newData.CompareTo(tmpParent.data) < 0) //left child
        {
            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);
        //case if the list is empty
        if (start == null)
        {
            start = temp;
        }
        //case if list has only 1 item
        else if (end == null)
        {
            end = start;
            start = temp;
            start.next = end;
            end.prev = start;
        }
        //case when list has 2 or more items
        else
        {
            start.prev = temp;
            temp.next = start;
            start = temp;
        }
    }

    public void AddLast(T dataToAdd)
    {
        Node<T> temp = new Node<T>(dataToAdd);
        //case if the list is empty
        if (start == null)
        {
            start = temp;
        }
        //case if list has only 1 item
        else if (end == null)
        {
            start.next = temp;
            end = start.next;
            end.prev = start;
        }
        //case when list has 2 or more items
        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.
Posted
Updated 24-Jul-17 1:35am
Comments
Graeme_Grant 23-Jul-17 16:27pm    
If you are only using X property for calculating position, then why is the Box class testing Y property in the CompareTo method?
George Swan 24-Jul-17 1:09am    
I'm not quite sure what you are trying to achieve. Based on my best guess, I would suggest the following. Make your linked list a public property of your Box class. To add an item, first search the tree. If a match is found, add the new node to the found node's linked list. If there is no match, add the new node to the tree.

1 solution

First answer:

The idea is that when I compare 2 boxes I will begin with comparing the X values, and if they are the same, I will compare the Y values.
That's the way I will know if the new box is bigger than the previous one or not.

Second answer:
So what you are saying is that instead of the Y property in the box class, I will replace it with a linked list that contains the Y values which related to the X?
That's interesting.

I will try this.

But isn't there a way to add a reference from the tree itself to the linked list?
I would strongly prefer to keep the box class as clean as possible and do most of the connections between the bst and the linked list classes.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900