void iszhengze(BiTree root,int*a){ if(root){ if(*a==0)return; if((root->LChild==NULL&&root->RChild!=NULL)||(root->LChild==NULL&&root->RChild!=NULL)){ *a=0; } iszhengze(root->LChild,&(*a)); iszhengze(root->RChild,&(*a)); } }为什么全是0
时间: 2024-02-23 10:57:17 浏览: 15
这段代码中出现全为0的情况是因为在函数的递归调用中使用了一个指针a的地址传递,并且在递归调用时未按照预期的方式更新指针a的值。由于未提供完整的代码和输入数据,我无法确定问题的具体原因,但是以下是一些可能导致这种情况的原因:
1. 没有正确初始化指针a的值。如果指针a的初始值为0,那么在第一次递归调用时,条件判断`if(*a==0)`就会满足,直接返回0。
2. 在递归调用时,未正确更新指针a的值。如果在递归调用时未正确更新指针a的值,那么每次递归调用时,指针a的值都会保持不变,从而导致函数无法正常工作。
为了解决这个问题,可以尝试在调用iszhengze函数时,正确初始化指针a的值,并在递归调用时,按照预期的方式更新指针a的值。另外,可以在函数中添加一些调试信息,例如打印变量值或使用调试工具,以帮助确定问题的原因。
相关问题
为什么全是no #include<iostream> #include<stdlib.h> #include<cstring> typedef struct node{ char data; struct node*LChild; struct node*RChild; }Tree,*BiTree; BiTree create(char data){ BiTree q; q=(BiTree)malloc(sizeof(Tree)); q->LChild=NULL; q->RChild=NULL; return q; } BiTree TreeBuild(char *preorder,char*inorder,int len){ if(len==0)return NULL; else if(len==1)return create(*preorder); else{ BiTree newnode=create(*preorder); int index=0; for(int i=0;i<len;i++){ if(*(inorder+i)==*(preorder)) index=i; break; } newnode->LChild=TreeBuild(preorder+1,inorder,index); newnode->RChild=TreeBuild(preorder+index+1,inorder+index+1,len-index-1); return newnode; } } void iszhengze(BiTree root,int*a){ if(root){ if(*a==0)return; if((root->LChild==NULL&&root->RChild!=NULL)||(root->LChild==NULL&&root->RChild!=NULL)){ *a=0; } iszhengze(root->LChild,&(*a)); iszhengze(root->RChild,&(*a)); } } int main(){ BiTree root; char pre[100],in[100]; std::cin>>pre; std::cin>>in; int len=strlen(pre); root=TreeBuild(pre,in,len); int a=1; iszhengze(root,&a); if(a==0)std::cout<<"No"; else std::cout<<"Yes"; return 0; }
在代码中,如果输入的先序遍历序列和中序遍历序列对应的二叉树不是一棵真正的二叉搜索树,那么输出结果就是"No",否则输出"Yes"。这里的问题可能出在iszhengze函数的判断条件上。
```
if((root->LChild==NULL&&root->RChild!=NULL)||(root->LChild==NULL&&root->RChild!=NULL))
```
这个条件判断的逻辑是:如果当前节点的左子树为空、右子树不为空,或者左子树不为空、右子树为空,那么就判定这个二叉树不是搜索树。但是这个条件判断中,对于右子树的判断条件写成了两次"root->RChild!=NULL",而左子树的判断条件写成了两次"root->LChild==NULL",这个条件判断的逻辑有点不合理。
所以,可以尝试修改iszhengze函数中的条件判断,将右子树的判断条件改为"root->LChild!=NULL",即左子树不为空即可。修改后的代码如下:
```
void iszhengze(BiTree root, int *a){
if(root){
if(*a==0) return;
if((root->LChild==NULL&&root->RChild!=NULL)||(root->LChild!=NULL&&root->RChild==NULL)){
*a=0;
}
iszhengze(root->LChild,&(*a));
iszhengze(root->RChild,&(*a));
}
}
```
这样修改之后,如果输入的先序遍历序列和中序遍历序列对应的二叉树不是一棵真正的二叉搜索树,那么输出结果就是"No",否则输出结果就是"Yes"。
//二叉链表的结构定义 typedef char ElemType;//元素类型为字符类型 typedef struct BTNode { ElemType data; struct BTNode *lchild, *rchild; }BTNode; typedef struct BTNode bitree; /*函数声明从此处开始*/ //以递归方式创建二叉树(需扩展),如输入:ABD##E##C#FHJ##K##I## bitree * CreateBTree1(); //以递归方式创建二叉树(需扩展),如输入:ABD##E##C#FHJ##K##I## void CreateBTree2(bitree *&root); //销毁二叉树:释放动态分配的空间 void DestroyBTree(bitree *root); /*函数声明到此处结束*/ /*函数定义从此处开始*/ //以递归方式创建二叉树(需扩展),如输入:ABD##E##C#FHJ##K##I## bitree * CreateBTree1() { bitree *root ;char ch; ch=getchar(); if (ch == '#') root = NULL; else { root=(bitree*)malloc(sizeof(bitree)); root->data=ch; root->lchild = NULL; root->rchild = NULL; root->lchild =CreateBTree1(); root->rchild= CreateBTree1(); } return root; } //以递归方式创建二叉树(需扩展),如输入:ABD##E##C#FHJ##K##I## void CreateBTree2(bitree *&root) { char ch; ch=getchar(); if (ch == '#') root = NULL; else { root=(bitree*)malloc(sizeof(bitree)); root->data=ch; root->lchild = NULL; root->rchild = NULL; CreateBTree2(root->lchild); CreateBTree2(root->rchild); } } //销毁二叉树:释放动态分配的空间 void DestroyBTree(bitree *root) { if( root != NULL) { if (root->lchild != NULL) DestroyBTree(root->lchild); if (root->rchild != NULL) DestroyBTree(root->rchild); free(root); } }
CEG###FH##I##,表示根节点为A,A的左子树为B的子树,B的左子树为空,右子树为D的子树,D的左右子树为空,A的右子树为C的子树,C的左子树为空,右子树为E的子树,E的左右子树为空,C的右子树为F的子树,F的左子树为空,右子树为空,A到F中间不含节点(即空节点记为#) bitree *CreatBTree(); //层次遍历输出二叉树 void LevelOrder(bitree *root); //前序遍历递归版 void PreOrder_Traversal(bitree *root); //前序遍历非递归版 void Pre_Order_Traversal(bitree *root); //中序遍历递归版 void InOrder_Traversal(bitree *root); //中序遍历非递归版 void In_Order_Traversal(bitree *root); //后序遍历递归版 void PostOrder_Traversal(bitree *root); //后序遍历非递归版 void Post_Order_Traversal(bitree *root); //计算二叉树的深度 int Tree_Depth(bitree *root); //统计二叉树中叶子节点的个数 int Leaf_Count(bitree *root); //统计二叉树中节点的个数 int Node_Count(bitree *root);
以上是二叉链表的结构定义和一些常用操作函数的函数声明。其中包括以递归和非递归方式实现的前序、中序、后序遍历函数、层次遍历函数、计算二叉树深度函数、统计叶子节点个数函数和统计节点个数函数。其中,以递归方式实现的遍历函数通常使用较简单,但非递归方式实现的遍历函数能够充分发挥栈的作用,具有一定的优势。-------------------------------------------------------------------------------------------------------------------------------------------------------------
English version:
The structure definition of binary linked list is shown below:
typedef char ElemType; //Element type is character
typedef struct BTNode {
ElemType data;
struct BTNode *lchild, *rchild;
} BTNode;
typedef struct BTNode bitree;
The following functions are included in the program:
bitree *CreatBTree(); //create binary tree recursively (to be expanded)
void LevelOrder(bitree *root); //traverse binary tree in level order
void PreOrder_Traversal(bitree *root); //preorder traversal recursively
void Pre_Order_Traversal(bitree *root);//preorder traversal non-recursively
void InOrder_Traversal(bitree *root);//inorder traversal recursively
void In_Order_Traversal(bitree *root);//inorder traversal non-recursively
void PostOrder_Traversal(bitree *root);//postorder traversal recursively
void Post_Order_Traversal(bitree *root);//postorder traversal non-recursively
int Tree_Depth(bitree *root);//calculate the depth of binary tree
int Leaf_Count(bitree *root);//count the number of leaf nodes in binary tree
int Node_Count(bitree *root);//count the number of nodes in binary tree
The above functions include recursive and non-recursive versions of preorder, inorder, and postorder traversal functions, level order traversal function, function to calculate the depth of the binary tree, function to count the number of leaf nodes, and function to count the number of nodes. Recursive traversal functions are usually simpler to implement, but non-recursive traversal functions can fully utilize the stack and have certain advantages.
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)