前序遍历创建排序二叉树并查找节点//前序遍历创建排序二叉树并查找节点
#include <stdio.h> #include <malloc.h> #include <iostream> using namespace std; typedef int Element; typedef struct node *Position; typedef struct node { ?Element el; ?Position left,right; }Node; typedef Position BST; Position CreateBST(Position T); Position FindKey(Element k,bool &found,BST B); Position FindKey(Element k,bool &found,BST B) { ?Position p,pfather; ?p=B; ?pfather=NULL; ?while(p) ??if(k==p->el) ??{ ???found=true; ???return p; ??} ??else ??{ ???pfather=p; ???if(k<p->el) ????p=p->left; ???else ????p=p->right; ??} ??found=false; ??return pfather; } |