當前位置:首頁 » 編程語言 » c語言先序遍歷

c語言先序遍歷

發布時間: 2023-07-17 13:53:20

㈠ 數據結構用c語言寫:創建樹,並進行先序,中序,後序遍歷!

#include <iostream>
using namespace std;

class Node
{
public:
Node(){ this->pLchild = this->pRchild = NULL; }
char data;
Node * pLchild;
Node * pRchild;
};
Node * CreateTree()
{
Node * root = new Node;
root->data = 'A';
Node * pB = new Node;
pB->data = 'B';
Node * pC = new Node;
pC->data = 'C';
Node * pD = new Node;
pD->data = 'D';
Node * pE = new Node;
pE->data = 'E';

root->pLchild = pB;
root->pRchild = pC;
pC->pLchild = pD;
pD->pRchild = pE;

return root;
}
void InTraverseTree(Node * pT) //中序遍歷
{
if (NULL == pT)
return;
else
{
InTraverseTree(pT->pLchild);
cout << pT->data;
InTraverseTree(pT->pRchild);

}
}
void PostTraverseTree(Node * pT) //後序遍歷
{
if (NULL == pT)
return;
else
{
PostTraverseTree(pT->pLchild);

PostTraverseTree(pT->pRchild);
cout << pT->data;
}
}
void PreTraverseTree(Node * pT) //先序遍歷
{
if (NULL==pT)
{
return;
}
else
{
cout << pT->data;
PreTraverseTree(pT->pLchild);
PreTraverseTree(pT->pRchild);
}
}
void destroyTree(Node * PT) //銷毀二叉樹
{
if (NULL == PT)
return;
else
{
destroyTree(PT->pLchild);
destroyTree(PT->pRchild);
delete PT;
}

}
int main()
{
Node * pT = CreateTree();
cout << "先序遍歷";

PreTraverseTree(pT);

destroyTree(pT);
/*cout << "\n中序遍歷" ;
InTraverseTree(pT);
cout << "\n後序遍歷";
PostTraverseTree(pT);*/
// PreTraverseTree(pT);
//cout << endl;
system("pause");
return 0;
}
用C++寫的 不過跟C沒什麼區別

㈡ C語言數據結構樹的前序遍歷演算法求指教

首先創建二叉樹,個人喜歡用先序次序創建,如
int CreateBiTree(Tree *T)// 按先序次序輸入二叉樹中結點的值,'#'字元表示空樹,構造二叉鏈表表示的二叉樹T
{
char ch;
scanf("%c",&ch);
if (ch=='#') T = NULL;
else {
if (!(T = (Tree *)malloc(sizeof(Tree)))) return ERROR;
T->data=ch; // 生成根結點
CreateBiTree(T->lchild); // 構造左子樹
CreateBiTree(T->rchild); // 構造右子樹
}
return OK;
}
再者,調用前序遍歷函數,再運用輸出函數輸出前序遍歷的二叉樹,如:
int Visit(int e ) // 輸出元素e的值
{
printf("%c", e );
return OK;
}
int main()
{
Tree *T;
CreateBiTree(T); //調用按先序次序輸入二叉樹中結點的值(一個字元),構造二叉鏈
pre_order(T,Visit);//調用前序遍歷二叉樹演算法
}

㈢ C語言前序遍歷輸入後序非遞歸遍歷輸出演算法

//輸入先序擴展序列:6500700
//後序遍歷序列:576
//6
///
//57
////
//0000

//輸入先序擴展序列:4210030050600
//後序遍歷序列:132654
//
//4
///
//25
////
//1306
/////
//000000

#include<stdio.h>
#include<stdlib.h>
#definemax100//20//棧的大小
structbitree//二叉樹的結構體
{
intdata;
structbitree*llink;//左分支
structbitree*rlink;//右分支
};

structstack_post//棧的結構體[用於後序遍歷]
{
structbitree*bt;//指向二叉樹
intleftOrRight;//當前節點是哪個分支,1=左分支0=右分支
};

structstack_postsk[max];//全局變數:棧(用於後序遍歷)

