用c语言实现二叉树 #包括 #包括 #包括 类型定义结构节点{ int键; 结构节点*左; 结构节点*右; }节点; 节点*搜索(节点*根,int x){ 节点*p=根; while(p!=NULL){ 如果(p->key==x){ 返回p; } 否则如果(p->key右; } 否则{ p=p->左; } } 返回NULL; } 节点*插入(节点*根,int x){ 节点*p=根; Node*parent=NULL; while(p!=NULL){ 父母=p; 如果(p->key==x){ printf(“相同的密钥。\n”); 返回p; } 否则如果(p->key右; } 否则{ p=p->左; } } Node*newNode=(Node*)malloc(sizeof(Node)); newNode->key=x; newNode->left=NULL; newNode->right=NULL; 如果(父项!=NULL){ if(父级->键键){ 父->右=新节点; } 否则{ 父节点->左=新节点; } } 返回newNode; } 节点*删除(节点*根,int x){ 节点*p=根; Node*parent=NULL; 而((p!=NULL)&&(p->key!=x)){ 父母=p; 如果(p->key右; } 否则{ p=p->左; } } if(p==NULL){ printf(“没有需要的节点。\n”); 返回根; } 如果(p->left==NULL&&p->right==NULL){ 如果(父项==NULL){ root=NULL; } 否则{ 如果(父->左==p){ 父->左=空; } 否则{ 父->右=空; } } } else if(p->left==NULL | | p->right==NULL){ 节点*child=(p->left!=NULL)?p->left:p->right; 如果(父项==NULL){ 根=子; } 否则{ 如果(父->左==p){ 父->左=子; } 否则{ 父->右=子; } } } 否则{ 节点*succeccor\u parent=p; 节点*SUCCECOR=p->左; while(成功->右侧!=NULL){ succeccor\u parent=succeccor; SUCCECOR=SUCCECOR->右侧; } p->key=succeccor->key; if(succeccor\u parent->right==succeccor){ 辅助父项->右=辅助父项->左; } 否则{ 辅助父项->左=辅助父项->左; } p=救援者; } 自由基(p); } 无效显示(节点*根){ if(root==NULL){ 返回; } printf(“%d\n”,根->键); 显示(根->左); 显示(根->右); } int main(){ //文件*ip=fopen(“C:\\VScode\\final\\input.txt”、“r”); Node*root=insert(NULL,5); 插入(根,2); 插入(根,18); 插入(根,1); 插入(根,3); 插入(根,8); 插入(根,6); 插入(根,11); 插入(根,7); 显示(根); 返回0; }

用c语言实现二叉树 #包括 #包括 #包括 类型定义结构节点{ int键; 结构节点*左; 结构节点*右; }节点; 节点*搜索(节点*根,int x){ 节点*p=根; while(p!=NULL){ 如果(p->key==x){ 返回p; } 否则如果(p->key右; } 否则{ p=p->左; } } 返回NULL; } 节点*插入(节点*根,int x){ 节点*p=根; Node*parent=NULL; while(p!=NULL){ 父母=p; 如果(p->key==x){ printf(“相同的密钥。\n”); 返回p; } 否则如果(p->key右; } 否则{ p=p->左; } } Node*newNode=(Node*)malloc(sizeof(Node)); newNode->key=x; newNode->left=NULL; newNode->right=NULL; 如果(父项!=NULL){ if(父级->键键){ 父->右=新节点; } 否则{ 父节点->左=新节点; } } 返回newNode; } 节点*删除(节点*根,int x){ 节点*p=根; Node*parent=NULL; 而((p!=NULL)&&(p->key!=x)){ 父母=p; 如果(p->key右; } 否则{ p=p->左; } } if(p==NULL){ printf(“没有需要的节点。\n”); 返回根; } 如果(p->left==NULL&&p->right==NULL){ 如果(父项==NULL){ root=NULL; } 否则{ 如果(父->左==p){ 父->左=空; } 否则{ 父->右=空; } } } else if(p->left==NULL | | p->right==NULL){ 节点*child=(p->left!=NULL)?p->left:p->right; 如果(父项==NULL){ 根=子; } 否则{ 如果(父->左==p){ 父->左=子; } 否则{ 父->右=子; } } } 否则{ 节点*succeccor\u parent=p; 节点*SUCCECOR=p->左; while(成功->右侧!=NULL){ succeccor\u parent=succeccor; SUCCECOR=SUCCECOR->右侧; } p->key=succeccor->key; if(succeccor\u parent->right==succeccor){ 辅助父项->右=辅助父项->左; } 否则{ 辅助父项->左=辅助父项->左; } p=救援者; } 自由基(p); } 无效显示(节点*根){ if(root==NULL){ 返回; } printf(“%d\n”,根->键); 显示(根->左); 显示(根->右); } int main(){ //文件*ip=fopen(“C:\\VScode\\final\\input.txt”、“r”); Node*root=insert(NULL,5); 插入(根,2); 插入(根,18); 插入(根,1); 插入(根,3); 插入(根,8); 插入(根,6); 插入(根,11); 插入(根,7); 显示(根); 返回0; },c,C,这是我的密码 结果值为 #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct Node { int key; struct Node *left; struct Node *right; } Node; Node *search(Node *root, int x) { Node *p = root; while (p

这是我的密码

结果值为

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Node {
    int key;
    struct Node *left;
    struct Node *right;
} Node;

Node *search(Node *root, int x) {

    Node *p = root;

    while (p != NULL) {

        if (p->key == x) {
            return p;
        }

        else if (p->key < x) {
            p = p->right;
        }

        else {
            p = p->left;
        }
    }
    return NULL;
}

Node *insert(Node *root, int x) {

    Node *p = root;

    Node *parent = NULL;

    while (p != NULL) {

        parent = p;

        if (p->key == x) {
            printf("same key.\n");
            return p;
        }

        else if (p->key < x) {
            p = p->right;
        }

        else {
            p = p->left;
        }
    }

    Node *newNode = (Node *)malloc(sizeof(Node));
    newNode->key = x;
    newNode->left = NULL;
    newNode->right = NULL;

    if (parent != NULL) {
        if (parent->key < newNode->key) {
            parent->right = newNode;
        }
        else {
            parent->left = newNode;
        }
    }
    return newNode;
}

Node *delete(Node *root, int x) {
    Node *p = root;
    Node *parent = NULL;

    while ((p != NULL) && (p->key != x)) {
        
        parent = p;
        
        if (p->key < x) {
            p = p->right;
        }

        else {
            p = p->left;
        }
    }

    if (p == NULL) {
        printf("No Node you want.\n");
        return root;
    }

    if (p->left == NULL && p->right == NULL) {

        if (parent == NULL) {
            root = NULL;
        }
        
        else {
            if (parent->left == p) {
                parent->left = NULL;
            }

            else {
                parent->right = NULL;
            }
        }
    }

    else if (p->left == NULL || p->right == NULL) {

        Node *child = (p->left != NULL) ? p->left : p->right;

        if (parent == NULL) {
            root = child;
        }
        else {
            if (parent->left == p) {
                parent->left = child;
            }
            else {
                parent->right = child;
            }
        }
    }

    else {
        Node *succeccor_parent = p;
        Node *succeccor = p->left;
        while (succeccor->right != NULL) {
            succeccor_parent = succeccor;
            succeccor = succeccor->right;
        }
        p->key = succeccor->key;
        if (succeccor_parent->right == succeccor) {
            succeccor_parent->right = succeccor_parent->left;
        }
        else {
            succeccor_parent->left = succeccor_parent->left;
        }
        p = succeccor;
    }
    free(p);
}

void display(Node *root) {
    if (root == NULL) {
        return;
    }
    printf("%d\n", root->key);
    display(root->left);
    display(root->right);
}

int main() {

    // FILE *ip = fopen("C:\\VScode\\final\\input.txt", "r");

    Node *root = insert(NULL, 5);
    insert(root, 2);
    insert(root, 18);
    insert(root, 1);
    insert(root, 3);
    insert(root, 8);
    insert(root, 6);
    insert(root, 11);
    insert(root, 7);

    display(root);

    return 0;
}
5. 2. 1. 3. 18 8. 6. 7. 11 但是我想要别的东西

5 2 1 3 18 8 6 7 11 5 2 5 18 2 1 2 3 18 8 8 6 8 11 6 7 我希望父节点和子节点一起显示。 我知道我需要修改一些显示函数,但我不知道具体如何修改

这是另一个问题。 FILE*ip=fopen(“C:\VScode\final\input.txt”、“r”)

我预先创建的文件是“input.txt”,我想将该值传递给插入函数。 我该怎么办


如果您能给我一个建议,我将不胜感激。

您可以通过将
显示更改为打印显示当前节点及其子节点的键的行来获得所需的输出:

5 2 5 18 2 1 2 3 18 8 8 6 8 11 6 7
每个帖子问一个问题。询问有关使用
文件
fopen
的问题应与询问有关显示树的问题分开。基本上,编辑您的问题,使其仅包含其中一个问题,并编辑标题,以明确询问的是哪一个问题。我刚刚意识到有答复。谢谢你帮助我。多亏了你,我才解决了这个问题。
void display(Node *root)
{
    if (root == NULL)
        return;
    if (root->left)
        printf("%d %d\n", root->key, root->left->key);
    if (root->right)
        printf("%d %d\n", root->key, root->right->key);
    display(root->left);
    display(root->right);
}