資料結構 十

2021-09-03 08:15:52 字數 3481 閱讀 5985

二叉樹

btree.h檔案

#ifndef  _btree_h_

#define _btree_h_

typedef enumbool;

typedef enummountway;

typedef char btreedata;

//二叉樹結點

typedef struct btreenode

btreenode;

//二叉樹

typedef struct btree

btree;

//建立二叉樹

btree *btree_create();

typedef void (*pfunc)(btreenode *node,int level);

//插入資料

/*btree 要插入的二叉樹

data 要插入的資料

path 插入位置的路徑

steps 從根節點開始走的步數

mw 原來的結點掛載到新節點後的方式

*/bool btree_insert(btree *btree,btreedata data,int path,int steps, mountway mw);

//刪除

bool btree_delete(btree *btree,int path,int steps);

//獲取結點總數

int btree_count(btree *btree);

//獲取樹的高度

int btree_height(btree *btree);

//獲取樹的度

int btree_degree(btree *btree);

//列印函式

void btree_display(btree *btree,pfunc callback);

//銷毀樹

void btree_destroy(btree *btree);

#endif//_btree_h_

btree.c檔案

#include "btree.h"

#include #include btree *btree_create()

bool btree_insert(btree *btree,btreedata data,int path,int steps, mountway mw)

else

steps --;

} if(left == mw)

else

if(parent != null)

else

}else

return true;

}//刪除資料

void r_delete(btreenode *node)

bool btree_delete(btree *btree,int path,int steps)

else

steps --;

} if(parent != null)

else

}else

return true;

}void r_dispaly(btreenode *node,int level,pfunc callback)

}void btree_display(btree *btree,pfunc callback)

int r_count(btreenode *node)

int btree_count(btree *btree)

int r_hegiht(btreenode *node)

int btree_height(btree *btree)

int r_degree(btreenode *node)

void btree_destroy(btree *btree)

main.c檔案

#include #include "btree.h"

void myprint(btreenode *node,int level)

else

}//前序遍歷

void pre_order(btreenode *node)

//中序遍歷

void mid_order(btreenode *node)

//後序遍歷

void last_order(btreenode *node)

int main()

btree_insert(btree, 'a',0,0,left);

btree_insert(btree, 'b',0,1,left);

btree_insert(btree, 'c',1,1,left);

btree_insert(btree, 'd',0,2,left);

btree_insert(btree, 'e',2,2,left);

btree_insert(btree, 'f',0,3,left);

btree_insert(btree, 'g',4,3,left);

btree_insert(btree, 'h',1,2,left);

btree_insert(btree, 'i',1,3,left);

btree_insert(btree, 'j',5,3,left);

btree_display(btree,myprint);

printf("結點總數:%d\n",btree_count(btree));

printf("樹的高度:%d\n",btree_height(btree));

printf("樹的度:%d\n",btree_degree(btree));

printf("前序遍歷\n");

pre_order(btree->root);

printf("\n");

printf("中序遍歷\n");

mid_order(btree->root);

printf("\n");

printf("後序遍歷\n");

last_order(btree->root);

printf("\n");

printf("**********************\n");

btree_delete(btree,1,3);

btree_delete(btree,1,1);

btree_display(btree,myprint);

printf("結點總數:%d\n",btree_count(btree));

printf("樹的高度:%d\n",btree_height(btree));

printf("樹的度:%d\n",btree_degree(btree));

printf("**********************\n");

btree_destroy(btree);

btree = null;

printf("樹以銷毀\n");

return 0;

}

十大資料結構

演算法 資料結構 程式設計 陣列是最簡單 也是使用最廣泛的資料結構。棧 佇列等其他資料結構均由陣列演變而來。下圖是乙個包含元素 1,2,3和4 的簡單陣列,陣列長度為4。每個資料元素都關聯乙個正數值,我們稱之為索引,它表明陣列中每個元素所在的位置。大部分語言將初始索引定義為零。以下是陣列的兩種型別 ...

資料結構(十) 排序

1 順序表 pragma once includeconst int maxsize 20 typedef int datatype typedef struct datalist void swap datalist l,int a,int b void createlist datalist l...

資料結構演算法爬坑日記十

雜湊表 也叫雜湊表,通過將關鍵碼值對映到表中的乙個位置來訪問,加快查詢的速度,對映函式也叫雜湊函式,存放記錄的陣列叫雜湊表,在對一些不常改變的資料進行操作的時候,為了加快速度,通常會採用快取技術將其載入 到記憶體中進行操作,而不直接對資料庫進行操作,如redis,雜湊表就相當於乙個簡化版redis,...