阵列数变为随机数

问题描述 投票:-1回答:1

我是C语言编程的初学者,无法解决问题。我正在尝试打印随机生成的二叉树以输出控制台,并在需要时删除一些叶子。删除后,我需要调整没有该数字的二叉树,因此我所做的是我从数组中删除了数字,并试图调用main函数来创建带有新数组的二叉树。

但是当我想起用于创建新的二叉树的主要功能时,数组变成这样,但每次都不同:

223412224   1195327832   4200751   0   -1921113323   30818502   -276547975   32767   0

但是应该是这样:

7 1 9 4 5 3 8 12 34  

可能是什么问题?我不知道。(当我尝试在再次调用主要功能之前打印新数组时,它可以正确打印)

代码:

#include<stdlib.h>
#include<stdio.h>
int count=4;
int kat=20;
int number=10;
int onetime=1;
struct bin_tree {
int data;
struct bin_tree * right, * left;
};
typedef struct bin_tree node;

void insert(node ** tree, int val)
{
    node *temp = NULL;
    if(!(*tree))
    {
        temp = (node *)malloc(sizeof(node));
        temp->right = temp->left = NULL;
        temp->data = val;
        *tree = temp;
        return;
    }

    if(val < (*tree)->data)
    {
        insert(&(*tree)->right, val);
    }
    else if(val > (*tree)->data)
    {
        insert(&(*tree)->left, val);
    }

}

node* search(node ** tree, int val)
{
    if(!(*tree))
    {
        return NULL;
    }

    if(val > (*tree)->data)
    {
        search(&((*tree)->left), val);
    }
    else if(val < (*tree)->data)
    {
        search(&((*tree)->right), val);
    }
    else if(val == (*tree)->data)
    {
        return *tree;
    }
}

int getLevelCount(node *tree)
{
    if (tree == NULL)
    {
        return 0;
    }
    int leftMaxLevel = 1 + getLevelCount(tree->left);
    int rightMaxLevel = 1 + getLevelCount(tree->right);
    if (leftMaxLevel > rightMaxLevel)
    {
        return leftMaxLevel;
    }
    else
    {
        return rightMaxLevel;
    }
}

void printLevel(node *tree, int level)
{
    int space;
    space=kat*count;
    int i;
    if (tree != NULL && level == 0)
    {
    for (i=kat; i < space; i++) {
        printf(" ");
    }
    printf("%d", tree->data);
    kat=kat-2;
    }   
    else if (tree != NULL)
    {
        printLevel(tree->left, level - 1);
        printLevel(tree->right, level - 1);
    }

}

void printElements(node *tree)
{
    int i;
    int levelCount = getLevelCount(tree);
    for (i = 0; i < levelCount; i++)
    {
        printLevel(tree, i);
        printf("\n");
    }
}

void deltree(node * tree)
{
    if (tree)
    {
        deltree(tree->left);
        deltree(tree->right);
        free(tree);
    }
}

int RandomNumberGenerator(int value)
{
    int i,out;
    out=rand() % 101;
    return out; 
}

int menu(int islem){
    int secim,sil;
    printf("do you wish to delete a number from tree\n");
    printf("1-yes\n");
    printf("0-no\n");
    scanf("%d",&secim);
    if (secim==1){
        printf("Enter the number you want to delete: \n");
        scanf("%d",&sil);
        return sil;
    }
    else{
        return 0;
    }
}

void main()
{
    srand(time(NULL));
    int random_numbers[number];
    int i,islem,pos;
    node *root;
    node *tmp;
    root = NULL;
    while (onetime==1){
        for(i = 0; i < number; i++){
            random_numbers[i] = RandomNumberGenerator(i);
        }
    onetime=0;
    }

    printf("\n");
    printf("Array : ");
    for(i = 0; i < number; i++){
        printf("  %d ",random_numbers[i]);
    }

    for(i=0;i<number;i++){
        insert(&root, random_numbers[i]);
    }

    printf("tree\n");
    printElements(root);    
    islem=menu(1);
    while(islem!=0){
        tmp = search(&root, islem);
        if (tmp)
        {
        printf("Searched node=%d\n", tmp->data);
        for(i = 0; i < number; i++){
            if(random_numbers[i]==islem){
                pos=i+1;
                for(i=pos-1; i<number-1; i++)
                {
                    random_numbers[i] = random_numbers[i + 1];
                }
                number--;
                printf("Node found and deleted\n");
                islem=0;
                main();
            }
        }
}
        else
        {
            printf("Couldnt find node.\n");
            islem=menu(1);
        }
    }
}   
c binary-tree
1个回答
0
投票

编译时,请始终启用警告。然后修复这些警告。

© www.soinside.com 2019 - 2024. All rights reserved.