二叉树问题汇总(1)—基础问题
2012-07-19 21:09
295 查看
1、创建二叉搜索树
二叉搜索树是排序的二叉树,即左子树的值都不大于根结点,右子树的值都比根结点大。插入结点到一颗二叉搜索树可以使用递归或者非递归方法。使用递归方法代码简单,使用非递归则易于理解。二叉搜索树BST定义:
struct node { int data; struct node* left; struct node* right; };二叉搜索树创建结点代码:
struct node* newNode(int data) { struct node* nd = (struct node*)malloc(sizeof(struct node)); //分配空间 nd->data = data; //设置data域 nd->left = nd->right = NULL; //初始化左右孩子结点为NULL return nd; }
二叉搜索树插入结点代码1(递归,函数返回根结点):
struct node* insert(struct node* root, int data) { if (root == NULL) { //二叉树为空,则创建结点直接返回。 return newNode(data); } else { if (root->data >= data) { //如果根结点值大于或等于插入值,则插入结点到左子树。 root->left = insert(root->left, data); } else { //如果根结点值小于插入值,则插入结点到右子树。 root->right = insert(root->right, data); } return root; //返回根结点指针。 } }
二叉搜索树插入节点代码2(递归,函数无返回值):
void insert2(struct node** rootRef, int data) { struct node* root = *rootRef; if (root == NULL) { *rootRef = newNode(data); return; } if (root->data >= data) insert2(&(root->left), data); else insert2(&(root->right), data); }
二叉搜索树插入节点代码3(非递归):
void insert3(struct node** rootRef, int data) { struct node* root = *rootRef; struct node* nd = newNode(data); if (root == NULL) { *rootRef = nd; return; } struct node* parent = NULL; struct node* current = root; while (current != NULL) { parent = current; //parent跟踪结点插入,直到其孩子结点为NULL if (current->data >= data) current = current->left; else current = current->right; } if (parent->data >= data) //判断应该插入到左孩子还是右孩子 parent->left = nd; else parent->right = nd; }
创建二叉搜索树,包含3个结点:
//直接创建二叉树 struct node* build123a() { struct node* root = newNode(2); root->left = newNode(1); root->right = newNode(3); return(root); }
//使用insert创建二叉搜索树 struct node* build123b() { struct node* root = NULL; root = insert(root, 2); root = insert(root, 1); root = insert(root, 3); return(root); }
2、二叉树结点数目(不需要是二叉搜索树)
//结点数目=左子树结点数目+右子树节点数目+1(根结点) int size(struct node* root) { if (root == NULL) return 0; return size(root->left) + size(root->right) + 1; }
3、二叉树深度(根结点到最远叶结点距离,空树深度为0)
int maxDepth(struct node* root) { if (root == NULL) return 0; else { int lDepth = maxDepth(root->left); //递归获取左子树深度 int rDepth = maxDepth(root->right); //获取右子树深度 return lDepth>rDepth? lDepth+1: rDepth+1; //取较大值+1即为二叉树深度 } }
4、二叉搜索树最小值(最大值类似,查询右子树即可)
//根据二叉搜索树性质,直接往左子树迭代查询 int minValue(struct node* root) { assert(root != NULL); struct node* current = root; while (current->left != NULL) { current = current->left; } return current->data; }
//递归算法查询二叉搜索树最小值 int minValue(struct node* root) { assert(root != NULL); if (root->left == NULL) return root->data; else return minValue(root->left); }
5、查找二叉搜索树中是否存在某个值
/*查找二叉搜索树中是否存在值为target的结点*/ bool lookup(struct node* root, int target) { if (root == NULL) return false; if (root->data == target) return true; else if (root->data > target) return lookup(root->left, target); else return lookup(root->right, target); }
相关文章推荐
- c语言基础问题汇总
- C#语言基础常见问题汇总
- python语言基础问题汇总
- silverlight基础问题汇总
- java语言基础1问题汇总
- MySQL-->基础知识-->MySQL 常见 问题汇总
- 二叉树的基础问题
- poscms基础问题汇总
- git基础问题汇总
- .NET基础知识问题汇总(持续更新中)
- OpenCV基础问题汇总
- C#语言基础常见问题汇总
- java 集合类基础问题汇总
- C#语言基础常见问题汇总
- 二叉树问题汇总(2)—常见问题
- 分账汇总少了一笔 和 对账和商户汇总比基础表少了一笔 问题处理方式
- 常见二叉树基础算法汇总
- C#语言基础常见问题汇总
- C#语言基础常见问题汇总(1)
- java 泛型基础问题汇总