Chào các bạn! Vì nhiều lý do từ nay Truyen2U chính thức đổi tên là Truyen247.Pro. Mong các bạn tiếp tục ủng hộ truy cập tên miền mới này nhé! Mãi yêu... ♥

cay nhi phan tim kiem 1

Viết hàm xuất các giá trị trong cây

Code: 

#include <stdio.h>

struct Node

{

Node* pLeft;

Node* pRight;

int iX;

};

typedef Node* Tree;

Node* TaoNode(int X)

{

Node* p = new Node;

if (p == NULL)

return NULL;

p->pLeft = NULL;

p->pRight = NULL;

p->iX=X;

return p;

}

void ThemNodeVaoCay(Node* p, Tree &c)

{

if (c == NULL)//nếu cây rỗng

c = p;

else //cây khác rỗng

{

if (p->iX < c->iX)

ThemNodeVaoCay(p,c->pLeft);

else if (p->iX > c->iX)

ThemNodeVaoCay(p,c->pRight);

else

return;

}

}

void Nhap(Tree &c)

{

int chon = 0;

do

{

int x;

printf("

Nhap x: ");

scanf_s("%d",&x);

Node* p = TaoNode(x);

ThemNodeVaoCay(p,c);

printf("Muon nhap thong tin tiep ko? 1: co, 0: ko ~~>");

scanf_s("%d",&chon);

}while(chon);

}

// Viết hàm xuất các giá trị trong cây

void Xuat(Tree c)

{

if (c!=NULL)

{

if (c->pLeft != NULL)

Xuat(c->pLeft);

printf("%4d", c->iX);

if (c->pRight != NULL)

Xuat(c->pRight);

}

}

void main()

{

Tree c = NULL;

Nhap(c);

printf("Xuat cay LNR (Tang dan): ");

Xuat(c);

}

Viết hàm xuất các giá trị chẵn trong cây

Code: 

#include <stdio.h>

struct Node

{

Node* pLeft;

Node* pRight;

int iX;

};

typedef Node* Tree;

Node* TaoNode(int X)

{

Node* p = new Node;

if (p == NULL)

return NULL;

p->pLeft = NULL;

p->pRight = NULL;

p->iX=X;

return p;

}

void ThemNodeVaoCay(Node* p, Tree &c)

{

if (c == NULL)//nếu cây rỗng

c = p;

else //cây khác rỗng

{

if (p->iX < c->iX)

ThemNodeVaoCay(p,c->pLeft);

else if (p->iX > c->iX)

ThemNodeVaoCay(p,c->pRight);

else

return;

}

}

void Nhap(Tree &c)

{

int chon = 0;

do

{

int x;

printf("

Nhap x: ");

scanf_s("%d",&x);

Node* p = TaoNode(x);

ThemNodeVaoCay(p,c);

printf("Muon nhap thong tin tiep ko? 1: co, 0: ko ~~>");

scanf_s("%d",&chon);

}while(chon);

}

//871 Viết hàm xuất các giá trị chẵn trong cây

void Xuat(Tree c)

{

if (c!=NULL)

{

if (c->pLeft != NULL)

Xuat(c->pLeft);

if (c->iX % 2 == 0)

printf("%4d", c->iX);

if (c->pRight != NULL)

Xuat(c->pRight);

}

}

void main()

{

Tree c = NULL;

Nhap(c);

printf("Xuat gia tri chan theo LNR (Tang dan): ");

Xuat(c);

}

viết xuất địa chỉ các nút trên cây có giá trị (khoá) lớn hơn x và nhỏ hơn y

Code: 

#include <stdio.h>

struct Node

{

Node* pLeft;

Node* pRight;

int iX;

};

typedef Node* Tree;

Node* TaoNode(int X)

{

Node* p = new Node;

if (p == NULL)

return NULL;

p->pLeft = NULL;

p->pRight = NULL;

p->iX=X;

return p;

}

void ThemNodeVaoCay(Node* p, Tree &c)

{

if (c == NULL)//nếu cây rỗng

c = p;

else //cây khác rỗng

{

if (p->iX < c->iX)

ThemNodeVaoCay(p,c->pLeft);

else if (p->iX > c->iX)

ThemNodeVaoCay(p,c->pRight);

else

return;

}

}

void Nhap(Tree &c)

{

int chon = 0;

do

{

int x;

printf("

Nhap x: ");

scanf_s("%d",&x);

Node* p = TaoNode(x);

ThemNodeVaoCay(p,c);

printf("Muon nhap thong tin tiep ko? 1: co, 0: ko ~~>");

scanf_s("%d",&chon);

}while(chon);

}

//872 Viết hàm xuất địa chỉ các nút trên cây có giá trị (Khóa) lớn hơn X và nhỏ hơn Y

void Xuat(Tree c, int x, int y)

{

if (c!=NULL)

{

if (c->pLeft != NULL)

Xuat(c->pLeft,x,y);

if (x<c->iX && c->iX<y)

printf("

%4d", &c);

if (c->pRight != NULL)

Xuat(c->pRight,x,y);

}

}

void NhapXY(int &x, int &y)

{

printf("Nhap x: ");

scanf_s("%d",&x);

printf("Nhap y: ");

scanf_s("%d",&y);

}

void main()

{

Tree c = NULL;

Nhap(c);

int x,y;

NhapXY(x,y);

printf("

Xuat dia chi < x va > y: ");

Xuat(c,x,y);

}

Viết hàm xuất các số hoàn thiện trong cây

Code: 

#include <stdio.h>

struct Node

{

Node* pLeft;

Node* pRight;

int iX;

};

typedef Node* Tree;

Node* TaoNode(int X)

{

Node* p = new Node;

if (p == NULL)

return NULL;

p->pLeft = NULL;

p->pRight = NULL;

p->iX=X;

return p;

}

void ThemNodeVaoCay(Node* p, Tree &c)

{

if (c == NULL)//nếu cây rỗng

c = p;

else //cây khác rỗng

{

if (p->iX < c->iX)

ThemNodeVaoCay(p,c->pLeft);

else if (p->iX > c->iX)

ThemNodeVaoCay(p,c->pRight);

else

return;

}

}

void Nhap(Tree &c)

{

int chon = 0;

do

{

int x;

printf("

Nhap x: ");

scanf_s("%d",&x);

Node* p = TaoNode(x);

ThemNodeVaoCay(p,c);

printf("Muon nhap thong tin tiep ko? 1: co, 0: ko ~~>");

scanf_s("%d",&chon);

}while(chon);

}

//873 Viết hàm xuất các số hoàn thiện trong cây

bool SoHoanThien(int x)

{

int s=0;

for (int i=1; i<x; i++)

if (x%i==0)

s+=i;

if (s==x)

return true;

return false;

}

void Xuat(Tree c)

{

if (c!=NULL)

{

if (c->pLeft != NULL)

Xuat(c->pLeft);

if (SoHoanThien(c->iX))

printf("%4d", c->iX);

if (c->pRight != NULL)

Xuat(c->pRight);

}

}

void main()

{

Tree c = NULL;

Nhap(c);

printf("Xuat cac so hoan thien theo LNR: ");

Xuat(c);

}

Viết hàm xuất tất cả các nút trên tầng thứ k của cây

Code: 

#include <stdio.h>

struct Node

{

Node* pLeft;

Node* pRight;

int iX;

};

typedef Node* Tree;

Node* TaoNode(int X)

{

Node* p = new Node;

if (p == NULL)

return NULL;

p->pLeft = NULL;

p->pRight = NULL;

p->iX=X;

return p;

}

void ThemNodeVaoCay(Node* p, Tree &c)

{

if (c == NULL)//nếu cây rỗng

c = p;

else //cây khác rỗng

{

if (p->iX < c->iX)

ThemNodeVaoCay(p,c->pLeft);

else if (p->iX > c->iX)

ThemNodeVaoCay(p,c->pRight);

else

return;

}

}

void Nhap(Tree &c)

{

int chon = 0;

do

{

int x;

printf("

Nhap x: ");

scanf_s("%d",&x);

Node* p = TaoNode(x);

ThemNodeVaoCay(p,c);

printf("Muon nhap thong tin tiep ko? 1: co, 0: ko ~~>");

scanf_s("%d",&chon);

}while(chon);

}

//874* Viết hàm xuất các nút trên tầng thứ k của cây

void Xuat(Tree c, int k)

{

if (c!=NULL)

{

k--;

if (c->pLeft != NULL)

Xuat(c->pLeft,k);

if (k==0)

printf("%4d", c->iX);

if (c->pRight != NULL)

Xuat(c->pRight,k);

}

}

void NhapK(int &k)

{

printf("

Nhap tang thu k: ");

scanf_s("%d",&k);

}

void main()

{

Tree c = NULL;

Nhap(c);

int k;

NhapK(k);

printf("

Xuat cac gia tri tang thu k cua cay theo LNR: ");

Xuat(c,k+1);

}

Viết hàm xuất tất cả các nút trên cây theo thứ tự từ tầng 0 đến tầng h-1 củacây (với h là chiều cao của cây)

Code: 

#include <stdio.h>

struct Node

{

Node* pLeft;

Node* pRight;

int iX;

};

typedef Node* Tree;

Node* TaoNode(int X)

{

Node* p = new Node;

if (p == NULL)

return NULL;

p->pLeft = NULL;

p->pRight = NULL;

p->iX=X;

return p;

}

void ThemNodeVaoCay(Node* p, Tree &c)

{

if (c == NULL)//nếu cây rỗng

c = p;

else //cây khác rỗng

{

if (p->iX < c->iX)

ThemNodeVaoCay(p,c->pLeft);

else if (p->iX > c->iX)

ThemNodeVaoCay(p,c->pRight);

else

return;

}

}

void Nhap(Tree &c)

{

int chon = 0;

do

{

int x;

printf("

Nhap x: ");

scanf_s("%d",&x);

Node* p = TaoNode(x);

ThemNodeVaoCay(p,c);

printf("Muon nhap thong tin tiep ko? 1: co, 0: ko ~~>");

scanf_s("%d",&chon);

}while(chon);

}

//875* Viết hàm xuất các nút trên cây theo thứ tự tầng 0 đến tầng h-1 của cây (với h là chiều cao của cây)

int ChieuCaoCay(Tree c)

{

if (c == NULL)

return 0;

int a = ChieuCaoCay(c->pLeft);

int b = ChieuCaoCay(c->pRight);

int max = (a>b)?a:b;

return 1 + max;

}

void XuatTheoTangK(Tree c, int k)

{

if (c!=NULL)

{

k--;

if (c->pLeft != NULL)

XuatTheoTangK(c->pLeft,k);

if (k==0)

printf("%4d", c->iX);

if (c->pRight != NULL)

XuatTheoTangK(c->pRight,k);

}

}

void Xuat(Tree c)

{

int h = ChieuCaoCay(c);

printf ("

Chieu cao cay: %d",h);

for (int i=0; i<=h-1; i++)

{

printf("

tang %d :", i);

XuatTheoTangK(c,i+1);

}

}

void main()

{

Tree c = NULL;

Nhap(c);

printf("

Xuat cac gia tri theo thu tu tang 0 -> h-1: ");

Xuat(c);

}

Đếm số lượng nút có đúng 1 con

Code: 

#include <stdio.h>

struct Node

{

Node* pLeft;

Node* pRight;

int iX;

};

typedef Node* Tree;

Node* TaoNode(int X)

{

Node* p = new Node;

if (p == NULL)

return NULL;

p->pLeft = NULL;

p->pRight = NULL;

p->iX=X;

return p;

}

void ThemNodeVaoCay(Node* p, Tree &c)

{

if (c == NULL)//nếu cây rỗng

c = p;

else //cây khác rỗng

{

if (p->iX < c->iX)

ThemNodeVaoCay(p,c->pLeft);

else if (p->iX > c->iX)

ThemNodeVaoCay(p,c->pRight);

else

return;

}

}

void Nhap(Tree &c)

{

int chon = 0;

do

{

int x;

printf("

Nhap x: ");

scanf_s("%d",&x);

Node* p = TaoNode(x);

ThemNodeVaoCay(p,c);

printf("Muon nhap thong tin tiep ko? 1: co, 0: ko ~~>");

scanf_s("%d",&chon);

}while(chon);

}

void Xuat(Tree c)

{

if (c!=NULL)

{

if (c->pLeft != NULL)

Xuat(c->pLeft);

printf("%4d", c->iX);

if (c->pRight != NULL)

Xuat(c->pRight);

}

}

//876 Đếm số lượng nút có đúng 1 con

int Dem(Tree c)

{

if (c!=NULL)

{

int a = Dem(c->pLeft);

int b = Dem(c->pRight);

if ((c->pLeft != NULL && c->pRight == NULL) || (c->pLeft == NULL && c->pRight != NULL))

return 1 + a + b;

return a + b;

}

return 0;

}

void main()

{

Tree c = NULL;

Nhap(c);

printf("

Xuat cay nhi phan LNR: ");

Xuat(c);

printf("

Dem so luong nut co dung 1 con: %d", Dem(c));

}

Đếm số lượng nút có đúng 2 con

Code: 

#include <stdio.h>

struct Node

{

Node* pLeft;

Node* pRight;

int iX;

};

typedef Node* Tree;

Node* TaoNode(int X)

{

Node* p = new Node;

if (p == NULL)

return NULL;

p->pLeft = NULL;

p->pRight = NULL;

p->iX=X;

return p;

}

void ThemNodeVaoCay(Node* p, Tree &c)

{

if (c == NULL)//nếu cây rỗng

c = p;

else //cây khác rỗng

{

if (p->iX < c->iX)

ThemNodeVaoCay(p,c->pLeft);

else if (p->iX > c->iX)

ThemNodeVaoCay(p,c->pRight);

else

return;

}

}

void Nhap(Tree &c)

{

int chon = 0;

do

{

int x;

printf("

Nhap x: ");

scanf_s("%d",&x);

Node* p = TaoNode(x);

ThemNodeVaoCay(p,c);

printf("Muon nhap thong tin tiep ko? 1: co, 0: ko ~~>");

scanf_s("%d",&chon);

}while(chon);

}

void Xuat(Tree c)

{

if (c!=NULL)

{

if (c->pLeft != NULL)

Xuat(c->pLeft);

printf("%4d", c->iX);

if (c->pRight != NULL)

Xuat(c->pRight);

}

}

//877 Đếm số lượng nút có đúng 2 con

int Dem(Tree c)

{

if (c!=NULL)

{

int a = Dem(c->pLeft);

int b = Dem(c->pRight);

if (c->pLeft != NULL && c->pRight != NULL)

return 1 + a + b;

return a + b;

}

return 0;

}

void main()

{

Tree c = NULL;

Nhap(c);

printf("

Xuat cay nhi phan LNR: ");

Xuat(c);

printf("

Dem so luong nut co dung 2 con: %d", Dem(c));

}

Đếm số lượng nút chẵn

Code: 

#include <stdio.h>

struct Node

{

Node* pLeft;

Node* pRight;

int iX;

};

typedef Node* Tree;

Node* TaoNode(int X)

{

Node* p = new Node;

if (p == NULL)

return NULL;

p->pLeft = NULL;

p->pRight = NULL;

p->iX=X;

return p;

}

void ThemNodeVaoCay(Node* p, Tree &c)

{

if (c == NULL)//nếu cây rỗng

c = p;

else //cây khác rỗng

{

if (p->iX < c->iX)

ThemNodeVaoCay(p,c->pLeft);

else if (p->iX > c->iX)

ThemNodeVaoCay(p,c->pRight);

else

return;

}

}

void Nhap(Tree &c)

{

int chon = 0;

do

{

int x;

printf("

Nhap x: ");

scanf_s("%d",&x);

Node* p = TaoNode(x);

ThemNodeVaoCay(p,c);

printf("Muon nhap thong tin tiep ko? 1: co, 0: ko ~~>");

scanf_s("%d",&chon);

}while(chon);

}

void Xuat(Tree c)

{

if (c!=NULL)

{

if (c->pLeft != NULL)

Xuat(c->pLeft);

printf("%4d", c->iX);

if (c->pRight != NULL)

Xuat(c->pRight);

}

}

//878 Đếm số lượng nút chẵn

int Dem(Tree c)

{

if (c!=NULL)

{

int a = Dem(c->pLeft);

int b = Dem(c->pRight);

if (c->iX%2==0)

return 1 + a + b;

return a + b;

}

return 0;

}

void main()

{

Tree c = NULL;

Nhap(c);

printf("

Xuat cay nhi phan LNR: ");

Xuat(c);

printf("

Dem so luong nut chan: %d", Dem(c));

879 Đếm số lượng nút lá mà thông tin tại nút đó là giá trị chẵn

int Dem(Tree c)

{

void main()

{

Tree c = NULL;

Nhap(c);

printf("

Xuat cay nhi phan LNR: ");

Xuat(c);

printf("

Dem so luong nut la ma gia tri chan: %d", Dem(c));

}

Đếm số lượng nút có đúng 1 con mà thông tin tại nút đó là số nguyên tố

Code: 

#include <stdio.h>

struct Node

{

Node* pLeft;

Node* pRight;

int iX;

};

typedef Node* Tree;

Node* TaoNode(int X)

{

Node* p = new Node;

if (p == NULL)

return NULL;

p->pLeft = NULL;

p->pRight = NULL;

p->iX=X;

return p;

}

void ThemNodeVaoCay(Node* p, Tree &c)

{

if (c == NULL)//nếu cây rỗng

c = p;

else //cây khác rỗng

{

if (p->iX < c->iX)

ThemNodeVaoCay(p,c->pLeft);

else if (p->iX > c->iX)

ThemNodeVaoCay(p,c->pRight);

else

return;

}

}

void Nhap(Tree &c)

{

int chon = 0;

do

{

int x;

printf("

Nhap x: ");

scanf_s("%d",&x);

Node* p = TaoNode(x);

ThemNodeVaoCay(p,c);

printf("Muon nhap thong tin tiep ko? 1: co, 0: ko ~~>");

scanf_s("%d",&chon);

}while(chon);

}

void Xuat(Tree c)

{

if (c!=NULL)

{

if (c->pLeft != NULL)

Xuat(c->pLeft);

printf("%4d", c->iX);

if (c->pRight != NULL)

Xuat(c->pRight);

}

}

//880 Đếm số lượng nút có đúng 1 con mà thông tin tại đó là số nguyên tố

bool SoNguyenTo(int n)

{

if (n<=1)

return 0;

for (int i=2; i<n; i++)

if (n%i == 0)

return 0;

return 1;

}

int Dem(Tree c)

{

if (c!=NULL)

{

int a = Dem(c->pLeft);

int b = Dem(c->pRight);

if (SoNguyenTo(c->iX))

if ((c->pLeft!=NULL && c->pRight==NULL) || (c->pLeft==NULL && c->pRight!=NULL))

return 1 + a + b;

return a + b;

}

return 0;

}

void main()

{

Tree c = NULL;

Nhap(c);

printf("

Xuat cay nhi phan LNR: ");

Xuat(c);

printf("

Dem so luong nut co dung 1 con va gia tri la so nguyen to: %d", Dem(c));

}

Đếm số lượng nút có đúng 2 con mà thông tin tại nút đó là số chính phương

Code: 

#include <stdio.h>

#include <math.h>

struct Node

{

Node* pLeft;

Node* pRight;

int iX;

};

typedef Node* Tree;

Node* TaoNode(int X)

{

Node* p = new Node;

if (p == NULL)

return NULL;

p->pLeft = NULL;

p->pRight = NULL;

p->iX=X;

return p;

}

void ThemNodeVaoCay(Node* p, Tree &c)

{

if (c == NULL)//nếu cây rỗng

c = p;

else //cây khác rỗng

{

if (p->iX < c->iX)

ThemNodeVaoCay(p,c->pLeft);

else if (p->iX > c->iX)

ThemNodeVaoCay(p,c->pRight);

else

return;

}

}

void Nhap(Tree &c)

{

int chon = 0;

do

{

int x;

printf("

Nhap x: ");

scanf_s("%d",&x);

Node* p = TaoNode(x);

ThemNodeVaoCay(p,c);

printf("Muon nhap thong tin tiep ko? 1: co, 0: ko ~~>");

scanf_s("%d",&chon);

}while(chon);

}

void Xuat(Tree c)

{

if (c!=NULL)

{

if (c->pLeft != NULL)

Xuat(c->pLeft);

printf("%4d", c->iX);

if (c->pRight != NULL)

Xuat(c->pRight);

}

}

//881 Đếm số lượng nút có đúng 2 con mà thông tin tại đó là số chính phương

bool SoChinhPhuong(int n)

{

if (n<=0)

return 0;

int s = sqrt((double)n);

if (s*s == n)

return 1;

return 0;

}

int Dem(Tree c)

{

if (c!=NULL)

{

int a = Dem(c->pLeft);

int b = Dem(c->pRight);

if (SoChinhPhuong(c->iX))

if (c->pLeft!=NULL && c->pRight!=NULL)

return 1 + a + b;

return a + b;

}

return 0;

}

void main()

{

Tree c = NULL;

Nhap(c);

printf("

Xuat cay nhi phan LNR: ");

Xuat(c);

printf("

Dem so luong nut co dung 2 con va gia tri la so chinh phuong: %d", Dem(c));

}

Đếm số lượng nút trên tầng thứ k của cây

Code: 

#include <stdio.h>

#include <math.h>

struct Node

{

Node* pLeft;

Node* pRight;

int iX;

};

typedef Node* Tree;

Node* TaoNode(int X)

{

Node* p = new Node;

if (p == NULL)

return NULL;

p->pLeft = NULL;

p->pRight = NULL;

p->iX=X;

return p;

}

void ThemNodeVaoCay(Node* p, Tree &c)

{

if (c == NULL)//nếu cây rỗng

c = p;

else //cây khác rỗng

{

if (p->iX < c->iX)

ThemNodeVaoCay(p,c->pLeft);

else if (p->iX > c->iX)

ThemNodeVaoCay(p,c->pRight);

else

return;

}

}

void Nhap(Tree &c)

{

int chon = 0;

do

{

int x;

printf("

Nhap x: ");

scanf_s("%d",&x);

Node* p = TaoNode(x);

ThemNodeVaoCay(p,c);

printf("Muon nhap thong tin tiep ko? 1: co, 0: ko ~~>");

scanf_s("%d",&chon);

}while(chon);

}

void Xuat(Tree c)

{

if (c!=NULL)

{

if (c->pLeft != NULL)

Xuat(c->pLeft);

printf("%4d", c->iX);

if (c->pRight != NULL)

Xuat(c->pRight);

}

}

Bạn đang đọc truyện trên: Truyen247.Pro

Tags: