Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- struct node
- {
- int value;
- struct node *left_child, *right_child;
- };
- struct node *new_node(int value)
- {
- struct node *tmp = (struct node *)malloc(sizeof(struct node));
- tmp->value = value;
- tmp->left_child = tmp->right_child = NULL;
- return tmp;
- }
- void print(struct node *root_node) // displaying the nodes!
- {
- if (root_node != NULL)
- {
- print(root_node->left_child);
- printf("%d \n", root_node->value);
- print(root_node->right_child);
- }
- }
- struct node* insert_node(struct node* node, int value) // inserting nodes!
- {
- if (node == NULL) return new_node(value);
- if (value < node->value)
- {
- node->left_child = insert_node(node->left_child, value);
- }
- else if (value > node->value)
- {
- node->right_child = insert_node(node->right_child, value);
- }
- return node;
- }
- void afisarePreordine(struct node *root)
- {
- if(root != NULL)
- {
- printf("%d ",root->value);
- afisarePreordine(root->left_child);
- afisarePreordine(root->right_child);
- }
- }
- void afisareInordine(struct node *root)
- {
- if(root != NULL)
- {
- afisareInordine(root->left_child);
- printf("%d ",root->value);
- afisareInordine(root->right_child);
- }
- }
- int main()
- {
- struct node *root_node = NULL;
- root_node = insert_node(root_node, 10);
- insert_node(root_node, 10);
- insert_node(root_node, 30);
- insert_node(root_node, 25);
- insert_node(root_node, 36);
- insert_node(root_node, 56);
- insert_node(root_node, 78);
- print(root_node);
- printf("Afisare preordine");
- printf("\n");
- afisarePreordine(root_node);
- printf("Afisare inordine");
- afisareInordine(root_node);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement