Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- struct Node
- {
- int Key;
- Node* Left;
- Node* Right;
- };
- typedef Node* Tree;
- Node* TaoNode(int X)
- {
- Node* p= new Node;
- if(p==NULL) return NULL;
- p->Left = p->Right=NULL;
- p->Key= X;
- return p;
- }
- void ThemNodeVaoCay( Node* p,Tree &c)
- {
- if(c==NULL) c=p;
- else
- {
- if(p->Key< c->Key)
- ThemNodeVaoCay(p,c->Left);
- else if(p->Key > c->Key) ThemNodeVaoCay(p,c->Right);
- else return;
- }}
- void Nhap(Tree &c)
- { int chon = 0;
- do
- { int x;
- printf("\n Nhap x: ");
- scanf("%d",&x);
- Node* p= TaoNode(x);
- ThemNodeVaoCay(p,c);
- printf("Bam 1 de tiep tuc nhap, 0 de out");
- scanf("%d",&chon);
- }while(chon);
- }
- void Xuat(Tree c)
- {if (c!=NULL)
- { if(c->Left!=NULL)
- Xuat(c->Left);
- if(c->Right!=NULL)
- Xuat(c->Right);
- }
- }
- void LNR(Tree c)
- { if(c!=NULL)
- { LNR(c->Left);
- LNR(c->Right);
- printf(" %d " , c->Key);
- }
- }
- void main()
- {
- Tree c = NULL;
- Nhap(c);
- printf(" Xuat theo LNR : " )
- Xuat(c);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement