1. 程式人生 > >c語言實現二叉樹的遍歷和建立程式(附帶註釋)

c語言實現二叉樹的遍歷和建立程式(附帶註釋)

/******************************************************************/
//樹的遞迴思想,把每個節點當作是一棵樹,以後序遍歷為例
//步驟1:訪問左子樹.2訪問右子樹3.列印當前節點的值
//在節點遍歷時如果某節點沒有左節點或者是右節點是傳遞的指標是NULL
//說明下一步沒有樹了可以返回
/******************************************************************/
typedef char data_node;
typedef struct NODE
{
struct NODE *l_node,*r_node;
data_node value;
} node; 

/******************************************************************/
//function:preordertraverse a bitree and printf the value in every node
//argument:pointer to the frist node of this bitree
/******************************************************************/
int preordertraversal(node *pointer)
{
if(NULL == pointer)                         //is a new bitree? if not, return.
return 0;
else
{
//print it's value before ,print it's child tree
printf("%c ",pointer->value);            //print value of this node
preordertraversal(pointer->l_node);     //enter left tree
preordertraversal(pointer->r_node);     //enter right tree
}
return 0;
}
/******************************************************************/
//function:inordertraverse a bitree and printf the value in every node
//argument:pointer to the frist node of this bitree
/******************************************************************/
int inordertraversal(node *pointer)
{
if(NULL == pointer)
return 0;
else
{
inordertraversal(pointer->l_node);     //enter left tree
//In this time ,left childtree blong this node was traversed,and we can print it's value
printf("%c ",pointer->value);            //print value of this node
inordertraversal(pointer->r_node);     //enter right tree
}
return 0;
}


/******************************************************************/
//function:postordertraverse a bitree and printf the value in every node
//argument:pointer to the frist node of this bitree
/******************************************************************/
int postordertraversal(node *pointer)
{
if(NULL == pointer)
return 0;
else
{
postordertraversal(pointer->l_node);     //enter left tree
postordertraversal(pointer->r_node);     //enter right tree
//In this time , two childtree blong this node was traversed,and we can print it's value
printf("%c ",pointer->value);            //print value of this node
}
return 0;
}
/******************************************************************/
//function:creat a new bitree
//argument:pointer to a char array contain many node and these nodes according to the preorder traversal is saved.

/******************************************************************/

//假設我們用二叉樹前序遍歷的思維,遍歷結果儲存在陣列中,將printf列印改成malloc建立節點即可 ,

//注意:在遍歷過程中如果當前節點沒有左子樹或者是右子樹就用×代替,以達到給遞迴創造返回條件的效果

node *creat_bitree(char a[])
{
static int i=0;                      //靜態儲存防止遞迴呼叫時丟失上一次呼叫後i所指的a陣列的位置
node *pointer;            
    data_node value;
if('*' == (value = a[i++]))
return NULL;
else
{
pointer = (node *)malloc(sizeof(node)); //建立該節點
pointer->value = value;                //先為該節點賦值
pointer->l_node = creat_bitree(a);           //建立該節點的左子樹
pointer->r_node = creat_bitree(a);           //建立該節點的右子樹
}
//In this time,new bitree tree was be created and now return the point to this bitree to user. or last node
return pointer;
}

相關推薦

c語言實現建立程式附帶註釋

/******************************************************************/ //樹的遞迴思想,把每個節點當作是一棵樹,以後序遍歷為例 //步驟1:訪問左子樹.2訪問右子樹3.列印當前節點的值 //在節點遍歷時如果

C語言實現的基本操作---建立、求深度、求葉子結點

#include <stdio.h> #include <stdlib.h> #include <malloc.h> typedefint ElemType;//資料型別 //定義二叉樹結構,與單鏈表相似,多了一個右孩子結點 typed

C語言實現的插入刪除

二叉樹的插入刪除://首先介紹二叉樹的插入:     //首先需要明白插入的規則:每個建好的結點p都需要從跟結點開始與根結點相比較資料域,如果根結點的資料域小於結點p,則接著將結點p與根結點的右子樹相比較,否則p將與根結點的左子樹相比較;     //繼續往下類推,一直到最後

C語言實現的結構常用操作

#include<stdio.h> #include <stdlib.h> typedef float ElemType; typedef struct S_BiTNode//定義結點型別結構體 { ElemType data;//資料域 str

C語言實現建立&

演算法思想(重點是遞迴的使用)  利用擴充套件先序遍歷序列建立二叉連結串列 採用類似先序遍歷的遞迴演算法,首先讀入當前根結點的資料,如果是'.'則將當前 樹根置為空,否則申請一個新結點,存入當前根結點的資料,分別用當前根結點的 左子域和右子域進行遞迴呼叫,建立左、右子樹. 

C語言實現的各種及求解深度

#include<stdio.h> #include<malloc.h> #define MAXSIZE 100 typedef char dataType; //二叉樹結構 typedef struct bnode{ dataType data; struct bnode *lC

c語言實現連結串列非遞迴後序

演算法思想 因為後序遍歷是先訪問左子樹,再訪問右子樹,最後訪問根節點。當用棧實現遍歷時,必須分清返回根節點時,是從左子樹返回的還是從右子樹返回的。所以使用輔助指標r指向最近已訪問的結點。當然也可以在節點中增加一個標誌域,記錄是否已被訪問。 #include<iost

c語言實現的先序,中序,後序

// new.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <MEMORY.H> #include <STRING.H&

c語言使用指標實現

使用指標實現二叉樹的定義,建立,以及前序遍歷,中序遍歷,後續遍歷。 /* 該程式實現了二叉樹的建立,以及樹的遍歷,前序遍歷,中序遍歷,後序遍歷。 */ #include <stdio.h> #include<stdlib.h> #include &

c++實現層序、前序創建,遞歸非遞歸實現

log ios cst ack ret 出棧 隊列 結點 非遞歸實現 #include <iostream> #include <cstdio> #include <stdio.h> #include <string> #i

C++類實現的構建

#include<iostream> #include<fstream> #include<string.h> using namespace std; /*

非遞迴實現c++完整程式碼

先序、中序和後序遍歷過程:遍歷過程中經過結點的路線一樣,只是訪問各結點的時機不同。 從圖中可以看到,前序遍歷在第一次遇見元素時輸出,中序遍歷在第二次遇見元素時輸出,後序遍歷在第三次遇見元素時輸出。 非遞迴演算法實現的基本思路:使用堆疊 一、前序遍歷 1、遞迴實

STL實現

nod 數據 blog new friend const turn ace lrn #include<iostream> using namespace std; template<class Type> class BSTree; templat

C語言實現各種基本運算的演算法

 包含如下函式: CreateBTree( BTNode * &b, char * str ) : 由 括號表 示 串 str 創 建二叉鏈b ; FindNode( BTNode * &b,  ElemType x ) : 返回data域 為 x的節

C語言實現中統計葉子結點的個數&度為1&度為2的結點個數

演算法思想 統計二叉樹中葉子結點的個數和度為1、度為2的結點個數,因此可以參照二叉樹三種遍歷演算法(先序、中序、後序)中的任何一種去完成,只需將訪問操作具體變為判斷是否為葉子結點和度為1、度為2的結點及統計操作即可。 #include <stdio.h> #include &

C語言實現的基本操作

       我在前面的部落格中講解了連結串列、棧和佇列,這些資料結構其實都是線性表,並且給出了詳細的實現。從今天開始,我們將要來學習樹,樹作為一種資料結構我們經常會用到,作為起步和基礎,我們先來實現二叉樹,也就是每個節點有不超過2個子節點的樹。對於樹的操作,最基本的建立、遍

c語言實現的插入、查詢、刪除、列印

目錄: 二叉樹的關鍵概念: 每個節點是一個自引用結構體,形式如下: struct TreeNode { struct TreeNode *leftPtr; /* pointer to left subtree */

c語言實現的基本操作--連結串列儲存

利用二叉連結串列儲存,並且利用遞迴的方法實現二叉樹的遍歷(前序遍歷、中序遍歷和後續遍歷)操作。 c語言具體實現程式碼如下: #include<stdio.h> #include<stdlib.h> #include<malloc.h>

c語言實現常用演算法

構造二叉樹結點結構 typedef struct BT { char data; struct BT *l_chrild; struct BT *r_chrild; }BT; 建立二叉樹 BT* Create_tree()//

資料結構之---C語言實現的順序儲存

//二叉樹的順序儲存 //這裡利用迴圈佇列儲存資料 //楊鑫 #include <stdio.h> #include <math.h> #include <stdlib.h> #include <string.h> #defi