鏈式二叉樹的實現以及常用操作(C語言) 超詳細!

2021-10-08 19:43:34 字數 2046 閱讀 3479

#include

#include

typedef

struct bin_node//結點

node;

//取小名

int cur;

//現在指向的結點

char str[

1000];

//陣列存放遍歷字串

node*

create_tree()

//建立樹

node* root =

(node*

)malloc

(sizeof

(node));

//不是空結點的話,給結點分配空間

root -> data = str[cur]

;//資料域賦值

root -> left =

create_tree()

;//遞迴 左子樹相同方法建立樹

root -> right =

create_tree()

;//遞迴 右子樹相同方法建立樹

return root;

}void

preorder_ergodic

(node* cur_node)

//前序遍歷樹 傳入該結點的位址

void

midorder_ergodic

(node* cur_node)

//中序遍歷樹

void

lateorder_ergodic

(node* cur_node)

//後序遍歷樹

intlen_tree

(node* tree)

intnum_node

(node* tree)

//求樹的結點數n

intnum_zero_node

(node* tree)

//求樹的葉子結點/度為0的數目n0

intnum_one_node

(node* tree)

//求度為1的結點數n1

intnum_two_node

(node* tree)

//求度為2的結點數n2

intmain()

cur =-1

; node* tree =

create_tree()

;printf

("pre_oder:");

preorder_ergodic

(tree)

;puts(""

);printf

("mis_oder:");

midorder_ergodic

(tree)

;puts(""

);printf

("late_oder:");

lateorder_ergodic

(tree)

;puts(""

);printf

("the degree of depth is:%d.\n"

,len_tree

(tree));

printf

("the number of the nodes is:%d.\n"

,num_node

(tree));

printf

("the number of the zero_nodes is:%d.\n"

,num_zero_node

(tree));

printf

("the number of the one_nodes is:%d.\n"

,num_one_node

(tree));

printf

("the number of the two_nodes is:%d.\n"

,num_two_node

(tree));

return0;

}//示例:abc##de#g##f###

執行結果:

學習java 二叉樹的鏈式實現以及三種遍歷方式

二叉樹的鍊錶結構 public class bintree public treenode object data 定義根結點 private treenode root public bintree public bintree t data 新增結點 isleft是否新增在parent的左結點 ...

二叉樹鏈式儲存的C實現

在實現二叉樹的鏈式儲存的過程中,我遇到了一些問題,感到對遞迴的理解還不夠深入。另外,中有一處必須使用全域性變數做陣列索引,還在研究其中的原因,已完成,現在貼在部落格中供參考 include include include include define maxsize 100 define ok 1 ...

鏈式二叉樹 操作總結

二叉樹中反覆使用遞迴思想,掌握尋找子問題的能力就能解決所有二叉樹問題。非遞迴先序遍歷需要用到資料結構棧,下文提供兩種思路解決。層序遍歷用到佇列,也即廣度遍歷思想。完整 github 構建二叉樹的結點 pbtnode buybintreenode btdatatype data 建立二叉樹 void ...