In this program, the user should be able to create an arbitrary binary tree from a sequence of input integers and they should be able select between to balanced
, left_only
, right_only
. I created it for balanced binary tree, but now I am unable to adjust it to right only and left only tree.
struct node {
int data;
struct node* left;
struct node* right;
};
struct tree {
struct node* root;
};
int init(struct tree* t) {
t->root = 0;
return 1;
}
struct node* makeNode(int d) {
struct node* ptr;
ptr = (struct node*)malloc(sizeof(struct node));
ptr->data = d;
ptr->left = ptr->right = 0;
return ptr;
}
// sorting the binary tree
struct node* insertrchild(struct node* n, int d) {
return (n->right = makeNode(d));
}
struct node* insertlchild(struct node* n, int d) {
return (n->left = makeNode(d));
}
struct node* insertbst(struct node** ptr, int d) {
if (*ptr == 0)
return (*ptr = makeNode(d));
if (d > (*ptr)->data)
insertbst(&(*ptr)->right, d);
else
insertbst(&(*ptr)->left, d);
}
void inorder(struct node* ptr) // Print Tree
{ // Perform Inorder Traversal of tree
if (ptr == 0) {
return;
}
inorder(ptr->left);
printf(" %d ", ptr->data);
inorder(ptr->right);
}
void preorder(struct node* ptr) {
if (ptr == 0)
return;
printf("%i\t", ptr->data);
preorder(ptr->left);
preorder(ptr->right);
}
void postorder(struct node* ptr) {
if (ptr == 0)
return;
postorder(ptr->left);
postorder(ptr->right);
printf("%i\t", ptr->data);
}
How do I adjust this code?