赞
踩
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #define To_Be_Find_Num 20 #define TextPath "C:\\Users\\86132\\Desktop\\第十二周周末程序设计\\二叉排序树\\Number.txt" typedef struct _BinaryTree BinaryTree; struct _BinaryTree { int Date; BinaryTree* Left, * Right; }; void Creat(BinaryTree* T, int n) { if (n < T->Date) if (NULL == T->Left) { T->Left = (BinaryTree*)malloc(sizeof(BinaryTree)); T->Left->Date = n; T->Left->Left = T->Left->Right = NULL; } else Creat(T->Left, n); else if (NULL == T->Right) { T->Right = (BinaryTree*)malloc(sizeof(BinaryTree)); T->Right->Date = n; T->Right->Left = T->Right->Right = NULL; } else Creat(T->Right,n); } int Search(BinaryTree* T , int n) { while (T) { if (T->Date > n) T = T->Left; else if (T->Date < n) T = T->Right; else return 1; } return 0; } void Creat_Tree(BinaryTree* T) { FILE* fp = fopen(TextPath, "r"); int tmp; BinaryTree* p = T; fscanf(fp, "%d", &T->Date); while (EOF != fscanf(fp, "%d", &tmp)) { Creat(T, tmp); } fclose(fp); } int main() { BinaryTree* T = (BinaryTree*)malloc(sizeof(BinaryTree)); T->Left = T->Right = NULL; Creat_Tree(T); if (Search(T, To_Be_Find_Num)) printf("%d在二叉排序树里\n" , To_Be_Find_Num); else printf("%d不在二叉排序树里\n", To_Be_Find_Num); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。