0

I'm trying to write a Java program that will create a Database using a Binary Search Tree whose keys will be the make of a car (Chevy for example). And that node will contain a Linked list that will contain more detailed information about the cars.

My cars are added in a Linked List class called DBTreeNode.

Can I modify the BST implementation here, to make the data of the Node be a Linked List?

1 Answer 1

1

One option would be to add your DBTreeNode List as a member of BST Node along with other fields like Left, Right, etc... Then add accessors (getter, setter) for DBTreeNode. Hope this helps. Good Luck!

Here is an Example:

public class BST<Key extends Comparable<Key>, Value> {
    private Node root;             // root of BST

    private class Node {
        private Key key;           // sorted by key
        private Value val;         // associated data
        private Node left, right;  // left and right subtrees
        private int N;             // number of nodes in subtree
    private DBTreeNode VehicleDetails; // your list

        public Node(Key key, Value val, int N) {
            this.key = key;
            this.val = val;
            this.N = N;
    this.VehicleDetails = new DBTreeNode(); // initialize your list
        }

    public DBTreeNode getDetails(){
        return this.VehicleDetails; 
    }

    public void addDetails(DBTreeNode details){
        for(DBTreeNodeElement detail : details) this.VehicleDetails.add(detail);
    }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks this is what I was thinking, I'm just getting confused in the implementation. I'll give it a shot.
That helps me get started. Now my only issue is I'm having trouble traversing the tree to see if the details are adding correctly. Am I missing an easy toString method that I could try?
Actually never mind I figured it out. Thank you for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.