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.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
noon
  • 21
  • 1
  • 2

3 Answers3

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.

js_248
  • 2,032
  • 4
  • 27
  • 38
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.

Community
  • 1
  • 1
Mad Matts
  • 1,118
  • 10
  • 14
-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;
    }

}
sg11
  • 21