思考了一下午啊,看来自己太水了。
#include <stdio.h> #include <stdlib.h> typedef int elemtype; //整形为树的数据类型 typedef struct node { elemtype data; struct node *lchild,*rchild; }btree,*ptree; void (*visit)(elemtype e); //树的创建 ptree CreatTree() { ptree root; elemtype data; scanf("%d",&data); if(data == 0)root = NULL; //0 代表空子树 else { root = (ptree)malloc(sizeof(btree)); if(!root)exit(1); root->data = data; root->lchild = CreatTree(); root->rchild = CreatTree(); } return root; } void PrintResult(elemtype e) { printf("%d ",e); } //前序遍历二叉树 void PreOrderTree(ptree root,void (*visit)(elemtype )) { if(root) { (visit)(root->data); PreOrderTree(root->lchild,visit); PreOrderTree(root->rchild,visit); } } //中序遍历二叉树 void InOrderTree(ptree root,void (*visit)(elemtype )) { if(root) { PreOrderTree(root->lchild,visit); (visit)(root->data); PreOrderTree(root->rchild,visit); } } //后序遍历二叉树 void PostOrderTree(ptree root,void (*visit)(elemtype )) { if(root) { PreOrderTree(root->lchild,visit); PreOrderTree(root->rchild,visit); (visit)(root->data); } } //求树的叶子数目 int SumLeaf(ptree root) { int count=0,m,n; if(root) { if(root->lchild == NULL && root->rchild == NULL)count++; m = SumLeaf(root->lchild); count += m; n = SumLeaf(root->rchild); count += n; } return count; } //求树的深度 int TreeDepth(ptree root) { int depth=0,depthl,depthr; if(!root)depth=0; else { depthl = TreeDepth(root->lchild); depthr = TreeDepth(root->rchild); depth += 1 + (depthl > depthr ?depthl:depthr); } return depth; } //销毁二叉树 void DstroyTree(ptree T) { if(T == NULL)return ; if(T) { DstroyTree(T->lchild); DstroyTree(T->rchild); } free(T); T = NULL; } //交换二叉树的左右子树 void SwapLRchild(ptree root) { ptree temp; if(root) { temp = root->lchild; root->lchild = root->rchild; root->rchild = temp; SwapLRchild(root->lchild); SwapLRchild(root->rchild); } } //给一个节点的值,求解这个节点的父节点 int Parent(ptree root,int e) { if(!root)return 0; else if(root) { if(root->rchild != NULL && root->lchild->data == e)return root->data; if(root->rchild != NULL && root->rchild->data == e)return root->data; Parent(root->lchild,e); Parent(root->rchild,e); } return 0; } //在二叉树中查找指定的节点 int FindTreeNode(ptree root,int e) { if(root == NULL)return 0; if(root->data == e)return e; if(FindTreeNode(root->lchild,e) == e)return e; else return FindTreeNode(root->rchild,e); } int main() { return 0; }