二叉樹的建立和遍歷

2021-09-12 02:05:13 字數 4340 閱讀 7964

二叉樹是每個結點最多有兩個子樹的樹結構。通常子樹被稱作「左子樹」和「右子樹」。

我們則需要掌握二叉樹的一些基本操作和應用,如二叉樹的構建、遍歷和求節點個樹。

講這些之前我們先用一棵二叉樹來講解一下什麼是前序遍歷、中序遍歷和後序遍歷。
前序遍歷指的就是:先訪問根節點,再訪問該節點的左孩子和右孩子。

中序遍歷指的就是:先訪問左孩子,再訪問根節點,最後訪問右孩子;

後序遍歷指的就是:先訪問左右孩子,最後訪問根節點。

首先我們用前序遍歷的陣列構建二叉樹:

// 通過前序遍歷的陣列"abd##e#h##cf##g##"構建二叉樹

btnode* binarytreecreate(btdatatype* a, int n, int* pi)

else

return null;

}

//前序遍歷:先訪問根節點,再訪問該節點的左孩子和右孩子

void binarytreeprevorder(btnode* root)

//中序遍歷:先訪問左孩子,再訪問根節點,最後訪問右孩子

void binarytreeinorder(btnode* root)

//後序遍歷:先訪問左右孩子,最後訪問根節點

void binarytreepostorder(btnode* root)

// 層序遍歷:按照樹的每一層(高度)進行遍歷。

void binarytreelevelorder(btnode* root)

queuedestory(&q);

}

//求節點個數

int binarytreesize(btnode* root)

//葉子節點個數

int binarytreeleafsize(btnode* root)

//第k層的個數

int binarytreelevelksize(btnode* root, int k)

//查詢x值,返回x節點

btnode* binarytreefind(btnode* root, btdatatype x)

//判斷一棵樹是否為完全二叉樹

int binarytreecomplete(btnode* root)

queuepush(&q, front->_left);

queuepush(&q, front->_right);

} while (queueempty(&q) != 0) }

return 1;

queuedestory(&q);

}

以下是筆者的源**。

btree.h

#include #include #include typedef char btdatatype;

typedef struct binarytreenode

btnode;

// 通過前序遍歷的陣列"abd##e#h##cf##g##"構建二叉樹

btnode* binarytreecreate(btdatatype* a, int n, int* pi);

void binarytreedestory(btnode* root);

int binarytreesize(btnode* root);

int binarytreeleafsize(btnode* root);

int binarytreelevelksize(btnode* root, int k);

btnode* binarytreefind(btnode* root, btdatatype x);

// 遍歷

void binarytreeprevorder(btnode* root);

void binarytreeinorder(btnode* root);

void binarytreepostorder(btnode* root);

// 層序遍歷

void binarytreelevelorder(btnode* root);

void testtree();

btree.c

#include "btree.h"

#include "queue.h"

#include "stack.h"

// 通過前序遍歷的陣列"abd##e#h##cf##g##"構建二叉樹

btnode* binarytreecreate(btdatatype* a, int n, int* pi)

else

return null;

}//銷毀

void binarytreedestory(btnode* root)

root = null;

}//結點個數

int binarytreesize(btnode* root)

//葉子結點個數

int binarytreeleafsize(btnode* root)

//第k層的個數

int binarytreelevelksize(btnode* root, int k)

//查詢x值,返回x結點

btnode* binarytreefind(btnode* root, btdatatype x)

//前序遍歷

void binarytreeprevorder(btnode* root)

//中序遍歷

void binarytreeinorder(btnode* root)

//後序遍歷

void binarytreepostorder(btnode* root)

// 層序遍歷

void binarytreelevelorder(btnode* root)

queuedestory(&q);

}//判斷一棵樹是否為完全二叉樹

int binarytreecomplete(btnode* root)

queuepush(&q, front->_left);

queuepush(&q, front->_right);

} while (queueempty(&q) != 0) }

return 1;

queuedestory(&q);

}

queue.h

#include #include #include #include #include "btree.h"

typedef btnode* elemtype;//定義資料型別

typedef struct sqqueue

sqqueue;

typedef struct queue

queue;

void queueinit(queue *q);

void queuedestory(queue *q);

int queueempty(queue *q);

void queuepush(queue *q, elemtype x);

void queuepop(queue *q);

elemtype queuefront(queue *q);

queue.c

#include "queue.h"

//初始化

void queueinit(queue *q)

//銷毀

void queuedestory(queue *q)

q->_front = q->_tail = null;

}//判斷佇列是否為空,0為空,1不為空

int queueempty(queue *q)

//入隊

void queuepush(queue *q, elemtype x)

else }

//出隊

void queuepop(queue *q)

//出隊頭元素

elemtype queuefront(queue *q)

列印//void queueprint(queue *q)

//// printf("\n");

//}

test.c

#include "btree.h"

#include "queue.h"

void testtree()

int main()

二叉樹建立和遍歷

二叉樹建立遍歷規則 1.先序 根 左 右 2.中序 左 根 右 3.後序 左 右 根 二叉樹定義和輔助函式如下 struct node void visit int data int indata 先序建立二叉樹 struct node createbitree 先序建立乙個二叉樹 return t...

二叉樹建立和遍歷

include include 帶返回值建立二叉樹 最簡單方法 節點資料結構 struct bs node typedef struct bs node tree tree head,p,root 建立二元查詢樹 有返回值的可以不傳參 沒有的話如何傳參 輸入0代表到了某個葉子節點 tree crea...

二叉樹建立和遍歷

二叉樹建立遍歷規則 1.先序 根 左 右 2.中序 左 根 右 3.後序 左 右 根 二叉樹定義和輔助函式例如以下 struct node void visit int data int indata 先序建立二叉樹 struct node createbitree 先序建立乙個二叉樹 return...