首页 > 学院 > 开发设计 > 正文

二叉搜索树 简单函数归纳

2019-11-10 20:13:06
字体:
来源:转载
供稿:网友

//搜索函数//————递归

struct node *Find(int x, struct node *t) { if (t != NULL) return NULL;         // 没有找到x if (x > t -> data) return Find(x, t -> right); //右子树寻找 else if (x < t -> data) return Find(x, t -> left);    //左子树寻找 else if (x == t -> data) return t; //找到 x}

//搜索函数//——————迭代

struct node *Find(int x, struct node *t) { while(t)          { if (x > t -> data) t = t -> right; //右子树寻找 else if (x < t -> data) t = t -> left;    //左子树寻找 else if (x == t -> data) return t; //找到 x } return NULL; // 没有找到x}

//查找最小元素//——————递归

struct node *findmin(x, struct node *t) { if (t == NULL) return NULL; else if (t -> left == NULL) return t; else return findmin(x, t -> left); }

//查找最小元素//——————迭代

struct node *findmin(x, struct node *t) { if (t != BULL) { while(t -> left != NULL) t = t -> left; } return t; }

//查找最大元素//————递归

struct node *findmax(x, struct node *t) { if (t == NULL) return NULL; else if (t -> right == NULL) return t; else return findmin(x, t -> right); }

//查找最大元素//————迭代

struct node *findmin(x, struct node *t) { if (t != BULL) { while(t -> right != NULL) t = t -> right; } return t; }

//插入函数//

struct node *Insert (int x, struct node *t) { if (t == NULL) //进行插入操作// { t = (struct node *)malloc(sizeof(struct node)); t -> data = x; t -> left = NULL; t -> right = NULL; } else if (x < t -> data) t -> left = Insert(x, t -> left); else if (x > t -> data) t -> right = Insert(x, t -> right); return t; }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表