如何用C++递归在BST(Binary Search Tree) 数有几个节点大于根节点的数字
来源:互联网 发布:商业机构域名 编辑:程序博客网 时间:2024/06/07 01:39
小编上课时使用英文教材,所以对中文计算机词汇不是那么了解,所以,这里先表示抱歉。但是,我还是尽量使用中文计算机词汇来表达,这样有助于你们能看的懂小编在写什么。
现在,要实现在Binary Search Tree, 也就是二叉搜索树。下面小编就用BST来表示二叉搜索树,因为这样方便。关于知识点,小编就不在写了,不懂得就百度吧!
OK,现在就展示如何用C++递归来实现这个问题。
//Function prototype是写在.h文件struct node{ int data; node * left; node * right;};class table{ public: //Count the number of nodes with data greater than the root's data in a BST int count_greater_root(); private: //Count the number of nodes with data greater than the root's data in a BST //这个是专门用递归来实现的 //也叫做recursive function int count_greater_root(node * root, int num);};//下面一部分是来实现这两个函数的//实现函数在.cpp文件里int table::count_greater_root(){ return count_greater_root(root,root->data);}int table::count_greater_root(node * root, int num){ if(!root) return 0; if(root->data > num) { return count_greater_root(root->right,num)+count_greater_root(root->left,num) + 1; } return count_greater_root(root->left,num)+count_greater_root(root->right,num);}//下面是在主函数里进行调用这些函数//主函数也是写在.cpp文件里的int main(){ table object; int result = object.count_greater_root(); cout<<"The result is: "<<result<<endl; return 0;}
下面是展示运行结果:
有可能大家对这个结果看不懂,没事小编会接触你们的疑惑的。
下面展示的是小编在白纸上根据结果的展示来描述这个BST.
所以,根据这个树的形状,就可以知道比数字21大的数字一共有8个。
希望这篇文章对大家有所帮助,以后,小编还会继续写有关BST的C++递归博客送给大家。敬请期待吧!
阅读全文
1 0
- 如何用C++递归在BST(Binary Search Tree) 数有几个节点大于根节点的数字
- 如何使用C++递归来实现在BST(Binary Search Tree)里将所有的叶子节点上的数字求和
- 如何用C++递归来删除所有的BST节点
- 如何用C++递归来查找BST中的根节点的inorder successor
- Inorder Successor in Binary Search Tree BST中找中序遍历的后继节点
- Tree_Graph Inorder Successor in Binary Search Tree BST中找中序遍历的后继节点 @CareerCup
- Binary Search Tree (BST)
- Binary Search Tree (BST)
- 如何使用C++递归来实现查找BST(Binary Search Tree)的最大高度
- BST(binary search tree)
- 数据结构-BST(Binary Search Tree)
- 浅谈BST (Binary Search Tree)
- BST-----Validate Binary Search Tree
- LintCode Insert a Node in a Binary Search Tree 在二叉查找树中插入节点
- lintcode insert-node-in-a-binary-search-tree 在二叉查找树中插入节点
- 给定BST,使用大于等于当前节点的总和代替当前节点
- Lowest Common Ancestor of a Binary Search Tree 二分搜索树的公共节点
- 501. Find Mode in Binary Search Tree | 二叉搜索树重复的节点
- 友员函数 operator
- 02 MapReduce
- MySQL server5.7版出现server failed
- IMMEDIATE DECODABILITY POJ
- 自适应网站怎么做好些?
- 如何用C++递归在BST(Binary Search Tree) 数有几个节点大于根节点的数字
- Unity3D shader(13)——Fragment Shader(简单的纹理采样)
- BugFree的安装与使用以及使用过程中可能出现的问题总结
- Redis服务器
- 算法:C++中的堆排序
- 数据结构--实现一个位图
- 牛人整理分享的面试知识:操作系统、计算机网络、设计模式、Linux编程,数据结构总结
- (转)树莓派系列安装opevcv3.0教程填坑版
- ZOJ 3210 A Stack or A Queue? 水题