简体   繁体   English

在C中创建右偏二叉树

[英]Creating a right skewed binary tree in C

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 . 在这个程序中,用户应该能够从输入整数序列创建任意的二进制树,他们应该能够选择之间进行balancedleft_onlyright_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? 如何调整此代码?

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 例如,插入10、7、9、12、6

在此处输入图片说明

If you insert ordered set you will make a vine. 如果您插入有序集,您将制造一棵葡萄树。 For example 3, 4, 6, 7, 9, 10, 11, 12 ,14 例如3、4、6、7、9、9、10、11、12、14

在此处输入图片说明

Also, you can make something like 另外,您可以制作类似

在此处输入图片说明

if insert 10, 50, 15, 45, 20, 35, 30, 34, 31 (ordered and unordered sets, one after another). 如果插入10、50、15、45、20、35、30、34、31(有序和无序集合,一个接一个)。 It is, like vine, has O(n) for search. 它像藤一样具有O(n)进行搜索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM