left\u-son==NULL&&root->*right\u-son==NULL) 回报率(d==水平+1); //如果内部节点和一个子节点为空 if(root->*left_-son==NULL | | root->*right_-son==NULL) 返回false; //左右子树必须是完美的。 返回isPerfectRec(根->*左上,d,级别+1)&& iPerfectRec(根->*右上,d,级别+1); } //isPerfectRec()上的包装器 bool isPerfect(二进制搜索树*root) { int d=findADepth(根); 返回isPerfectRec(根,d); } int findADepth(二进制搜索树*节点) { int d=0; while(节点!=NULL) { d++; 节点=节点->左侧子节点; } 返回d; } //查找给定树中第二大元素的函数。 void secondLargestUtil(二进制搜索树*root,int&c) { //基本情况下,第二个条件对于 //避免不必要的递归调用 if(root==NULL | | c>=2) 回来 //按照反向顺序遍历,以便 //首先访问最大的元素 第二个最大值(根->右上,c); //访问节点的增量计数 C++; //如果c现在变成k,那么这是第二大 如果(c==2) { std::cout left_son,c); } 第二大空(BinarySearchTree*根) { //将访问的节点数初始化为0 int c=0; //注意,c是通过引用传递的 第二大类(根,c); } 布尔只有一个子(整数前[],整数大小) { int nextDiff,lastDiff; 对于(int i=0;i>x; 而(x>=0){ 输入=二进制搜索树(x); 如果(root==NULL){//如果root为空 根=输入; temp=root;//temp用于存储值以进行比较 } 否则{ temp=root;//对于每个新添加,必须从root开始才能找到正确的位置 while(输入!=NULL){ 如果(xpinfo){//如果较小的x添加到左侧 如果(temp->left\u son==NULL){//left为空 温度->左上=输入; input=NULL;//添加了新节点,退出循环 } else{//如果不是空的,则将temp设置为子树 temp=temp->left\u-son;//需要从当前位置向左移动 } } else{//否则x添加到右侧 如果(temp->right\u son==NULL){//right为空 温度->右上=输入; input=NULL;//添加了新节点,退出循环 } 否则{ temp=temp->right\u-son;//需要从当前位置向右移动 } } } } 标准:cin>>x; } 返回根; } }; int main(){ BinarySearchTree*r=新的BinarySearchTree; BinarySearchTree*r1=新的BinarySearchTree; BinarySearchTree*p=新的BinarySearchTree; p=readListInter(); r->插入(6); r->插入(8); r->插入(1); r->插入(9); r->插入(10); r->插入(4); r->插入(13); r->插入(12); printf(“\n\u\n”); r1->插入(6); r1->插入(8); r1->插入(1); r1->插入(9); r1->插入(10); r1->插入(4); r1->插入(13); r1->插入(12); printf(“\n\u\n”); r->isPerfect(r); int pre[]={8,3,5,7,6}; int size=sizeof(pre)/sizeof(pre[0]); if(hasOnlyOneChild(pre,size)==true) printf(“是”); 其他的 printf(“否”); s 返回0; },c++,binary-search-tree,C++,Binary Search Tree" /> left\u-son==NULL&&root->*right\u-son==NULL) 回报率(d==水平+1); //如果内部节点和一个子节点为空 if(root->*left_-son==NULL | | root->*right_-son==NULL) 返回false; //左右子树必须是完美的。 返回isPerfectRec(根->*左上,d,级别+1)&& iPerfectRec(根->*右上,d,级别+1); } //isPerfectRec()上的包装器 bool isPerfect(二进制搜索树*root) { int d=findADepth(根); 返回isPerfectRec(根,d); } int findADepth(二进制搜索树*节点) { int d=0; while(节点!=NULL) { d++; 节点=节点->左侧子节点; } 返回d; } //查找给定树中第二大元素的函数。 void secondLargestUtil(二进制搜索树*root,int&c) { //基本情况下,第二个条件对于 //避免不必要的递归调用 if(root==NULL | | c>=2) 回来 //按照反向顺序遍历,以便 //首先访问最大的元素 第二个最大值(根->右上,c); //访问节点的增量计数 C++; //如果c现在变成k,那么这是第二大 如果(c==2) { std::cout left_son,c); } 第二大空(BinarySearchTree*根) { //将访问的节点数初始化为0 int c=0; //注意,c是通过引用传递的 第二大类(根,c); } 布尔只有一个子(整数前[],整数大小) { int nextDiff,lastDiff; 对于(int i=0;i>x; 而(x>=0){ 输入=二进制搜索树(x); 如果(root==NULL){//如果root为空 根=输入; temp=root;//temp用于存储值以进行比较 } 否则{ temp=root;//对于每个新添加,必须从root开始才能找到正确的位置 while(输入!=NULL){ 如果(xpinfo){//如果较小的x添加到左侧 如果(temp->left\u son==NULL){//left为空 温度->左上=输入; input=NULL;//添加了新节点,退出循环 } else{//如果不是空的,则将temp设置为子树 temp=temp->left\u-son;//需要从当前位置向左移动 } } else{//否则x添加到右侧 如果(temp->right\u son==NULL){//right为空 温度->右上=输入; input=NULL;//添加了新节点,退出循环 } 否则{ temp=temp->right\u-son;//需要从当前位置向右移动 } } } } 标准:cin>>x; } 返回根; } }; int main(){ BinarySearchTree*r=新的BinarySearchTree; BinarySearchTree*r1=新的BinarySearchTree; BinarySearchTree*p=新的BinarySearchTree; p=readListInter(); r->插入(6); r->插入(8); r->插入(1); r->插入(9); r->插入(10); r->插入(4); r->插入(13); r->插入(12); printf(“\n\u\n”); r1->插入(6); r1->插入(8); r1->插入(1); r1->插入(9); r1->插入(10); r1->插入(4); r1->插入(13); r1->插入(12); printf(“\n\u\n”); r->isPerfect(r); int pre[]={8,3,5,7,6}; int size=sizeof(pre)/sizeof(pre[0]); if(hasOnlyOneChild(pre,size)==true) printf(“是”); 其他的 printf(“否”); s 返回0; },c++,binary-search-tree,C++,Binary Search Tree" />

函数不能';还没有解决 我的C++代码有问题。它说所有以isPerfectRec()开头的函数都无法解析…为什么?我试过很多方法,但显然都不管用。我有很多帮助,比如验证二元搜索树是否完美,在二元搜索树中查找第二大元素等等 #include <stdio.h> #include<iostream> #include<stack> template<typename T> class BinarySearchTree { public: BinarySearchTree<T> *root, *left_son, *right_son, *parent; T *pinfo; BinarySearchTree() { left_son = right_son = NULL; root = this; pinfo = NULL; } void setInfo(T info) { pinfo = new T; *pinfo = info; } void insert(T x) { if (pinfo == NULL) setInfo(x); else insert_rec(x); } bool isPerfectRec(BinarySearchTree *root, int d, int level = 0) { // An empty tree is perfect if (*root == NULL) return true; // If leaf node, then its depth must be same as // depth of all other leaves. if (*root->left_son == NULL && root->*right_son == NULL) return (d == level+1); // If internal node and one child is empty if (root->*left_son == NULL || root->*right_son == NULL) return false; // Left and right subtrees must be perfect. return isPerfectRec(root->*left_son, d, level+1) && isPerfectRec(root->*right_son, d, level+1); } // Wrapper over isPerfectRec() bool isPerfect(BinarySearchTree *root) { int d = findADepth(root); return isPerfectRec(root, d); } int findADepth(BinarySearchTree *node) { int d = 0; while (node != NULL) { d++; node = node->left_son; } return d; } // A function to find 2nd largest element in a given tree. void secondLargestUtil(BinarySearchTree *root, int &c) { // Base cases, the second condition is important to // avoid unnecessary recursive calls if (root == NULL || c >= 2) return; // Follow reverse inorder traversal so that the // largest element is visited first secondLargestUtil(root->right_son, c); // Increment count of visited nodes c++; // If c becomes k now, then this is the 2nd largest if (c == 2) { std::cout << "2nd largest element is " << root->pinfo; printf("\n___\n"); return; } // Recur for left subtree secondLargestUtil(root->left_son, c); } void secondLargest(BinarySearchTree *root) { // Initialize count of nodes visited as 0 int c = 0; // Note that c is passed by reference secondLargestUtil(root, c); } bool hasOnlyOneChild(int pre[], int size) { int nextDiff, lastDiff; for (int i=0; i<size-1; i++) { nextDiff = pre[i] - pre[i+1]; lastDiff = pre[i] - pre[size-1]; if (nextDiff*lastDiff < 0) return false;; } return true; } BinarySearchTree * readListInter(){ BinarySearchTree* root = NULL;//returning object BinarySearchTree* temp; BinarySearchTree* input;//new node to add int x; std::cout << "enter number (>0 to stop): "; std::cin >> x; while(x>=0){ input = BinarySearchTree(x); if(root == NULL){//if root is empty root = input; temp = root;//temp is use to store value for compare } else{ temp = root; //for each new addition, must start at root to find correct spot while(input != NULL){ if( x < temp->pinfo){//if smaller x to add to left if(temp->left_son == NULL){//left is empty temp->left_son = input; input = NULL;//new node added, exit the loop } else{//if not empty set temp to subtree temp = temp->left_son;//need to move left from the current position } } else{//otherwise x add to right if(temp->right_son == NULL){//right is empty temp->right_son = input; input = NULL;//new node added, exit the loop } else{ temp = temp->right_son;//need to move right from the current position } } } } std::cin >> x; } return root; } }; int main() { BinarySearchTree<int> *r = new BinarySearchTree<int>; BinarySearchTree<int> *r1 = new BinarySearchTree<int>; BinarySearchTree<int> *p = new BinarySearchTree<int>; p = readListInter(); r->insert(6); r->insert(8); r->insert(1); r->insert(9); r->insert(10); r->insert(4); r->insert(13); r->insert(12); printf("\n___\n"); r1->insert(6); r1->insert(8); r1->insert(1); r1->insert(9); r1->insert(10); r1->insert(4); r1->insert(13); r1->insert(12); printf("\n___\n"); r->isPerfect(r); int pre[] = {8, 3, 5, 7, 6}; int size = sizeof(pre)/sizeof(pre[0]); if (hasOnlyOneChild(pre, size) == true ) printf("Yes"); else printf("No"); s return 0; } #包括 #包括 #包括 模板类二进制搜索树{ 公众: BinarySearchTree*根,*左子,*右子,*父; T*pinfo; 二进制搜索树(){ 左儿子=右儿子=空; 根=这个; pinfo=NULL; } 无效设置信息(T信息){ pinfo=新T; *pinfo=info; } 空白插入(T x){ if(pinfo==NULL) setInfo(x); 其他的 插入_rec(x); } bool isPerfectRec(二进制搜索树*root,int d,int level=0) { //一棵空树是完美的 如果(*root==NULL) 返回true; //如果是叶节点,则其深度必须与 //所有其他叶子的深度。 如果(*root->left\u-son==NULL&&root->*right\u-son==NULL) 回报率(d==水平+1); //如果内部节点和一个子节点为空 if(root->*left_-son==NULL | | root->*right_-son==NULL) 返回false; //左右子树必须是完美的。 返回isPerfectRec(根->*左上,d,级别+1)&& iPerfectRec(根->*右上,d,级别+1); } //isPerfectRec()上的包装器 bool isPerfect(二进制搜索树*root) { int d=findADepth(根); 返回isPerfectRec(根,d); } int findADepth(二进制搜索树*节点) { int d=0; while(节点!=NULL) { d++; 节点=节点->左侧子节点; } 返回d; } //查找给定树中第二大元素的函数。 void secondLargestUtil(二进制搜索树*root,int&c) { //基本情况下,第二个条件对于 //避免不必要的递归调用 if(root==NULL | | c>=2) 回来 //按照反向顺序遍历,以便 //首先访问最大的元素 第二个最大值(根->右上,c); //访问节点的增量计数 C++; //如果c现在变成k,那么这是第二大 如果(c==2) { std::cout left_son,c); } 第二大空(BinarySearchTree*根) { //将访问的节点数初始化为0 int c=0; //注意,c是通过引用传递的 第二大类(根,c); } 布尔只有一个子(整数前[],整数大小) { int nextDiff,lastDiff; 对于(int i=0;i>x; 而(x>=0){ 输入=二进制搜索树(x); 如果(root==NULL){//如果root为空 根=输入; temp=root;//temp用于存储值以进行比较 } 否则{ temp=root;//对于每个新添加,必须从root开始才能找到正确的位置 while(输入!=NULL){ 如果(xpinfo){//如果较小的x添加到左侧 如果(temp->left\u son==NULL){//left为空 温度->左上=输入; input=NULL;//添加了新节点,退出循环 } else{//如果不是空的,则将temp设置为子树 temp=temp->left\u-son;//需要从当前位置向左移动 } } else{//否则x添加到右侧 如果(temp->right\u son==NULL){//right为空 温度->右上=输入; input=NULL;//添加了新节点,退出循环 } 否则{ temp=temp->right\u-son;//需要从当前位置向右移动 } } } } 标准:cin>>x; } 返回根; } }; int main(){ BinarySearchTree*r=新的BinarySearchTree; BinarySearchTree*r1=新的BinarySearchTree; BinarySearchTree*p=新的BinarySearchTree; p=readListInter(); r->插入(6); r->插入(8); r->插入(1); r->插入(9); r->插入(10); r->插入(4); r->插入(13); r->插入(12); printf(“\n\u\n”); r1->插入(6); r1->插入(8); r1->插入(1); r1->插入(9); r1->插入(10); r1->插入(4); r1->插入(13); r1->插入(12); printf(“\n\u\n”); r->isPerfect(r); int pre[]={8,3,5,7,6}; int size=sizeof(pre)/sizeof(pre[0]); if(hasOnlyOneChild(pre,size)==true) printf(“是”); 其他的 printf(“否”); s 返回0; }

函数不能';还没有解决 我的C++代码有问题。它说所有以isPerfectRec()开头的函数都无法解析…为什么?我试过很多方法,但显然都不管用。我有很多帮助,比如验证二元搜索树是否完美,在二元搜索树中查找第二大元素等等 #include <stdio.h> #include<iostream> #include<stack> template<typename T> class BinarySearchTree { public: BinarySearchTree<T> *root, *left_son, *right_son, *parent; T *pinfo; BinarySearchTree() { left_son = right_son = NULL; root = this; pinfo = NULL; } void setInfo(T info) { pinfo = new T; *pinfo = info; } void insert(T x) { if (pinfo == NULL) setInfo(x); else insert_rec(x); } bool isPerfectRec(BinarySearchTree *root, int d, int level = 0) { // An empty tree is perfect if (*root == NULL) return true; // If leaf node, then its depth must be same as // depth of all other leaves. if (*root->left_son == NULL && root->*right_son == NULL) return (d == level+1); // If internal node and one child is empty if (root->*left_son == NULL || root->*right_son == NULL) return false; // Left and right subtrees must be perfect. return isPerfectRec(root->*left_son, d, level+1) && isPerfectRec(root->*right_son, d, level+1); } // Wrapper over isPerfectRec() bool isPerfect(BinarySearchTree *root) { int d = findADepth(root); return isPerfectRec(root, d); } int findADepth(BinarySearchTree *node) { int d = 0; while (node != NULL) { d++; node = node->left_son; } return d; } // A function to find 2nd largest element in a given tree. void secondLargestUtil(BinarySearchTree *root, int &c) { // Base cases, the second condition is important to // avoid unnecessary recursive calls if (root == NULL || c >= 2) return; // Follow reverse inorder traversal so that the // largest element is visited first secondLargestUtil(root->right_son, c); // Increment count of visited nodes c++; // If c becomes k now, then this is the 2nd largest if (c == 2) { std::cout << "2nd largest element is " << root->pinfo; printf("\n___\n"); return; } // Recur for left subtree secondLargestUtil(root->left_son, c); } void secondLargest(BinarySearchTree *root) { // Initialize count of nodes visited as 0 int c = 0; // Note that c is passed by reference secondLargestUtil(root, c); } bool hasOnlyOneChild(int pre[], int size) { int nextDiff, lastDiff; for (int i=0; i<size-1; i++) { nextDiff = pre[i] - pre[i+1]; lastDiff = pre[i] - pre[size-1]; if (nextDiff*lastDiff < 0) return false;; } return true; } BinarySearchTree * readListInter(){ BinarySearchTree* root = NULL;//returning object BinarySearchTree* temp; BinarySearchTree* input;//new node to add int x; std::cout << "enter number (>0 to stop): "; std::cin >> x; while(x>=0){ input = BinarySearchTree(x); if(root == NULL){//if root is empty root = input; temp = root;//temp is use to store value for compare } else{ temp = root; //for each new addition, must start at root to find correct spot while(input != NULL){ if( x < temp->pinfo){//if smaller x to add to left if(temp->left_son == NULL){//left is empty temp->left_son = input; input = NULL;//new node added, exit the loop } else{//if not empty set temp to subtree temp = temp->left_son;//need to move left from the current position } } else{//otherwise x add to right if(temp->right_son == NULL){//right is empty temp->right_son = input; input = NULL;//new node added, exit the loop } else{ temp = temp->right_son;//need to move right from the current position } } } } std::cin >> x; } return root; } }; int main() { BinarySearchTree<int> *r = new BinarySearchTree<int>; BinarySearchTree<int> *r1 = new BinarySearchTree<int>; BinarySearchTree<int> *p = new BinarySearchTree<int>; p = readListInter(); r->insert(6); r->insert(8); r->insert(1); r->insert(9); r->insert(10); r->insert(4); r->insert(13); r->insert(12); printf("\n___\n"); r1->insert(6); r1->insert(8); r1->insert(1); r1->insert(9); r1->insert(10); r1->insert(4); r1->insert(13); r1->insert(12); printf("\n___\n"); r->isPerfect(r); int pre[] = {8, 3, 5, 7, 6}; int size = sizeof(pre)/sizeof(pre[0]); if (hasOnlyOneChild(pre, size) == true ) printf("Yes"); else printf("No"); s return 0; } #包括 #包括 #包括 模板类二进制搜索树{ 公众: BinarySearchTree*根,*左子,*右子,*父; T*pinfo; 二进制搜索树(){ 左儿子=右儿子=空; 根=这个; pinfo=NULL; } 无效设置信息(T信息){ pinfo=新T; *pinfo=info; } 空白插入(T x){ if(pinfo==NULL) setInfo(x); 其他的 插入_rec(x); } bool isPerfectRec(二进制搜索树*root,int d,int level=0) { //一棵空树是完美的 如果(*root==NULL) 返回true; //如果是叶节点,则其深度必须与 //所有其他叶子的深度。 如果(*root->left\u-son==NULL&&root->*right\u-son==NULL) 回报率(d==水平+1); //如果内部节点和一个子节点为空 if(root->*left_-son==NULL | | root->*right_-son==NULL) 返回false; //左右子树必须是完美的。 返回isPerfectRec(根->*左上,d,级别+1)&& iPerfectRec(根->*右上,d,级别+1); } //isPerfectRec()上的包装器 bool isPerfect(二进制搜索树*root) { int d=findADepth(根); 返回isPerfectRec(根,d); } int findADepth(二进制搜索树*节点) { int d=0; while(节点!=NULL) { d++; 节点=节点->左侧子节点; } 返回d; } //查找给定树中第二大元素的函数。 void secondLargestUtil(二进制搜索树*root,int&c) { //基本情况下,第二个条件对于 //避免不必要的递归调用 if(root==NULL | | c>=2) 回来 //按照反向顺序遍历,以便 //首先访问最大的元素 第二个最大值(根->右上,c); //访问节点的增量计数 C++; //如果c现在变成k,那么这是第二大 如果(c==2) { std::cout left_son,c); } 第二大空(BinarySearchTree*根) { //将访问的节点数初始化为0 int c=0; //注意,c是通过引用传递的 第二大类(根,c); } 布尔只有一个子(整数前[],整数大小) { int nextDiff,lastDiff; 对于(int i=0;i>x; 而(x>=0){ 输入=二进制搜索树(x); 如果(root==NULL){//如果root为空 根=输入; temp=root;//temp用于存储值以进行比较 } 否则{ temp=root;//对于每个新添加,必须从root开始才能找到正确的位置 while(输入!=NULL){ 如果(xpinfo){//如果较小的x添加到左侧 如果(temp->left\u son==NULL){//left为空 温度->左上=输入; input=NULL;//添加了新节点,退出循环 } else{//如果不是空的,则将temp设置为子树 temp=temp->left\u-son;//需要从当前位置向左移动 } } else{//否则x添加到右侧 如果(temp->right\u son==NULL){//right为空 温度->右上=输入; input=NULL;//添加了新节点,退出循环 } 否则{ temp=temp->right\u-son;//需要从当前位置向右移动 } } } } 标准:cin>>x; } 返回根; } }; int main(){ BinarySearchTree*r=新的BinarySearchTree; BinarySearchTree*r1=新的BinarySearchTree; BinarySearchTree*p=新的BinarySearchTree; p=readListInter(); r->插入(6); r->插入(8); r->插入(1); r->插入(9); r->插入(10); r->插入(4); r->插入(13); r->插入(12); printf(“\n\u\n”); r1->插入(6); r1->插入(8); r1->插入(1); r1->插入(9); r1->插入(10); r1->插入(4); r1->插入(13); r1->插入(12); printf(“\n\u\n”); r->isPerfect(r); int pre[]={8,3,5,7,6}; int size=sizeof(pre)/sizeof(pre[0]); if(hasOnlyOneChild(pre,size)==true) printf(“是”); 其他的 printf(“否”); s 返回0; },c++,binary-search-tree,C++,Binary Search Tree,我认为您需要在这些函数中编写BinarySearchTree而不是BinarySearchTree作为数据类型。这是一个输入错误:p=readListInter()?而且绝对没有成员调用inOrderTraversal中的,因此代码与报告的错误不匹配。对不起,我忘记删除那一行了,inOrderTraversal它工作正常。您可以复制/粘贴错误消息吗?无法解析函数“readListInter”。没有这样的函数,您有一个可以由r->readLi调用的函数

我认为您需要在这些函数中编写
BinarySearchTree
而不是
BinarySearchTree
作为数据类型。

这是一个输入错误:
p=readListInter()
?而且绝对没有成员调用inOrderTraversal中的
,因此代码与报告的错误不匹配。对不起,我忘记删除那一行了,inOrderTraversal它工作正常。您可以复制/粘贴错误消息吗?无法解析函数“readListInter”。没有这样的函数,您有一个可以由r->readLi调用的函数