資料結構實驗3《二叉樹的基本操作》
根據輸入的資料建立一個二叉樹;
分別採用前序、中序、後序的遍歷方式顯示輸出二叉樹的遍歷結果
程式碼:
#include<iostream>
using namespace std;
structnode //定義結點
{
char ch;
structnode *lchild;
structnode *rchild;
};
node * create(structnode *p) //利用遞迴函式,按照先序建立二叉樹,以0代表空
{
char c;
cin >> c;
if (c == '0')p = NULL;
else
{
p = new node;
p->ch = c;
p->lchild = create(p->lchild);
p->rchild = create(p->rchild);
}
return p;
}
void preOrder(node*p) //利用遞迴函式,先序遍歷
{
if (p)
{
cout << p->ch << " ";
preOrder(p->lchild);
preOrder(p->rchild);
}
}
void inOrder(node*p) //利用遞迴函式,中序遍歷
{
if ( p)
{
inOrder(p->lchild);
cout << p->ch << " ";
inOrder(p->rchild);
}
}
void postOrder(node*p) //利用遞迴函式,後序遍歷
{
if (p)
{
postOrder(p->lchild);
postOrder(p->rchild);
cout << p->ch << " ";
}
}
int main()
{
node*root = new node;
cout << "輸入二叉樹儲存的字元,以0表示不存在" << endl;
root = create(root); //易錯點1
cout << "先序遍歷是 ";
preOrder(root);
cout << endl << "中序遍歷是 ";
inOrder(root);
cout << endl << "後序遍歷是 ";
postOrder(root);
system("pause>nul");
return 0;
}
用來測試的二叉樹:
執行結果:
相關推薦
資料結構實驗之二叉樹六:哈夫曼編碼(SDUT 3345)
題解:離散中的“最小生成樹(最優樹)”。 #include <bits/stdc++.h> using namespace std; void qusort(int l, int r, int a[]) { int x = a[l]; int i = l, j =
資料結構實驗之二叉樹一:樹的同構 (SDUT 3340)
題解:把原本結構體的左右子樹的型別定義成 int 型,用來存放這個結點的左右子樹的編號,分別建造兩棵二叉樹,按個比較,如果在第二棵樹中沒有找到,那麼就不用在判斷了。 #include <bits/stdc++.h> using namespace std; struct node
資料結構實驗之二叉樹八:(中序後序)求二叉樹的深度(SDUT 2804)
#include <stdio.h> #include <stdlib.h> #include <string.h> struct node { char data ; struct node *l,*r; }; struct node *cr
資料結構實驗之二叉樹七:葉子問題(SDUT 3346)
#include <bits/stdc++.h> using namespace std; struct node { char data; struct node *lc, *rc; }; char a[100]; int num = 0; struct node
資料結構實驗之二叉樹四:(先序中序)還原二叉樹 (SDUT 3343)
#include <bits/stdc++.h> using namespace std; struct node { char data; struct node *lc, *rc; }; char a[100],b[100]; int n; struct node
資料結構實驗之二叉樹五:層序遍歷 (SDUT 3344)
#include <bits/stdc++.h> using namespace std; struct node { char data; struct node *lc, *rc; }; char s[505]; int num; struct node *cre
資料結構實驗之二叉樹三:統計葉子數 SDUT 3342
#include <stdio.h> #include <string.h> struct node { char data; struct node *l,*r; }; struct node *root; char st[51]; int i; in
資料結構實驗之二叉樹二:遍歷二叉樹 SDUT 3341
#include <bits/stdc++.h> using namespace std; struct Tree { char data; struct Tree *right; struct Tree *left; }; char str[55]; in
【二叉樹】SDUT 3342 資料結構實驗之二叉樹三:統計葉子數
Problem Description 已知二叉樹的一個按先序遍歷輸入的字元序列,如abc,,de,g,,f,,, (其中,表示空結點)。請建立二叉樹並求二叉樹的葉子結點個數。 Input 連續輸入多組資料,每組資料輸入一個長度小於50個字元的字串。 Output 輸出
SDUTOJ3344資料結構實驗之二叉樹五:層序遍歷
資料結構實驗之二叉樹五:層序遍歷 https://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Contest/contestproblem/cid/2711/pid/3344 Time Limit: 1000 ms Memo
3340--資料結構實驗之二叉樹一:樹的同構
現給定兩棵樹,請你判斷它們是否是同構的。 Input 輸入資料包含多組,每組資料給出2棵二叉樹的資訊。對於每棵樹,首先在一行中給出一個非負整數N (≤10),即該樹的結點數(此時假設結點從0到N−1編號);隨後N行,第i行對應編號第i個結點,給出該結點中儲存的1
資料結構實驗之二叉樹三:統計葉子數
Problem Description 已知二叉樹的一個按先序遍歷輸入的字元序列,如abc,de,g,f, (其中,表示空結點)。請建立二叉樹並求二叉樹的葉子結點個數。 Input 連續輸入多組資料,每
資料結構實驗之二叉樹二:遍歷二叉樹
Problem Description 已知二叉樹的一個按先序遍歷輸入的字元序列,如abc,de,g,f, (其中,表示空結點)。請建立二叉樹並按中序和後序的方式遍歷該二叉樹。 Input 連續輸入多組資料,每組資料輸入一個長度小於50個字元的字串。 Outpu
資料結構實驗之二叉樹三:統計葉子數(有返回值版)
Problem Description 已知二叉樹的一個按先序遍歷輸入的字元序列,如abc,de,g,f, (其中,表示空結點)。請建立二叉樹並求二叉樹的葉子結點個數。 Input 連續輸入多組資料,每組資料輸入一個長度小於50個字元的字串。 Output 輸出
資料結構實驗之二叉樹四:(先序中序)還原二叉樹
Problem Description 給定一棵二叉樹的先序遍歷序列和中序遍歷序列,要求計算該二叉樹的高度。 Input 輸入資料有多組,每組資料第一行輸入1個正整數N(1 <= N <=
資料結構實驗之二叉樹七:葉子問題
Problem Description 已知一個按先序輸入的字元序列,如abd,eg,cf,(其中,表示空結點)。請建立該二叉樹並按從上到下從左到右的順序輸出該二叉樹的所有葉子結點。 Input 輸入資
資料結構實驗之二叉樹三:統計葉子數 SDUT 3342
#include <stdio.h> #include <string.h> struct node { char data; struct node *l,*
資料結構實驗之二叉樹八:(中序後序)求二叉樹的深度
Problem Description 已知一顆二叉樹的中序遍歷序列和後序遍歷序列,求二叉樹的深度。 Input 輸入資料有多組,輸入T,代表有T組資料。每組資料包括兩個長度小於50的字串,第一個字串表示二叉樹的中序遍歷,第二個表示二叉樹的後序遍歷。 Output
資料結構實驗之二叉樹一:樹的同構
老師說的好,一遍不會兩遍,兩遍不會三遍,三遍不會n遍。。。。總有會的那一天,只要你敢付出學會的時間。。
SDUTOJ3345資料結構實驗之二叉樹六:哈夫曼編碼
資料結構實驗之二叉樹六:哈夫曼編碼 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 字元的編碼方式有多種,除了大家熟悉的ASC