生成并遍历二叉树?
C++代码如下:
#include<iostream>
#include<string>
using namespace std;
struct TreeNode { // 二叉树结构
char val;
TreeNode *left, *right;
TreeNode(char ch) : val(ch), left(nullptr), right(nullptr) {}
};
// 由扩展前序序列生成二叉树
TreeNode* construct(string& s, int& i) { // 注意传入的i为引用
if (i == s.length())
return nullptr;
TreeNode *root = nullptr;
if (s[i] != '*') {
root = new TreeNode(s[i]);
++i;
root->left = construct(s, i);
++i;
root->right = construct(s, i);
}
return root;
}
void preOrder(TreeNode* root) { // 前序遍历
if (root) {
cout << root->val;
preOrder(root->left);
preOrder(root->right);
}
}
void inOrder(TreeNode* root) { // 中序遍历
if (root) {
inOrder(root->left);
cout << root->val;
inOrder(root->right);
}
}
void postOrder(TreeNode* root) { // 后序遍历
if (root) {
postOrder(root->left);
postOrder(root->right);
cout << root->val;
}
}
int main() {
string s = "ABC**DE*G**F***";
cout << "扩展前序序列为:" << s << endl;
int i = 0;
TreeNode* root = construct(s, i); // 生成该二叉树
cout << "其前序遍历序列为:";
preOrder(root);
cout << endl;
cout << "其中序遍历序列为:";
inOrder(root);
cout << endl;
cout << "其后序遍历序列为:";
postOrder(root);
cout << endl;
return 0;
}
编译通过,输出如下:
符合示例结果,望采纳~
绛旓細TreeNode* root = construct(s, i); // 鐢熸垚璇浜屽弶鏍 cout << "鍏跺墠搴閬嶅巻搴忓垪涓猴細";preOrder(root);cout << endl;cout << "鍏朵腑搴忛亶鍘嗗簭鍒椾负锛";inOrder(root);cout << endl;cout << "鍏跺悗搴忛亶鍘嗗簭鍒椾负锛";postOrder(root);cout << endl;return 0;} 缂栬瘧閫氳繃锛岃緭鍑哄涓嬶細绗﹀悎绀轰緥...
绛旓細娣卞叆鐞嗚В浜屽弶鏍鐨閬嶅巻锛岃鎴戜滑閫氳繃C璇█瀹炵幇闈為掑綊閬嶅巻绠楁硶锛岄鍏堬紝瀹氫箟缁撴瀯浣撳拰鍩烘湰鍑芥暟濡備笅锛<stdio.h>#include <stdlib.h>typedef struct { int data; struct node *lchild, *rchild;} treetp, tree;</鍑芥暟`create(treetp t, int c)`鐢ㄤ簬鍒涘缓鏂扮殑鑺傜偣锛岃緭鍏ヨ妭鐐瑰拰鏁存暟:treetp create(tr...
绛旓細宸茬煡涓妫浜屽弶鏍鐨勪腑搴忓簭鍒椾负CBEDAHGIJF锛屽悗搴忓簭鍒椾负CEDBHJIGFA锛屾瀯閫犳浜屽弶鏍戝涓嬶細浠庡悗搴忕殑鏍逛负A锛屽洖鍒颁腑搴忓皢鍏跺垏鍒嗘垚3閮ㄥ垎锛岄噸澶嶈繖涓繃绋嬪嵆鍙繕鍘燂紝濡傚浘鎵绀恒
绛旓細1锛夊厛搴忛亶鍘嗭紝鎸夌収鏍瑰乏鍙崇殑椤哄簭娌夸竴瀹氳矾寰勭粡杩囪矾寰勪笂鎵鏈夌殑缁撶偣銆傚湪浜屽弶鏍戜腑锛屽厛鏍瑰悗宸﹀啀鍙銆2锛変腑搴忛亶鍘嗭紝棣栧厛閬嶅巻宸﹀瓙鏍戯紝鐒跺悗璁块棶鏍圭粨鐐癸紝鏈鍚庨亶鍘嗗彸瀛愭爲銆3锛夊悗搴忛亶鍘嗭紝鍙鍋氬乏鍙虫牴銆傚湪浜屽弶鏍戜腑锛屽厛宸﹀悗鍙冲啀鏍癸紝鍗抽鍏堥亶鍘嗗乏瀛愭爲锛岀劧鍚庨亶鍘嗗彸瀛愭爲锛屾渶鍚庤闂牴缁撶偣銆4锛夎繖妫典簩鍙夋爲鐨勬牴鑺傜偣鏄疉銆
绛旓細printf("2--鐢ㄨ繖浜涙暣鏁版潵鐢熸垚涓妫浜屽弶鏍\n"); printf("3--鍒嗗埆瀵逛簩鍙夋爲杩涜鍏堝簭閬嶅巻,涓簭閬嶅巻鍜屽悗搴忛亶鍘嗚緭鍑烘爲涓粨鐐瑰厓绱犲簭鍒梊n"); printf("娉ㄦ剰:鍏堝簭閬嶅巻杈撳嚭瑕佹眰閲囩敤闈為掑綊鏉ュ疄鐜癨n\n"); printf("浜х敓100涓(涓嶅ぇ浜100涓斿悇涓嶇浉鍚岀殑)闅忔満鏁存暟:\n"); RandomHundred(ran); for(i=0;i<100;++i)pri...
绛旓細鍒欒浜屽弶鏍戠殑鍓嶅簭閬嶅巻搴忓垪涓篈BDECF锛屼腑搴忛亶鍘嗗簭鍒椾负DBEAFC锛屽悗搴忛亶鍘嗗簭鍒椾负DEBFCA銆傚厛搴閬嶅巻浜屽弶鏍瑙勫垯锛氭牴-宸-鍙 1銆佽闂牴缁撶偣锛2銆佸厛搴忛亶鍘嗗乏瀛愭爲锛3銆佸厛搴忛亶鍘嗗彸瀛愭爲銆備腑搴忛亶鍘嗕簩鍙夋爲瑙勫垯锛氬乏-鏍-鍙 1銆佸厛涓簭閬嶅巻宸﹀瓙鏍戯紱2銆佸啀璁块棶鏍硅妭鐐癸紱3銆佹渶鍚庤闂腑搴忛亶鍘嗗彸瀛愭爲銆傚悗搴忛亶鍘嗕簩鍙夋爲瑙勫垯...
绛旓細瀹屽叏浜*鏍戞槸鎸囬櫎鏈鍚庝竴灞傚锛屾瘡涓灞備笂鐨勭粨鐐规暟鍧囪揪鍒版渶澶у硷紝鍦ㄦ渶鍚庝竴灞備笂鍙己灏戝彸杈圭殑鑻ュ共缁撶偣銆備簩*鏍戝瓨鍌ㄧ粨鏋勯噰鐢ㄩ摼寮忓瓨鍌ㄧ粨鏋勶紝瀵逛簬婊′簩*鏍戜笌瀹屽叏浜*鏍戝彲浠ユ寜灞傚簭杩涜椤哄簭瀛樺偍銆備簩*鏍戠殑閬嶅巻锛氾紙1锛夊墠搴忛亶鍘嗭紙DLR锛夛紝棣栧厛璁块棶鏍圭粨鐐癸紝鐒跺悗閬嶅巻宸﹀瓙鏍戯紝鏈鍚庨亶鍘嗗彸瀛愭爲锛涳紙2锛変腑搴忛亶鍘嗭紙LDR锛夛紝...
绛旓細鍓嶅簭閬嶅巻棣栧厛璁块棶鏍圭粨鐐圭劧鍚庨亶鍘嗗乏瀛愭爲锛屾渶鍚庨亶鍘嗗彸瀛愭爲銆傚湪閬嶅巻宸︺佸彸瀛愭爲鏃讹紝浠嶇劧鍏堣闂牴缁撶偣锛岀劧鍚庨亶鍘嗗乏瀛愭爲锛屾渶鍚庨亶鍘嗗彸瀛愭爲銆備腑搴忛亶鍘嗛鍏堥亶鍘嗗乏瀛愭爲锛岀劧鍚庤闂牴缁撶偣锛屾渶鍚庨亶鍘嗗彸瀛愭爲銆傝嫢浜屽弶鏍涓虹┖鍒欑粨鏉熻繑鍥炪傚洜姝わ紝A鏄牴缁撶偣锛孊鏄疉鐨勫乏瀛愭爲锛孎鏄疉鐨勫彸瀛愭爲銆侲鏄疊鐨勫乏瀛愭爲锛孋鏄疊鐨勫彸瀛愭爲锛...
绛旓細printf("2--鐢ㄨ繖浜涙暣鏁版潵鐢熸垚涓妫浜屽弶鏍\n"); printf("3--鍒嗗埆瀵逛簩鍙夋爲杩涜鍏堝簭閬嶅巻,涓簭閬嶅巻鍜屽悗搴忛亶鍘嗚緭鍑烘爲涓粨鐐瑰厓绱犲簭鍒梊n"); printf("娉ㄦ剰:鍏堝簭閬嶅巻杈撳嚭瑕佹眰閲囩敤闈為掑綊鏉ュ疄鐜癨n\n"); printf("浜х敓100涓(涓嶅ぇ浜100涓斿悇涓嶇浉鍚岀殑)闅忔満鏁存暟:\n"); RandomHundred(ran); for(i=0;i<100;++i)pri...
绛旓細int createTree(); //鍒涘缓鏍 int preTravel(); //鍏堝簭閬嶅巻鏍 int inTravel(); //涓簭閬嶅巻鏍 };struct treeNode * createBT(struct treeNode *bt, int k){ char b;struct treeNode *p, *t;b = getchar();if (b != '0'){ p = (struct treeNode *)malloc(sizeof(struct tree...