二叉树的建立
麻烦大侠们帮我看下,我的代码错在那里了?
- C/C++ code
#include "stdafx.h"#include<iostream>using namespace std;typedef struct BiTreeNode{ char data; BiTreeNode *lchild,*rchild;}*BiTree;BiTree CreateBiTree( );void PrintBiTree(BiTree root);int _tmain(int argc, _TCHAR* argv[]){ BiTree root; root=CreateBiTree( ); PrintBiTree(root); system("pause"); return 0;}BiTree CreateBiTree( ){ BiTree pTemp=new BiTreeNode; /*pTemp->data=NULL; pTemp->lchild=NULL; pTemp->rchild=NULL;*/ char tempCh; cin>>tempCh; if (tempCh) { //BiTree pTemp=new BiTreeNode; pTemp->data=tempCh; pTemp->lchild=CreateBiTree(); pTemp->rchild=CreateBiTree(); } return pTemp;}void PrintBiTree(BiTree root){ if (root==NULL) return ; else { PrintBiTree(root->lchild); cout<<root->data; PrintBiTree(root->rchild); }}
[解决办法]
- C/C++ code
#include "stdafx.h"#include<iostream>using namespace std;typedef struct BiTreeNode{ char data; BiTreeNode *lchild,*rchild;}*BiTree;BiTree CreateBiTree( );void PrintBiTree(BiTree root);int _tmain(int argc, _TCHAR* argv[]){ BiTree root; root=CreateBiTree( ); PrintBiTree(root); system("pause"); return 0;}BiTree CreateBiTree( ){ BiTree pTemp=NULL; /*pTemp->data=NULL; pTemp->lchild=NULL; pTemp->rchild=NULL;*/ char tempCh; cin>>tempCh; if (tempCh!='#')//作为结束标志。不然没有办法结束 { pTemp=new BiTreeNode; pTemp->data=tempCh; pTemp->lchild=CreateBiTree(); pTemp->rchild=CreateBiTree(); } return pTemp;}void PrintBiTree(BiTree root){ if (root==NULL) return ; else { PrintBiTree(root->lchild); cout<<root->data; PrintBiTree(root->rchild); }}
[解决办法]
- C/C++ code
#include <iostream>#include <cstdio>#include <stack>using namespace std;typedef struct Node{ int nData; struct Node* pLeft; struct Node* pRight;}Node;void insert(Node*& pRoot,int num) { if(pRoot==NULL) { Node* pNode=new Node; pNode->nData=num; pRoot=pNode; return; } if(num > pRoot->nData) insert(pRoot->pRight,num); else insert(pRoot->pLeft,num); }void creatTree(Node* pRoot) { printf("请输入整数以创建节点,输入'q'来结束\n"); int num; scanf("%d",&num); pRoot->nData=num; while(scanf("%d",&num)) { insert(pRoot,num); } }void CreatBs(Node* pRoot) { printf("请输入整数以创建节点,输入'q'来结束\n"); int num; scanf("%d",&num); pRoot->nData=num; pRoot->pLeft=NULL; pRoot->pRight=NULL; while(scanf("%d",&num)) { Node* pNode=new Node; pNode->nData=num; pNode->pLeft=NULL; pNode->pRight=NULL; Node* pFind=pRoot; Node* pParent=pFind; while(pFind) { if(num>pFind->nData) { pParent=pFind; pFind=pFind->pRight; }else if(num<pFind->nData) { pParent=pFind; pFind=pFind->pLeft; } else { cout << "the number is already exits" << endl; break; } } if(num>pParent->nData) pParent->pRight=pNode; else pParent->pLeft=pNode; }}void Travel(Node* pCurrent) { if(!pCurrent) return; Travel(pCurrent->pLeft); cout << pCurrent->nData << " "; Travel(pCurrent->pRight); }void Travel_first(Node*& pCurrent) { if(pCurrent) { cout << pCurrent->nData << " " ; Travel_first(pCurrent->pLeft); Travel_first(pCurrent->pRight); } }void Travel_last(Node* pCurrent) { if(!pCurrent) return; Travel_last(pCurrent->pLeft); Travel_last(pCurrent->pRight); cout << pCurrent->nData << " "; }void Travel2(Node* pRoot) { stack<Node*> sn; Node* pNode=pRoot; while(!sn.empty()||pNode!=NULL){ while(pNode) { sn.push(pNode); pNode=pNode->pLeft; } if(!sn.empty()){ pNode=sn.top(); sn.pop(); cout << pNode->nData << " "; pNode=pNode->pRight; } }}void Travel_first2(Node* pRoot){ stack<Node*> sn; Node* pNode=pRoot;// sn.push(pNode); while(!sn.empty()||pNode!=NULL) { while(pNode!=NULL) { cout << pNode->nData << " "; sn.push(pNode); pNode=pNode->pLeft; } if(!sn.empty()) { pNode=sn.top(); sn.pop(); pNode=pNode->pRight; } } }int main(void){ Node* pRoot=new Node; creatTree(pRoot);// CreatBs(pRoot); cout << "中序: "; Travel(pRoot); cout << endl << "中序非递归: "; Travel2(pRoot); cout << endl<<"先序: "; Travel_first(pRoot); cout << endl << "先序非递归: "; Travel_first2(pRoot); cout << endl << "后序: "; Travel_last(pRoot); cout << endl; return 0;}