Hi all,
Can someone help in devicing an efficient method for building a tree for representing a list as list represented using trees take less time to traverse and delete...
Thanks in advance
Regards
C_Enthusiast
Hi all,
Can someone help in devicing an efficient method for building a tree for representing a list as list represented using trees take less time to traverse and delete...
Thanks in advance
Regards
C_Enthusiast
All else depends on traversal type:Code:struct tree_node { char data[80]; struct tree_node *parent; struct tree_node *left_child; struct tree_node *right_child; };
- inorder
- preorder
- postorder
- level-order
I hope it's bug-free... and correct.
Code:#include <stdio.h> #include <stdlib.h> struct tree_node { int data; struct tree_node *parent; struct tree_node *left_child; struct tree_node *right_child; }; int tree_push(struct tree_node *, int); int inorder_traversal(struct tree_node *); int main() { struct tree_node root; int i, val; root.data = 128; root.parent = NULL; root.left_child = NULL; root.right_child = NULL; srand(RAND_MAX - 1); for (i=0; i<20; i++) { val = (rand() % 256) + 1; tree_push(&root, val); } inorder_traversal(&root); printf("\n"); return 0; } int tree_push(struct tree_node *parent, int value) { if (value < (*parent).data) { if ((*parent).left_child != NULL) { tree_push((*parent).left_child, value); } else { (*parent).left_child = malloc(sizeof(struct tree_node)); (*((*parent).left_child)).data = value; (*((*parent).left_child)).parent = parent; (*((*parent).left_child)).left_child = NULL; (*((*parent).left_child)).right_child = NULL; } } else { if ((*parent).right_child != NULL) { tree_push((*parent).right_child, value); } else { (*parent).right_child = malloc(sizeof(struct tree_node)); (*((*parent).right_child)).data = value; (*((*parent).right_child)).parent = parent; (*((*parent).right_child)).left_child = NULL; (*((*parent).right_child)).right_child = NULL; } } return 0; } int inorder_traversal(struct tree_node *parent) { if (parent == NULL) { return 0; } else { inorder_traversal((*parent).left_child); printf(" %d",(*parent).data); inorder_traversal((*parent).right_child); } return 0; }
The above code is just to create a binary tree... I want to create a tree in which external nodes represents linked list nodes and internal nodes contain the count of linked list nodes to their left side....
This is something like creating a tree for huffman codes but in this case an almost balanced binary tree can be created....
Regards
C_Enthusiast
If you sort your input, you have a linked list.
Stop providing code without having the OP make any effort in solving his own problem.
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
A graphic representation would really help...
Seriously there are pictures, diagrams, videos, applets and tutorials galore, on the net about AVL trees... Let me google that for you
A few of the regular contributors here even have information about them on their sites.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
I was talking to C_Enthusiast...
I am still here....
I cannot provide any pictures butt his idea of representing list using trees is given in book data structures using c and c++ by tenanbaum... page number 308 section 5.4
I am not getting that why they want to so this after all creating a tree would have been better...
Thanks for your help till now
Regards
C_Enthusiast
@C_Enthusiast: At least you can draw something...
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Hi all,
Nothing better that this I can provide... This shows a linked list and the corresponding tree representation in which the internal nodes contain the number of linked list nodes present to its left and the external nodes represent the nodes of linked list..
Hope this will be helpful....
Thnaks in advance
Regards
C_Enthusiast