在C

时间:2017-10-18 19:51:17

标签: c

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

typedef struct Tree {
    int data;
    struct Tree *l,*r;
}node;

node *root = NULL;

node *addNode(int n) {
    node *p=(node *)malloc(sizeof(node));
    p->data=n;
    p->r=NULL;
    p->l=NULL;
    return p;
}

node *Node(node *parent,int data) {
    if (parent == NULL) {
        parent=addNode(data);
        return parent;
    }

    if(parent->data>data)
        parent->l=Node(parent->l,data);

    if(parent->data<data)
        parent->r=Node(parent->r,data);

    return parent;
}

int countNode(node *parent) {
    int count=1;

    if (parent == NULL) {
        return 0;
    } else {
        count++;
        countNode(parent->l,count);
        countNode(parent->r,count);
        return count;
    }
}

void display( node *tree) {
    if(tree==NULL)
        return;
    display(tree->l);
    printf("%d ", tree->data);
    display(tree->r);
}

void main() {
    int no,i,n,search,numberNode,co=1;
    char c;

    do {
        system("cls");
        printf("\n1.Insert \n5.CountNode \nEnter Your Choice ");
        scanf("%d",&n);
        switch(n) {
            case 1:
                printf("\nEnter Number of Elements :- ");
                scanf("%d",&c);
                for( i=1;i<=c;i++){
                    printf("Element(%d) :- ",(i));
                    scanf("%d",&n);
                    root=Node(root,n);
                }
                break;
            case 2:
                display(root);
                break;
            case 3:
                numberNode=countNode(root);
                printf("\nNumber of Node Are :- %d",numberNode);
        }
        printf("\nDo you want to continue? : ");
        fflush(stdin);
        scanf("%c",&c);
    } while(c=='y');
}

我正在学习数据结构,我想计算节点。我已经在C中完成了它但是我再次插入元素,它没有给出,它给出了旧值。例如,首先我插入了两个元素。当我们选择计数节点时再给它两个节点我插入五个元素并选择计数节点它只给出两个元素。

0 个答案:

没有答案