b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

C++创建二叉排序树并中序遍历

电脑杂谈  发布时间:2019-08-13 11:01:36  来源:网络整理

二叉树的层次遍历_二叉排序树中序遍历_用文字描述先(根)序的遍历二叉树算法算法,中(根)序的遍历二

#include<iostream>

using namespace std;

struct BinSearchNode;

typedef struct BinSearchNode *PBinSearchNode;

struct BinSearchNode{

int key;

PBinSearchNode llink,rlink;

};

typedef struct BinSearchNode *BinSearchTree;

typedef BinSearchTree *PBinSearchTree;

int search(PBinSearchTree ptree,int key,PBinSearchNode *position){

PBinSearchNode p,q;

p=*ptree;

q=p;

while(p!=NULL){

q=p;

if(p->key==key){

用文字描述先(根)序的遍历二叉树算法算法,中(根)序的遍历二_二叉树的层次遍历_二叉排序树中序遍历

*position=p;

return 1;

}

else if(p->key>key)

p=p->llink;

else

p=p->rlink;

}

*position=q;

return ;

}

int insert(PBinSearchTree ptree,int key){

PBinSearchNode p,position;

if(search(ptree,key,&position)==1)

return 1;

p=(PBinSearchNode)malloc(sizeof(struct BinSearchNode));

if(p==NULL){

二叉排序树中序遍历_二叉树的层次遍历_用文字描述先(根)序的遍历二叉树算法算法,中(根)序的遍历二

cout<<"Error"<<endl;

return ;

}

p->key=key;

p->llink=p->rlink=NULL;

if(position==NULL)

*ptree=p;

else if(key<position->key)

position->llink=p;

else

position->rlink=p;

return 1;

}

int createSearchTree(PBinSearchTree ptree){

*ptree=NULL;

int key;

cin>>key;

二叉树的层次遍历_用文字描述先(根)序的遍历二叉树算法算法,中(根)序的遍历二_二叉排序树中序遍历

while(key!='#'){

insert(ptree,key);

cin>>key;

}

return 1;

}

void inOrder(PBinSearchNode ptree){

if(ptree!=NULL){

inOrder(ptree->llink);

cout<<ptree->key+" ";

inOrder(ptree->rlink);

}

}

void main(){

cout<<"请输入二叉排序树的节点值二叉排序树中序遍历,以'#'号结束"<<endl;

BinSearchTree ptree;

PBinSearchNode p;

二叉排序树中序遍历_二叉树的层次遍历_用文字描述先(根)序的遍历二叉树算法算法,中(根)序的遍历二

createSearchTree(&ptree);

p=ptree;

inOrder(p);

}

出了多少难题,运行的时候输入#就会没反应了二叉排序树中序遍历,也不提示按任意键退出

int createSearchTree(PBinSearchTree ptree){

*ptree=NULL;//将二叉排序树置空

int key;

string keyStr;

cin>>keyStr;

stringstream ss;

while(keyStr[]!='#'){//将新节点插入树中

ss<<keyStr;

ss>>key;

insert(ptree,key);

cin>>keyStr;

}

return 1;


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-119083-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      热点图片
      拼命载入中...