-1

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?

NetVipeC
  • 4,402
  • 1
  • 17
  • 19
user2750830
  • 17
  • 3
  • 12
  • 1
    There is nothing in your code that balances your tree; the balance (or unbalance) of the tree is controlled solely by the order in which the nodes are presented for insertion into the tree. – Jonathan Leffler Sep 02 '14 at 16:20
  • I have code for balancing. i used the DSW balancing algorithm which is not included here. – user2750830 Sep 03 '14 at 03:54

1 Answers1

2

Binary tree, as I always understood that structure (maybe, I am totally wrong), has an order. Each new element, less than current node, goes to left. Each node, greater than current node goes to right. Or vice versa.
In your example you have functions that put new node on the left or on the right. So what's the problem? Call right insert and you will have right vine, call left insert and you will have left vine, but there would be no sense (IMHO).
Usually, balanced binary tree is a result of insertion of good distributed values (hence, shuffled). For example, insert 10, 7, 9, 12, 6

enter image description here

If you insert ordered set you will make a vine. For example 3, 4, 6, 7, 9, 10, 11, 12 ,14

enter image description here

Also, you can make something like

enter image description here

if insert 10, 50, 15, 45, 20, 35, 30, 34, 31 (ordered and unordered sets, one after another). It is, like vine, has O(n) for search.

Ivan Ivanov
  • 2,076
  • 16
  • 33