當前位置:首頁 » 操作系統 » 二叉樹演算法c

二叉樹演算法c

發布時間: 2022-07-14 22:17:58

1. 二叉樹c語言實現

#include<iostream.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
char data;
struct node *lchild,*rchild;//
}BiTNode,*BiTree;
void CreatBiTree(BiTree &T)
{
char ch;
ch=getchar();
if (ch == ' ')
T = 0;
else {
T=(BiTNode*)malloc(sizeof(BiTNode));
T->data=ch;//生成根節點
CreatBiTree(T->lchild);//構造左子樹
CreatBiTree(T->rchild);//構造右子樹
}
}
void preorder(BiTree T)//前序遍歷
{
if (T!=NULL){
printf ("%c",T->data);
preorder(T->lchild);
preorder(T->rchild);
}
}
void inorder(BiTree T)//中序遍歷
{
if (T!=NULL){
inorder(T->lchild);
printf ("%c",T->data);
inorder(T->rchild);
}
}
void postorder(BiTree T)//後序遍歷
{
if (T!=NULL){
postorder(T->lchild);
postorder(T->rchild);
printf ("%c",T->data);
}
}
void main ()
{
cout<<"請輸入要創建的二叉樹包括空格:"<<endl ;
BiTree T;
CreatBiTree(T);//創建二叉樹
cout<<"前序遍歷的結果為:"<<endl;
preorder(T);
cout<<endl;
cout<<"中序遍歷的結果為:"<<endl;
inorder(T);
cout<<endl;
cout<<"後序遍歷的結果為:"<<endl;
postorder(T);
}

2. C語言二叉樹遞歸演算法怎麼做

#include<stdio.h>
#include<string.h>

structtreenode{
intvalue;
treenode*left;
treenode*right;
};
typedeftreenode*BiTree;

voidvisit(treenode*node)
{
printf("%2d",node->value);
}

//結點總數
intnode(BiTreeT)
{
if(!T){
return0;
}
returnnode(T->left)+node(T->right)+1;
}

//前序
voidpreOrder(BiTreeT)
{
if(T){
visit(T);
preOrder(T->left);
preOrder(T->right);
}
}

//中序
voidinOrder(BiTreeT)
{
if(T){
inOrder(T->left);
visit(T);
inOrder(T->right);
}
}

//後序
voidpostOrder(BiTreeT)
{
if(T){
postOrder(T->left);
postOrder(T->right);
visit(T);
}
}

//葉子節點數
intleafnode(BiTreeT)
{
if(T){
if(!T->left&&!T->right)
return1;
else
leafnode(T->left)+leafnode(T->right);
}else{
return0;
}
}

intheight(BiTreeT)
{
if(T){
intlh=height(T->left);
intrh=height(T->right);
return(lh>rh?lh:rh)+1;
}else{
return0;
}
}

intmain()
{


return0;
}

3. 求一個c語言遍歷二叉樹的演算法

#include <stdio.h>
#include <stdlib.h>
//1 根據二叉樹的性質5,結點按完全二叉樹來編號,則根據結點編號,
// 就可算出其雙親結點的編號,以及該結點是左孩子還是右孩子,
// 這樣一來,就可把該結點的指針賦予雙親結點的相應指針域。
// 怎樣找到雙親結點呢?,在輸入雙親結點的同時要把結點的指針
// 保存起來。也就是說,要設計一個指針數組,來保存每個結點指針。
// 這樣,當輸入下層結點時,才能找到它的雙親。
//2 回想單鏈表的建立過程,單鏈表建立過程中,只需把當前結點,
// 當成前驅結點,故只需設計一個指針變數即可。

typedef char ElementType;

typedef struct node //二叉樹鏈表結點
{
ElementType data;
struct node *lchild,*rchild;//左、右孩子指針
}BinNode,*BinTree; //結點和結點指針的標識符

BinNode * creat(void) //建二叉樹鏈表(返回根結點的指針)
{
int i,j;
ElementType x;
BinNode *q,*s[20];//結點指針、輔助數組(存放結點的指針,該結點有可能是雙親結點)
BinNode *t=NULL; //根結點指針(目前是空樹,生成樹後要返回根結點指針)

printf("\n 請輸入結點編號i和結點值x");
printf("\n 如:1A 2B 3C 4D 5E 7F 00(全為0,輸入結束)");
printf("\n 或:1A 2B 3C 4D 6F 7G 00(全為0,輸入結束)");
printf("\n 或:1A 2B 3C 5E 7G 15M 00(全為0,輸入結束)\n");
scanf("%d%c",&i,&x); //輸入結點編號及結點值

while((i!=0)&&(x!=0))
{
q=(BinNode *)malloc(sizeof(BinNode));//申請結點內存
q->data=x; //保存數據
q->lchild=NULL;
q->rchild=NULL;
s[i]=q; //s[i]存放第i號結點的指針
if(i==1) //1號結點是根結點
t=q; //保存根結點指針,以備返回
else
{
j=i/2; //由該結點號求雙親結點號
if((i%2)==0)
s[j]->lchild=q; //i為偶數是左孩子,該結點指針存入雙親結點的左孩子指針
else
s[j]->rchild=q; //i為奇數是右孩子,該結點指針存入雙親結點的右孩子指針
}
scanf("%d%c",&i,&x);//繼續輸入結點編號和結點值
}
return t; //返回根結點的指針(二叉鏈表的指針)
}

void DisplayBinTree(BinTree T)//用縮進表示二叉樹
{
BinTree stack[100],p; //棧(結點指針數組)、當前結點指針
int level[100]; //棧(每層根結點對應的空格 數 )
int flag[100]; //棧(flag[]=0,1,2分別表示是根結點、左子樹、右子樹 )
int top,n,i; //棧頂指針,空格個數,循環變數

if(T!=NULL) //若有根結點
{
top=1; //1號結點(根結點 )
stack[top]=T; //入棧(保存根結點指針)
level[top]=1; //顯示空格的個數
flag[top]=0; //根結點
while(top>0) //有根結點
{
p=stack[top]; //取根結點指針
n=level[top]; //取顯示空格的個數

for(i=1;i<=n;i++)//顯示空格(縮進)
printf(" ");

if(flag[top]==0) //若是根結點
printf("T:%c\n",p->data); //顯示根結點
else //不是根結點
{
if(flag[top]==2) //是右子樹根結點
printf("R:%c\n",p->data); //顯示右子樹根結點
if(flag[top]==1) //是左子樹根結點
printf("L:%c\n",p->data,top); //顯示左子樹根結點
}

top--; //顯示一個(出棧一個)結點,top-1

if(p->rchild!=NULL)//若有右孩子
{
top++; //保存一個根結點,top+1
stack[top]=p->rchild;//保存右子樹根結點
level[top]=n+3;
flag[top]=2;
}
if(p->lchild!=NULL)//若有左孩子
{
top++;
stack[top]=p->lchild;//保存左子樹根結點
level[top]=n+3;
flag[top]=1;
}
// printf("level[top]=%d\n",level[top]);
}
}
}

main()
{
BinNode *T; //根結點的指針
T=creat(); //建二叉樹
printf("\n用縮進表示二叉樹的層次(如ppt62所示):\n");
DisplayBinTree(T);
getch();
}

4. C語言 二叉樹 遞歸查找演算法

你的if(T->data==x)後面的{}裡面沒有返回,導致不能及時推出
望採納,謝謝

5. 數據結構c二叉樹的演算法

額 我來講講吧:
第一個問題 你問應該用何種方式來存儲,這個問題糾結了,什麼叫應該用什麼方式存儲,當然是都可以啦兩種方式~~不過你的意思可能是哪種方式最好,如果就是進行那兩種操作的話,是順序存儲方式比較好(你應該知道順序和鏈式兩種吧);其實二叉樹是很少用順序方式存儲的。但是由於你已經說了你是完全二叉樹,那麼就可以考慮順序方式存儲了;書序二叉樹每個節點你可以編號,直接運算序號就可以找到父節點和兩個子節點了。
第二個問題 用C++寫了 就採用鏈式結構吧 每個節點內有lchild rchild指針分別指向左右兩孩子結點;結點類型就假定Node吧:
void exchange (Node * node){
exchange(node->lchild);
exchange(node->rchild);
Node * n;
n=node->lchild;
node->lchild=node->rchild;
node->rchild=n;
}//遞歸方式實現的函數
exchange(bt);
非遞歸演算法就需要藉助隊列了 代碼較長 不想打了;隊列就是實現按層次操作每個節點;操作玩的結點一次出隊,出隊的同時他們的孩子一次入隊,最後沒有結點入隊出隊就演算法結束了;一般都是深度優先的遞歸演算法來使用樹形結構,很少有按層次結構非遞歸演算法的,樹形結構發明出來就是讓你深度搜索用的

6. 二叉樹C語言演算法,急!!!!

清華大學
嚴蔚敏
的<數據結構里>都有完整的代碼,解釋的也很清楚
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
typedef
struct
tree
{
struct
tree
*left;
int
date;
struct
tree
*right;
}treenode,*b_tree;
///////插入節點/////////////////////
b_tree
insert(b_tree
root,int
node)
{
b_tree
newnode;
b_tree
currentnode;
b_tree
parentnode;
newnode=(b_tree)malloc(sizeof(treenode));
newnode->date=node;
newnode->right=NULL;
newnode->left=NULL;
if(root==NULL)
return
newnode;
else
{
currentnode=root;
while(currentnode!=NULL)
{
parentnode=currentnode;
if(currentnode->date>node)
currentnode=currentnode->left;
else
currentnode=currentnode->right;
}
if(parentnode->date>node)
parentnode->left=newnode;
else
parentnode->right=newnode;
}
return
root;
}
//////建立樹///////////////////
b_tree
creat(int
*date,int
len)
{
b_tree
root=NULL;
int
i;
for(i=0;i<len;i++)
root=insert(root,date[i]);
return
root;
}
//////中序列印////////////////
void
print1(b_tree
root)
{if(root!=NULL)
{
print1(root->left);
printf("%d->",root->date);
print1(root->right);
}
}
//////後序列印////////////////
void
print2(b_tree
root)
{if(root!=NULL)
{
print2(root->left);
print2(root->right);
printf("%d->",root->date);
}
}
//////前序列印////////////////
void
print3(b_tree
root)
{if(root!=NULL)
{
printf("%d->",root->date);
print3(root->left);
print3(root->right);
}
}
//////////在二叉樹中查找給定關鍵字
////////////
b_tree
lookfor(b_tree
root,int
e)
{
b_tree
p1,p2;
if(root!=NULL)
{
if(root->date==e)
return
root;
else
p1=lookfor(root->left,e);
p2=lookfor(root->right,e);
if(p1!=NULL)
return
p1;
else
if(p2!=NULL)
return
p2;
else
return
NULL;
}
else
return
NULL;
}
///////測試函數//////////////////
void
main()
{
b_tree
root=NULL;
int
i,index;
int
value;
int
nodelist[20];
cout<<"輸入樹的節點,輸入0結束\n";
index=0;
cin>>value;
while(value!=0)
{
nodelist[index]=value;
index=index+1;
cin>>value;
}
root=creat(nodelist,index);
printf("\n中序列印\n");
print1(root);
printf("\n後序列印\n");
print2(root);
printf("\n前序列印\n");
print3(root);
printf("\n查找的詞:\n");
int
a;
scanf("%d",&a);
b_tree
p3=lookfor(root,a);
if(p3!=NULL)
printf("%d\n",p3->date);
else
printf("沒你要找的詞");
}

7. 求二叉樹遍歷演算法C語言實現的

Status
PreOrderTraverse
(
BiTree
T,
Status
(
*Visit
)
(
TElemType
e
)
)
{
//
採用二叉鏈表存儲結構,Visit
是對數據元素操作的應用函數,先序遍歷二叉樹
T
的遞歸演算法。
if
(
T
)
{
//

T
不為空
if
(
Visit
(
T->data
)
)
//
調用函數
Visit
if
(
PreOrderTraverse
(
T->lchild,
Visit
)
)
//
遞歸調用左子樹
if
(
PreOrderTraverse
(
T->rchild,
Visit
)
)
return
OK;
//
遞歸調用右子樹
return
ERROR;
}
else
return
OK;
}
//
PreOrderTraverse

8. 用c語言寫二叉樹,源代碼。

二叉樹是採用遞歸定義的,實現起來代碼簡潔(也許並不簡單)。並且它在具體的計算機科學中有很重要的運用,是一種很重要的數據結構,二叉樹有三種遍歷和建立的方式。今天先學習一下它的建立和列印。
以下代碼在Win-Tc1.9.1下編譯通過。

#include <stdio.h>
#define ElemType char
//節點聲明,數據域、左孩子指針、右孩子指針
typedef struct BiTNode{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
//先序建立二叉樹
BiTree CreateBiTree(){
char ch;
BiTree T;
scanf("%c",&ch);
if(ch=='#')T=NULL;
else{
T = (BiTree)malloc(sizeof(BiTNode));
T->data = ch;
T->lchild = CreateBiTree();
T->rchild = CreateBiTree();
}
return T;//返回根節點
}
//先序遍歷二叉樹
void PreOrderTraverse(BiTree T){
if(T){
printf("%c",T->data);
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}
}

//中序遍歷
void InOrderTraverse(BiTree T){
if(T){
PreOrderTraverse(T->lchild);
printf("%c",T->data);
PreOrderTraverse(T->rchild);
}
}
//後序遍歷
void PostOrderTraverse(BiTree T){
if(T){
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
printf("%c",T->data);
}
}
void main(){
BiTree T;
T = CreateBiTree();//建立
PreOrderTraverse(T);//輸出
getch();
}

9. C語言 二叉樹

#include<stdio.h>

#include<stdlib.h>


typedef struct Node

{

int e;

struct Node *l, *r;

} Node;


Node *init() //先序遍歷構造二叉樹

{

char n;

Node *p;

scanf("%c", &n);

if (n=='0')

return NULL;

p = (Node*)malloc(sizeof(Node));

if (!p)

exit(0);

p->e = n-'0';

p->l = init();

p->r = init();

return p;

}


void DLR(Node *head) //先序遍歷二叉樹(遞歸演算法)

{

if (head)

{

printf("%d", head->e);

DLR(head->l);

DLR(head->r);

}

}

void destory(Node *head) //銷毀二叉樹

{

Node *l, *r;

if (!head)

return;

l = head->l;

r = head->r;

free(head);

destory(l);

destory(r);

}

int main()

{

Node *head = init();

DLR(head);

destory(head);

return 0;

}

熱點內容
wincc圖形編譯在哪裡 發布:2024-09-17 03:58:26 瀏覽:977
androidubuntu 發布:2024-09-17 03:50:27 瀏覽:701
識夢源碼 發布:2024-09-17 03:50:18 瀏覽:26
諾基亞密碼忘了打什麼電話 發布:2024-09-17 03:27:09 瀏覽:555
樹深度優先演算法 發布:2024-09-17 03:26:58 瀏覽:472
跳轉頁源碼 發布:2024-09-17 03:13:05 瀏覽:543
html文件上傳表單 發布:2024-09-17 03:08:02 瀏覽:785
聊天軟體編程 發布:2024-09-17 03:00:07 瀏覽:726
linuxoracle安裝路徑 發布:2024-09-17 01:57:29 瀏覽:688
兩個安卓手機照片怎麼同步 發布:2024-09-17 01:51:53 瀏覽:207