#include <stdio.h> #include <malloc.h> #include <stdlib.h> #include <queue> #include <stack> using namespace std; typedef struct BstNode { int data; BstNode *lchild, *rchild; }BstNode,*BstTree; void Bst_insert(BstTree &T,int z)//建树 { if(T==NULL) { T=(BstTree)malloc(sizeof(BstNode)); T->lchild=NULL; T->rchild=NULL; T->data=z; } else { if(T->data!=z) if(T->data>z) Bst_insert(T->lchild,z); else Bst_insert(T->rchild,z); } } void Bst_preorder(BstTree T) //中序递归遍历 { if(T!=NULL) { Bst_preorder(T->lchild); printf("%d\n",T->data); Bst_preorder(T->rchild); } } bool Bst_exist(BstTree T,int z)//寻找某数值是否存在 { if(T==NULL) return false; if(T->data==z) return true; if(T->data>z) Bst_exist(T->lchild,z);
Bst的基本操作
最新推荐文章于 2022-02-17 18:22:04 发布