0

I am wondering if I can get some help in creating a binary tree using a linked list. In each node there is a smaller linked list to hold the information for that specific node. I'm looking for a hint on how to make each node hold another linked list. This is for homework.

6
  • Simple. Add a reference to a linked list in your node class. Commented Apr 28, 2016 at 19:09
  • You need a double linked list for a binary tree though Commented Apr 28, 2016 at 19:15
  • Ok ,I dont know how but Ill find out how..think you Commented Apr 28, 2016 at 19:15
  • no it is single linked list ..I have to go to each node when I`m looking for a specific info in a specific node Commented Apr 28, 2016 at 19:18
  • @noon a binary tree does not require list. Each node should have to variables node, leftChild and rightChild. Why do you want a list? Commented Apr 28, 2016 at 19:25

3 Answers 3

2

If you want tree node data as linked list instead of integer then you can use this

public class Tree {
    static class LinkedList{
        int data;
        LinkedList next;
        public LinkedList(int data, LinkedList next){
            this.data = data;
            this.next = next;
        }
    }
    LinkedList node;
    Tree left;
    Tree right;
    public Tree(LinkedList node, Tree left, Tree right){
        this.node = node;
        this.left = left;
        this.right = right;

    }
    public static void main(String[] args) {
        LinkedList l3 = new LinkedList(3,null);
        Tree node3 = new Tree(l3,null, null);//left child
        LinkedList l4 = new LinkedList(4,null);
        Tree node4 = new Tree(l4,null, null);//right child
        LinkedList l1 = new LinkedList(1,null);
        LinkedList l2 = new LinkedList(2, l1);
        Tree node1 = new Tree(l2,node3, node4);//node1 be the root of tree
    }
}

Here each node of a tree will be holding a linked list.

Sign up to request clarification or add additional context in comments.

Comments

1

There is a difference between a LinkedList and a Binary search tree Difference between a LinkedList and a Binary Search Tree . The principle for a BST is called 'composition'. e.g. composition ≠ inheritance Difference between Inheritance and Composition. A Binary Search Tree is simply an ordered binary tree for storing composition Items. Because it's binary, you could go either left or right in each Node. So each Node Object should have a leftNode and a rightNode as Composition. This should get you started to do your homework on your own.

Comments

-1

can you see if this answers your question? I got this from another person from geeksforgeeks.org

/*
The structure of Link list node is as follows 
struct node
{
    int data;
    struct node* next;
};

The structure of TreeNode is as follows
struct TreeNode
{
    int data;
    TreeNode *left;
    TreeNode *right;
};
*/

TreeNode* newTreeNode(int data)
{
    TreeNode *temp = new TreeNode;
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}
/*You are required to complete this method*/
void convert(node *head,TreeNode * &root)
{
 queue<TreeNode *> q;
    root=newTreeNode(head->data);
    head=head->next;
    q.push(root);
    while(!q.empty()){
        TreeNode *tmp=q.front();
        q.pop();
        if(head){
            tmp->left=newTreeNode(head->data);
            q.push(tmp->left);
            head=head->next;
        }
        if(head){
            tmp->right=newTreeNode(head->data);
            q.push(tmp->right);
            head=head->next;
        }
        if(!head)
            break;
    }

}

3 Comments

Incomplete, barely relevant, wrong language. Don't rush your answers, pay attention.
@qlown - do you know you are mean?
You got a downvote (not me) and might wonder why. I forgot to leave this: How to Answer

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.