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.
-
Simple. Add a reference to a linked list in your node class. – nanofarad Apr 28 '16 at 19:09
-
You need a double linked list for a binary tree though – raven Apr 28 '16 at 19:15
-
Ok ,I don`t know how but I`ll find out how..think you – noon Apr 28 '16 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 – noon Apr 28 '16 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? – Nadir Apr 28 '16 at 19:25
-
I `ll implement the binary tree in linked list .. cuz I have to chose either array or binary tree – noon Apr 28 '16 at 19:35
3 Answers
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.

- 2,032
- 4
- 27
- 38
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.
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;
}
}

- 21
-
1Incomplete, barely relevant, wrong language. Don't rush your answers, pay attention. – qlown May 22 '17 at 06:15
-
-
1You got a downvote (not me) and might wonder why. I forgot to leave this: [answer] – qlown May 23 '17 at 06:30