//voidbuild(structbitree*n)
//創建二叉樹:先序擴展序列+遞歸法
voidbuild(structbitree**n)
{
//注:structbitree**n是指針的指針
intch;
scanf("%d",&ch);
if(ch==0)
{
*n=NULL;
}
else
{
*n=(structbitree*)malloc(sizeof(structbitree));
if(*n==NULL)
{
printf(" Memoryoverflow! ");
exit(1);
}
(*n)->data=ch;
build(&((*n)->llink));
build(&((*n)->rlink));
}
//原代碼
/*
n=(structbitree*)malloc(sizeof(structbitree));
scanf("%d",&ch);
if(ch==0)
n=NULL;
else
{
if(!(n=(structbitree*)malloc(sizeof(structbitree))))
{
return;
}
n->data=ch;
build(n->llink);
build(n->rlink);
}
*/
}

//後序遍歷序列(非遞歸)
voidPost_Order(structbitree*n)
{
structbitree*p=NULL;
////structstack_postsk[max];//全局變數:棧(用於後序遍歷)
inttop=0;//棧頂為0,top=1用於存放第1個數據
intleftOrRight;//當前節點是哪個分支,1=左分支0=右分支
structbitree*tmpTree;

if(n==NULL)return;
p=n;//當前二叉樹的節點
leftOrRight=1;//1=左分支(默認從左分支開始遍歷)
while(p!=NULL)
{
if(p->llink!=NULL)//有左分支,當前節點入棧
{
top++;
sk[top].bt=p;
sk[top].leftOrRight=leftOrRight;
p=p->llink;
leftOrRight=1;
}
elseif(p->rlink!=NULL)//有右分支,當前節點入棧
{
top++;
sk[top].bt=p;
sk[top].leftOrRight=leftOrRight;
p=p->rlink;
leftOrRight=0;
}
else//該節點是"葉子"
{
printf("%d",p->data);
while(1)//處於"回溯"狀態
{
tmpTree=sk[top].bt;//看[棧頂]
if(tmpTree==NULL)//[棧]已經沒有數據
{
p=NULL;//整個遍歷過程結束,退出循環
break;
}
if(leftOrRight==1)//處於"左分支"回溯
{
if(tmpTree->rlink==NULL)//[棧頂]的節點沒有右分支,出棧,列印
{
p=sk[top].bt;
leftOrRight=sk[top].leftOrRight;
top--;
printf("%d",p->data);
//仍然處於"回溯"狀態
}
else//[棧頂]的節點有右分支,不出棧,右分支成為當前節點
{
p=tmpTree->rlink;
leftOrRight=0;//設置當前處於右分支
break;//退出"回溯"狀態
}
}
else//處於"右分支"回溯
{
//[棧]有節點,出棧,列印
p=sk[top].bt;
leftOrRight=sk[top].leftOrRight;
top--;
printf("%d",p->data);
//仍然處於"回溯"狀態
}
}//while(1)結束
}//if(p->llink!=NULL)else結束
}//while(p!=NULL)結束

//棧頂top=0,說明[棧]已經沒有數據
printf(" 核對,棧頂top=%d ",top);
}

//有錯誤
//原代碼,後序遍歷序列(非遞歸)
/*
voidpostorder(structbitree*n)
{
structbitree*p,*q;
inti;
p=(structbitree*)malloc(sizeof(structbitree));
p=n;
q=(structbitree*)malloc(sizeof(structbitree));
structbitree*s[max];
for(i=0;i<max;i++)
{
s[i]=(structbitree*)malloc(sizeof(structbitree));
}
inttop=-1;
do
{
while(p!=NULL)
{
if(p->rlink!=NULL)
{
top++;
s[top]=p->rlink;
}
p=p->llink;;
}
p=s[top];
top--;
if(p->rlink==NULL||p->rlink==q)
{
printf("%d",p->data);
q=p;
p=NULL;
}
else
p=p->rlink;
}while(p!=NULL||top!=-1);
}
*/

//後序遍歷序列(遞歸)[用於對比測試]
voidpostTest(structbitree*n)
{
if(n!=NULL)
{
postTest(n->llink);
postTest(n->rlink);
printf("%d",n->data);
}
}

intmain()
{
structbitree*n;
//原代碼n=(structbitree*)malloc(sizeof(structbitree));
printf("Inputthepreorder-numbersofthetree('0'forNULL): ");
//原代碼build(n);
build(&n);

printf(" Postorderisbelow:");
//原代碼postorder(n);//後序遍歷序列(非遞歸)
Post_Order(n);//後序遍歷序列(非遞歸)

printf(" 後序遍歷序列(遞歸):");
postTest(n);//用於對比測試

printf(" ");
return0;
}

