二叉樹的存儲(chǔ)結(jié)構(gòu)主要有三種:順序存儲(chǔ)結(jié)構(gòu),鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu)和仿真指針存儲(chǔ)結(jié)構(gòu)。本文采用的是二叉樹的鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu)。
二叉樹的鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu)就是用指針建立二叉樹中結(jié)點(diǎn)之間的關(guān)系。二叉樹最常用的鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu)是二叉鏈。每個(gè)結(jié)點(diǎn)包括三個(gè)域:數(shù)據(jù)域data、左子節(jié)點(diǎn)指針域left和右子節(jié)點(diǎn)指針域right.
二叉樹的結(jié)構(gòu)體定義如下(我們假設(shè)int為初始類型):
typedef int Type;
// 定義二叉樹
typedef struct Node {
Type data;
struct Node* left;
struct Node* right;
}BiTreeNode;
初始化
// 初始化二叉樹
void Initiate(BiTreeNode** root, Type x) {
*root = (BiTreeNode*)malloc(sizeof(BiTreeNode));
(*root)->data = x;
(*root)->left = nullptr;
(*root)->right = nullptr;
}
左插入結(jié)點(diǎn)
// 若當(dāng)前結(jié)點(diǎn)curr非空,則再curr的左子樹插入元素值為x的新結(jié)點(diǎn)
// 原curr所指結(jié)點(diǎn)的左子樹成為新插入結(jié)點(diǎn)的左子樹
// 若插入成功,則返回新插入結(jié)點(diǎn)的指針,否則返回空指針
BiTreeNode* InsertLeftNode(BiTreeNode* curr, Type x) {
if (curr == nullptr) return nullptr;
BiTreeNode *s, *t;
s = curr->left;
t = (BiTreeNode*)malloc(sizeof(BiTreeNode));
t->data = x;
curr->left = t;
t->left = s;
t->right = nullptr;
return t;
}
右插入結(jié)點(diǎn)
BiTreeNode* InsertRightNode(BiTreeNode* curr, Type x) {
if (curr == nullptr) return nullptr;
BiTreeNode* t;
t = (BiTreeNode*)malloc(sizeof(BiTreeNode));
t->data = x;
t->right = curr->right;
t->left = nullptr;
curr->right = t;
return t;
}
// 銷毀這個(gè)結(jié)點(diǎn)及其所有子樹
void Destroy(BiTreeNode** root) {
if (*root != nullptr && (*root)->left != nullptr)
Destroy(&(*root)->left);
if (*root != nullptr && (*root)->right != nullptr)
Destroy(&(*root)->right);
free(*root);
}
刪除所指結(jié)點(diǎn)的左子樹
void DeleteLeftTree(BiTreeNode* curr) {
if (curr == nullptr || curr->left == nullptr) return;
Destroy(&curr->left);
curr->left = nullptr;
}
刪除所指結(jié)點(diǎn)的右子樹
void DeleteRightTree(BiTreeNode* curr) {
if (curr == nullptr || curr->right == nullptr) return;
Destroy(&curr->right);
curr->right = nullptr;
}
打印二叉樹
void PrintBiTree(BiTreeNode* root, int n) {
// 逆時(shí)針旋轉(zhuǎn)90°打印二叉樹root,n為縮進(jìn)層數(shù),初始值為0
if (root == nullptr) return;
PrintBiTree(root->right, n+1);
// visit root
for (int i = 0; i < n-1; i++) printf(" ");
if (n > 0) {
printf("---");
printf("%d\n", root->data);
}
PrintBiTree(root->left, n+1);
}
前序遍歷二叉樹并按訪問(wèn)順序?qū)⑵湓卮蛴〕鰜?lái)
void PreOrder(BiTreeNode* root) {
if (root != nullptr) {
printf("%d ", root->data);
PreOrder(root->left);
PreOrder(root->right);
}
}
中序遍歷二叉樹
void InOrder(BiTreeNode* root) {
if (root != nullptr) {
InOrder(root->left);
printf("%d ", root->data);
InOrder(root->right);
}
}
后序遍歷二叉樹
void PostOrder(BiTreeNode* root) {
if (root != nullptr) {
PostOrder(root->left);
PostOrder(root->right);
printf("%d ", root->data);
}
}
查找指定元素
BiTreeNode* Search(BiTreeNode* root, Type x) {
// 找到返回該節(jié)點(diǎn)指針,未找到返回空
BiTreeNode* find = nullptr;
if (root != nullptr) {
if (root->data == x)
find = root;
else {
find = Search(root->left, x);
if (find == nullptr)
Search(root->right, x);
}
}
return find;
}
本站文章版權(quán)歸原作者及原出處所有 。內(nèi)容為作者個(gè)人觀點(diǎn), 并不代表本站贊同其觀點(diǎn)和對(duì)其真實(shí)性負(fù)責(zé),本站只提供參考并不構(gòu)成任何投資及應(yīng)用建議。本站是一個(gè)個(gè)人學(xué)習(xí)交流的平臺(tái),網(wǎng)站上部分文章為轉(zhuǎn)載,并不用于任何商業(yè)目的,我們已經(jīng)盡可能的對(duì)作者和來(lái)源進(jìn)行了通告,但是能力有限或疏忽,造成漏登,請(qǐng)及時(shí)聯(lián)系我們,我們將根據(jù)著作權(quán)人的要求,立即更正或者刪除有關(guān)內(nèi)容。本站擁有對(duì)此聲明的最終解釋權(quán)。