Tree(cay)

// Tree.cpp : Defines the entry point for the console application.

#include "stdafx.h"

struct node

{

    int data;//[type] data;

    node *pLeft;

    node *pRight;

};

node *Getnode(int x)

{

    node *p= new node;

    if (p==NULL)

        return NULL;

    p->data=x;

    p->pLeft=NULL;

    p->pRight=NULL;

    return p;

}

int Addnode(node* &TREE, node *p)

{

    if(TREE ==NULL)

    {

        TREE = p;

        return 1;

    }

    if(TREE->data>p->data)

        return Addnode(TREE->pLeft,p);

    if(TREE->data<p->data)

        return Addnode(TREE->pRight,p);

    else

        return 0;

}

void NLR(node *TREE)//randoom

{

    if( TREE ==NULL)

        return;

    printf("%d",TREE->data);

    NLR(TREE->pLeft);

    NLR(TREE->pRight);

}

void LNR(node *TREE)//tang dan

{

    if(TREE==NULL)

        return;

    LNR(TREE->pLeft);

    printf("%d",TREE->data);

    LNR(TREE->pRight);

}

void RNL(node *TREE)//giam dan

{

    if(TREE==NULL)

        return;

    RNL(TREE->pRight);

    printf("%d",TREE->data);

    RNL(TREE->pLeft);

}

void LRN(node *TREE)//randoom

{

    if(TREE==NULL)

        return;

    LRN(TREE->pLeft);

    LRN(TREE->pRight);

    printf("%d",TREE->data);

}

void ReadFile(node* &TREE)

{

    FILE *f= fopen("input.txt","rt");

    while(!feof(f))

    {

        fscanf(f,"%d",&temp);

        node *p=Getnode(temp);

        Addnode(TREE,p);

    //    printf("%d

",TREE);

    }

}

int _tmain(int argc, _TCHAR* argv[])

{

    node *TREE =new node;

    TREE= NULL;

    ReadFile(TREE);

    LNR(TREE); 

}

Bạn đang đọc truyện trên: AzTruyen.Top

Tags: