Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*this program builds a binary tree*/
- #include <stdio.h>
- #include <stdlib.h>
- /*external variables*/
- struct node{
- int data;
- struct node* left;
- struct node* right;
- };
- struct node* root = NULL;
- /*prototypes*/
- struct node* constructor(int number);
- struct node* addNode(int number, struct node* current);
- /*main*/
- /*since there isn't a real tree or any data, main looks a bit skant*/
- /*but the other functions are what really matter here*/
- int main(int argc, char *argv[])
- {
- int number = 0;
- if (argc == 2)
- {
- number = atoi(argv[1]);
- }
- addNode(number, root);
- return 0;
- }
- /*makes a new node, or sets the root if it's the first*/
- struct node* constructor(int number)
- {
- struct node* newNode = (struct node*)malloc(sizeof(struct node));
- if (newNode == NULL)
- {
- printf("Error allocating new node!\n");
- return NULL;
- }
- newNode->data = number;
- newNode->left = NULL;
- newNode->left = NULL;
- if (root == NULL)
- {
- root = newNode;
- }
- return newNode;
- }
- struct node* addNode(int number, struct node* current)
- {
- /*if we're at the end of a branch*/
- if (current == NULL)
- {
- constructor(number);
- }
- /*if we need to go down branches further*/
- if (number < current->data)
- {
- addNode(number, current->left);
- }
- else if (number > current->data)
- {
- addNode(number, current->right);
- }
- else
- {
- printf("dickbutt");
- }
- return current;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement