資料結構 建立二叉樹

2021-09-23 14:36:05 字數 1237 閱讀 7005

#include#include#define max 100

using namespace std;

typedef struct bnode

bnode, *btree;

btree q[max];

btree creatree() //層次輸入

rear++;

q[rear] = s;

if (rear == 1) root = s;

else

cin >> ch;

} return root;

}int search(char ino, char pre)//在中序序列中查詢先序中該元素所在位置

void creatbt(btree &t, char pre, char ino, int ps, int is, int n) //使用先序和中序建立二叉樹 }}

btree createbintree() //先序輸入

return t;

}void preorder(btree t)

}void inorder(btree t)

}void postorder(btree t)

}int main()

} while (choice2 != 0);

break;

case 2:

btree tree;

tree = creatree();

do} while (choice2 != 0);

cout << endl;

break;

case 3:

char pre[max], ino[max];

cin >> pre;

cin >> ino;

btree tr;

tr = null;

creatbt(tr, pre, ino, 0, 0, 7);

cout << "先序遍歷的二叉樹:";

preorder(tr);

cout << endl;

cout << "中序遍歷的二叉樹:";

inorder(tr);

cout << endl;

cout << "後序遍歷的二叉樹:";

postorder(tr);

cout << endl;

break;

case 0:

exit(0);

} }}

資料結構 後序建立二叉樹

二叉樹的前,中,後序遍歷 以及通過補 字元補充二叉樹空分支,按照前序順序構造二叉樹,這些都比較簡單,我這就不過多地贅述 直接貼 include stdafx.h includeusing namespace std typedef char datatype typedef struct node ...

資料結構 二叉樹 反轉二叉樹

include using namespace std define maxsize 1000 struct binary tree node class queue queue queue void queue push binary tree node btn binary tree node ...

資料結構(二叉樹) 二叉樹的建立 儲存 遍歷

建立二叉樹,並通過呼叫函式,輸出先序遍歷 中序遍歷與後序遍歷的結果 include include define true 1 define false 0 define ok 1 define error 0 datatype 是二叉樹資料元素型別,此程式定義為char型 typedef char...