㈣ 二叉樹先序遍歷演算法流程圖怎麼畫,學的是數據結構c語言。

在計算機軟體專業中,數據結構、以及 C 語言這兩門課程是非常重要的兩門課程。最為重要的是:如果將來想做計算機軟體開發工作的話,那麼對 C 語言中的指針編程、以及遞歸的概念是必須要熟練精通掌握的,因為它和數據結構課程中的鏈表、二叉樹等內容的關系實在是太緊密了。但是這個編程技能必須要依靠自己多上機實踐才能夠真正徹底掌握的。
首先要搞明白二叉樹的幾種遍歷方法:(1)、先序遍歷法:根左右;(2)、中序遍歷法:左根右;(3)、後序遍歷法:左右根。其中根:表示根節點;左:表示左子樹;右:表示右子樹。
至於談到如何畫先序遍歷的流程圖,可以這樣考慮:按照遞歸的演算法進行遍歷一棵二叉樹。

程序首先訪問根節點,如果根節點的值為空(NULL),則停止訪問;如果根節點的值非空,則遞歸訪問二叉樹的左子樹(left),然後是依然判斷二叉樹下面的左子樹下面的根節點是否為空(NULL),如果根節點的值為空(NULL),則返回上一層,再訪問二叉樹的右子樹(right)。依此類推。

㈤ C語言數據結構,急求在線二叉樹先序中序後序遞歸遍歷

#include
<iostream.h>
#include
<stdio.h>
#include
<malloc.h>
#define
MaxNode
100
typedef
char
DataType;
typedef
struct
node
{
DataType
data;
struct
node
*lchild;
struct
node
*rchild;
}BiTNode,BiTree;
void
CreateBiTree(BiTree
*bt)//建立一個二叉樹
{
char
ch;
//ch=getchar();
scanf("%c",&ch);
if
(ch=='
')
bt=NULL;
else{
bt=(BiTree*)malloc(sizeof(BiTNode));
bt->data=ch;
CreateBiTree(bt->lchild);
CreateBiTree(bt->rchild);
}
}
void
PreOrder(BiTree
*root)//前序遍歷
{
if(root!=NULL)
{
Visit(root->data);
PreOrder(root->lchild);
PreOrder(root->rchild);
}
}
void
InOrder(BiTree
*root)//中序遍歷
{
if(root!=NULL)
{
InOrder(root->lchild);
Visit(root->data);
InOrder(root->rchild);
}
}
void
LaOrder(BiTree
*root)//後序遍歷
{
if(root!=NULL)
{
PreOrder(root->lchild);
PreOrder(root->rchild);
Visit(root->data);
}
}
void
main()
{
BiTree
*bt;
printf("請輸入數據:\n");
bt=
NULL;
CreateBiTree(bt);
//and
here
printf("\n
結果如下:\n");
printf("先序遍歷的結果為:\n");
PreOrder(bt);
printf("\n");
printf("中序遍歷的結果為:\n");
InOrder(bt);
printf("\n");
printf("後序遍歷的結果為:\n");
LaOrder(bt);
}
有個Visit()函數
你沒寫!!
我只是改了語法錯誤!!
只剩那一個函數沒定義
你定義下就沒語法錯誤了!

熱點內容
cryengine源碼 發布:2025-02-08 09:50:58 瀏覽:392
aardio可以反編譯嗎 發布:2025-02-08 09:50:53 瀏覽:482
公司營業執照密碼是什麼 發布:2025-02-08 09:47:56 瀏覽:854
體驗腳本 發布:2025-02-08 09:46:15 瀏覽:690
醫學生需要什麼配置的筆記本 發布:2025-02-08 09:45:34 瀏覽:771
騷擾電話資料庫 發布:2025-02-08 09:45:34 瀏覽:179
u盤文件加密器 發布:2025-02-08 09:40:35 瀏覽:769
plc數據存儲app 發布:2025-02-08 09:37:17 瀏覽:708
伺服器的峰值高低有什麼區別 發布:2025-02-08 09:35:46 瀏覽:689
maven預編譯 發布:2025-02-08 09:20:34 瀏覽:755