问题描述:

I am trying to write a program using binary search trees, we have to use binary search trees, that will hopefully show the exact date in 2016 when a user inputs any number (day number) between 1 and 366 (leap year), e.g

>enter a number: 1

Friday the 1st of January!

I am currently just trying to make the tree and then will get on to mapping the numbers to the dates (not sure how yet), but I don't know how to actually make a tree with 366 integers in it. Ive got structures for a node and functions for creating a new tree and to insert in to the tree etc, but not sure where to go from here. Code below:

//Binary search tree implementation

#include <stdio.h>

#include <stdlib.h>

//node structure

struct node {

int data;

struct node* left;

struct node* right;

} ;

//tree wrapper structure

struct tree {

struct node *root;

} ;

typedef struct tree Tree;

typedef struct node Node;

//create a new tree

Tree *new_tree() {

Tree *t = malloc(sizeof(Tree));

t->root = NULL;

return t;

}

//create a new node

Node* NewNode(int data) {

Node* node = malloc(sizeof(Node));

node->data = data;

node->left = NULL;

node->right = NULL;

return(node);

}

//insert in to the binary tree

Node* insert(Node* node, int data) {

//If the tree is empty, return a new, single node

if (node == NULL) {

return(NewNode(data));

}

else {

// 2. Otherwise, recur down the tree

if (data <= node->data) node->left = insert(node->left, data);

else node->right = insert(node->right, data);

return(node); // return the (unchanged) node pointer

}

}

int main(){

return 0;

}

相关阅读:
Top