樹存儲結構(代碼、分析、匯編)

目錄:

    • 代碼:
    • 分析:
    • 匯編:

代碼:

LinkList.h
LinkList.c
線性表

GTree.h

#ifndef _GTREE_H_
#define _GTREE_H_typedef void GTree;//定義樹類型
typedef void GTreeData;//定義節點中存放數據的類型
typedef void (GTree_Printf)(GTreeData*);//定義一個參數是一個節點中存放數據的類型指針并且無返回值的函數類型GTree* GTree_Create();//聲明創建樹函數void GTree_Destroy(GTree* tree);//聲明銷毀樹函數void GTree_Clear(GTree* tree);//聲明清空樹函數int GTree_Insert(GTree* tree, GTreeData* data, int pPos);//聲明插入節點函數GTreeData* GTree_Delete(GTree* tree, int pos);//聲明刪除節點函數GTreeData* GTree_Get(GTree* tree, int pos);//聲明獲取節點函數GTreeData* GTree_Root(GTree* tree);//聲明獲取根節點函數int GTree_Height(GTree* tree);//聲明獲取高度函數int GTree_Count(GTree* tree);//聲明獲取節點數量函數int GTree_Degree(GTree* tree);//聲明獲取度數函數void GTree_Display(GTree* tree, GTree_Printf* pFunc, int gap, char div);//聲明給每個節點數據進行操作函數#endif

GTree.c

#include <stdio.h>
#include <malloc.h>
#include "GTree.h"
#include "LinkList.h"typedef struct _tag_GTreeNode GTreeNode;//定義實際使用的樹節點類型
struct _tag_GTreeNode
{GTreeData* data;//節點內存放的數據指針GTreeNode* parent;//節點的父節點LinkList* child;//節點的子節點(一個鏈表類型)
};typedef struct _tag_TLNode TLNode;//定義樹節點與鏈表關聯的類型(用于插入到鏈表的)
struct _tag_TLNode
{LinkListNode header;GTreeNode* node;
};static void recursive_display(GTreeNode* node, GTree_Printf* pFunc, int format, int gap, char div)
{int i = 0;if( (node != NULL) && (pFunc != NULL) )//判斷節點與函數指針不為空{for(i=0; i<format; i++)//format 大于0時將輸出格式符 div{printf("%c", div);}pFunc(node->data);//執行函數(進行節點數據輸出)printf("\n");for(i=0; i<LinkList_Length(node->child); i++)//給該節點的每個子節點 執行相同操作{TLNode* trNode = (TLNode*)LinkList_Get(node->child, i);//取得子節點recursive_display(trNode->node, pFunc, format + gap, gap, div);//在每層子節點以2個格式占位符遞增}}
}static void recursive_delete(LinkList* list, GTreeNode* node)//定義遞歸刪除節點函數
{if( (list != NULL) && (node != NULL) )//判斷鏈表與樹節點不為空{GTreeNode* parent = node->parent;//取得刪除節點的父節點int index = -1;int i = 0;for(i=0; i<LinkList_Length(list); i++)//在鏈表中找出要刪除的節點{TLNode* trNode = (TLNode*)LinkList_Get(list, i);//取出鏈表中節點if( trNode->node == node )//如果該節點是要刪除的樹節點{LinkList_Delete(list, i);//將該節點從鏈表中刪除free(trNode);//將該節點的空間釋放(就是Insert函數中trNode申請的)index = i;//取得刪除的下標break;}}if( index >= 0 )//如果在鏈表中刪除了節點{  if( parent != NULL )//如果父節點不為空{for(i=0; i<LinkList_Length(parent->child); i++)//在父節點中的子鏈表中找到要刪除的節點{TLNode* trNode = (TLNode*)LinkList_Get(parent->child, i);//在父節點中的子鏈表中取出要刪除的節點if( trNode->node == node )//如果取出的節點是要刪除的節點 {LinkList_Delete(parent->child, i);//將該節點從父節點的子鏈表中刪除free(trNode);//該空間釋放(就是Insert函數中cldNode申請的)break;}}               }while( LinkList_Length(node->child) > 0 )//如果刪除節點還有子節點{TLNode* trNode = (TLNode*)LinkList_Get(node->child, 0);//取出刪除節點中的子節點recursive_delete(list, trNode->node);//將要每個子節點執行同樣操作}LinkList_Destroy(node->child);//這必須放在while后面,free(node);//釋放樹節點空間(就是Insert函數中cNode申請的)這必須放在while后面}}
}static int recursive_height(GTreeNode* node)//定義遞歸計算樹高度函數
{int ret = 0;if( node != NULL )//如果節點不為空{int subHeight = 0;int i = 0;for(i=0; i<LinkList_Length(node->child); i++){TLNode* trNode = (TLNode*)LinkList_Get(node->child, i);//取出子節點subHeight = recursive_height(trNode->node);//遞歸調用從最后一個子節點開始計算if( ret < subHeight )//如果當前層的的數量小于子層返回的數量{ret = subHeight;//將這個節點的高度設為子層數量表示這個節點下有這么多層子節點//下面ret+1。然后返回到上一個節點就相當再加上自己本身的這層}}ret = ret + 1;//加一 表示有一層子節點,只要node(傳來的子節點)不為空必須加一}return ret;
}static int recursive_degree(GTreeNode* node)//定義遞歸計算樹度數函數
{
int ret = -1;if( node != NULL )//如果節點不為空{int subDegree = 0;int i = 0;ret = LinkList_Length(node->child);//獲取該節點的子鏈表的長度for(i=0; i<LinkList_Length(node->child); i++){TLNode* trNode = (TLNode*)LinkList_Get(node->child, i);//取出子節點subDegree = recursive_degree(trNode->node);//將每個子節點執行同樣操作if( ret < subDegree )//如果子節點的數量大于當前節點的子節點數量{//將返回最大數量,就是找出子節點最多的總數ret = subDegree;}}}return ret;
}GTree* GTree_Create()//定義創建樹函數
{return LinkList_Create();//調用創建鏈表
}void GTree_Destroy(GTree* tree)//定義銷毀樹函數
{GTree_Clear(tree);LinkList_Destroy(tree);
}void GTree_Clear(GTree* tree)//定義清空樹函數
{GTree_Delete(tree, 0);//刪除根節點所有的子節點也會刪除
}int GTree_Insert(GTree* tree, GTreeData* data, int pPos)//定義插入節點函數
{LinkList* list = (LinkList*)tree;//將樹指針轉成鏈表指針int ret = (list != NULL) && (data != NULL) && (pPos < LinkList_Length(list));//判斷表不為空插入的數據不為空與pPos正常if( ret )//條件成功{TLNode* trNode = (TLNode*)malloc(sizeof(TLNode));//新建樹節點與鏈表關聯類型// 注意:如果是下面pNode==NULL 。比如插入第一個節點時,后面這個cldNode申請的空間釋放不了TLNode* cldNode = (TLNode*)malloc(sizeof(TLNode));//新建樹節點與鏈表關聯類型TLNode* pNode = (TLNode*)LinkList_Get(list, pPos);//獲取出pPos位置的節點GTreeNode* cNode = (GTreeNode*)malloc(sizeof(GTreeNode));//新建一個樹類型節點ret = (trNode != NULL) && (cldNode != NULL) && (cNode != NULL);//判斷三個申請的空間成功if( ret )//條件成功{cNode->data = data;//給新建樹節點中存放的數據賦值cNode->parent = NULL;//給新建樹節點的父節點賦空cNode->child = LinkList_Create();//給新建節點的子節點賦值(新創建一個鏈表)trNode->node = cNode;//cldNode->node = cNode;LinkList_Insert(list, (LinkListNode*)trNode, LinkList_Length(list));//將新建節點插入鏈表最后一個位置if( pNode != NULL )//如果該位置本來有節點{cNode->parent = pNode->node;//將該位置本來有的節點賦給新建樹節點的的父節點//使用樹節點與鏈表關聯類型將新建節點插入到該位置本來節點的子鏈表最后一個位置LinkList_Insert(pNode->node->child, (LinkListNode*)cldNode, LinkList_Length(pNode->node->child));}}else//如果條件不成功將申請的空間釋放{free(trNode);free(cldNode);free(cNode);}}return ret;
}GTreeData* GTree_Delete(GTree* tree, int pos)//定義刪除節點函數
{TLNode* trNode = (TLNode*)LinkList_Get(tree, pos);//從鏈表中獲取出刪除位置節點GTreeData* ret = NULL;//定義指向刪除節點中存放的數據的指針if( trNode != NULL )//如果有該位置節點{ret = trNode->node->data;//取得刪除節點存放的數據//執行遞歸刪除在總鏈表與其父節點子鏈表中移除該節點,再將該節點的子節點執行同樣操作recursive_delete(tree, trNode->node);}return ret;//返回刪除節點中存放的數據指針
}GTreeData* GTree_Get(GTree* tree, int pos)//定義獲取節點數據函數
{TLNode* trNode = (TLNode*)LinkList_Get(tree, pos);//從鏈表中獲取出節點GTreeData* ret = NULL;if( trNode != NULL ){ret = trNode->node->data;//取得節點數據指針}return ret;//返回點中存放的數據指針
}GTreeData* GTree_Root(GTree* tree)//定義獲取根節點數據函數
{return GTree_Get(tree, 0);//調用獲取節點數據函數
}int GTree_Height(GTree* tree)//定義獲取樹的高度函數
{TLNode* trNode = (TLNode*)LinkList_Get(tree, 0);//取出第一個節點(根節點)int ret = 0;if( trNode != NULL ){ret = recursive_height(trNode->node);//調用遞歸計算高度}return ret;
}int GTree_Count(GTree* tree)//定義返回樹節點總數量函數
{return LinkList_Length(tree);//返回鏈表數量
}int GTree_Degree(GTree* tree)//定義獲取樹的度數函數
{TLNode* trNode = (TLNode*)LinkList_Get(tree, 0);//取得根節點int ret = -1;if( trNode != NULL ){ret = recursive_degree(trNode->node);//調用遞歸計算度數}return ret;
}void GTree_Display(GTree* tree, GTree_Printf* pFunc, int gap, char div)//定義給每個節點數據進行操作函數
{TLNode* trNode = (TLNode*)LinkList_Get(tree, 0);//取得根節點if( (trNode != NULL) && (pFunc != NULL) )//根節點與函數指針不為空{  recursive_display(trNode->node, pFunc, 0, gap, div);//調用遞歸處理}
}

main.c

#include <stdio.h>
#include "GTree.h"
void printf_data(GTreeData* data)
{printf("%c", (int)data);
}int main(int argc, char *argv[])
{GTree* tree = GTree_Create();int i = 0;GTree_Insert(tree, (GTreeData*)'A', -1);GTree_Insert(tree, (GTreeData*)'B', 0);GTree_Insert(tree, (GTreeData*)'C', 0);GTree_Insert(tree, (GTreeData*)'D', 0);GTree_Insert(tree, (GTreeData*)'E', 1);GTree_Insert(tree, (GTreeData*)'F', 1);GTree_Insert(tree, (GTreeData*)'H', 3);GTree_Insert(tree, (GTreeData*)'I', 3);GTree_Insert(tree, (GTreeData*)'J', 3);printf("Tree Height: %d\n", GTree_Height(tree));printf("Tree Degree: %d\n", GTree_Degree(tree));printf("Full Tree:\n");GTree_Display(tree, printf_data, 2, ' ');printf("Get Tree Data:\n");for(i=0; i<GTree_Count(tree); i++){printf_data(GTree_Get(tree, i));printf("\n");}printf("Get Root Data:\n");printf_data(GTree_Root(tree));printf("\n");GTree_Delete(tree, 3);printf("After Deleting D:\n");GTree_Display(tree, printf_data, 2, '-');GTree_Clear(tree);printf("After Clearing Tree:\n");GTree_Display(tree, printf_data, 2, '.');GTree_Destroy(tree);getchar();return 0;
}

分析:

在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述

匯編:

在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/379185.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379185.shtml
英文地址,請注明出處:http://en.pswp.cn/news/379185.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Python-《twinkle twinkle little star》統計單詞出現次數

統計英文兒歌《twinkle twinkle little star》中&#xff0c;使用到的單詞及其出現次數。要求去除單詞大小寫的影響&#xff0c;不統計標點符號的個數&#xff0c;并按降序輸出。 Twinkle, twinkle, little star, How I wonder what you are! Up above the world so high, Like…

二元矩陣峰值搜索_好斗的牛(二元搜索)

二元矩陣峰值搜索A farmer has built a long barn with N stalls. The stalls are placed in a straight manner at positions from x1, x2, ...xN. But his cows (C) are aggressive and don’t want to be near other cows. To prevent cows from hurting each other, he wan…

WinForm Paenl里面添加Form

Form7 f7 new Form7();f7.TopLevel false;f7.Parent this.panel1;this.panel1.Controls.Add(f7);f7.Show();轉載于:https://www.cnblogs.com/Haibocai/archive/2012/10/30/2746003.html

跳躍表SkipList

跳躍表(Skip List)是一種隨機化數據結構&#xff0c;基于并聯的鏈表&#xff0c;其效率可比擬于二叉查找樹(對于大多數操作需要O(log n)平均時間)。 基本上&#xff0c;跳躍列表是對有序的鏈表增加上附加的前進鏈接&#xff0c;增加是以隨機化的方式進行的&#xff0c;所以在列…

Python---冒泡排序、選擇排序

冒泡排序 依次輸入n個數&#xff0c;進行冒泡排序 冒泡排序法&#xff0c;即兩個相鄰的進行比較&#xff0c;比較之后換位置 def bubbleSort(arr):n len(arr)for i in range(n):for j in range(0, n-i-1):if arr[j] > arr[j1] :arr[j], arr[j1] arr[j1], arr[j]arr[] n…

react js 添加樣式_如何在React JS Application中添加圖像?

react js 添加樣式Hello! In this article, we will learn how to add images in React JS? I remember when I just started coding in React JS, I thought adding images would be done exactly as it is in HTML. I later realized that it was different. 你好&#xff0…

二叉樹(多路平衡搜索樹)-(代碼、分析、匯編)

目錄&#xff1a;代碼&#xff1a;分析&#xff1a;匯編&#xff1a;代碼&#xff1a; BTree.h #ifndef _BTREE_H_ #define _BTREE_H_#define BT_LEFT 0 //定義左子節點標識 #define BT_RIGHT 1 //定義右子節點標識typedef void BTree;//定義樹類型 typedef unsigned long lo…

window service服務安裝錯誤

今天按照園子里面的文章&#xff0c;弄了一個系統服務&#xff0c;可是一直裝不上去&#xff0c; 正在運行事務處理安裝。 正在開始安裝的“安裝”階段。查看日志文件的內容以獲得 D:\TecCreateSvc\TecJsCreateService.exe 程序集的進度。該文件位于 D:\TecCreateSvc\TecJsCre…

DM9000調試記錄

最近在調試DM9000&#xff0c;遇到了很多問題&#xff0c;在網上幾乎也能找到同樣的問題&#xff0c;但是答案千變萬化&#xff0c;弄的我這樣不行&#xff0c;那樣也不行。 1、遇到的第一個問題&#xff0c;網卡不識別&#xff0c;出現的調試信息就是&#xff1a; dm9000 dm90…

Python---二分法查找

輸入n個數&#xff0c;通過二分法查找該數的下標 def binarySearch(arr,value):m 0#開始n len(arr#最后)while m<n:mid(mn)//2#計算中間位置if valuearr[mid]:#查找成功&#xff0c;返回元素對應的位置return midelif value>arr[mid]:#在后面一半元素中繼續查找mmid1e…

Python datetime isocalendar()方法與示例

Python datetime.isocalendar()方法 (Python datetime.isocalendar() Method) datetime.isocalendar() method is used to manipulate objects of datetime class of module datetime. datetime.isocalendar()方法用于操作模塊datetime的datetime類的對象。 It uses a dateti…

ASP.NET 技術(附翻譯)

1.構建 ASP.NET 頁面ASP.NET 和ASP.NET結構ASP.NET 是微軟.NET framework整體的一部分, 它包含一組大量的編程用的類&#xff0c;滿足各種編程需要。 在下列的二個部分中, 你如何學會 ASP.NET 很適合的放在.NET framework, 和學會能在你的 ASP.NET 頁面中使用語言。.NET類庫假想…

SQL捕獲異常

原文地址 http://technet.microsoft.com/zh-cn/office/ms179296%28vsql.100%29在 Transact-SQL 中使用 TRY...CATCHTransact-SQL 代碼中的錯誤可使用 TRY…CATCH 構造處理&#xff0c;此功能類似于 Microsoft Visual C 和 Microsoft Visual C# 語言的異常處理功能。TRY…CATCH …

二叉樹遍歷(代碼,分析,匯編)

目錄&#xff1a;代碼&#xff1a;分析&#xff1a;匯編&#xff1a;代碼&#xff1a; BTree.h BTree.c 二叉樹&#xff08;多路平衡搜索樹&#xff09; LinkQueue.h #ifndef _LINKQUEUE_H_ #define _LINKQUEUE_H_typedef void LinkQueue;//定義隊列類型LinkQueue* LinkQueu…

Java Vector insertElementAt()方法與示例

矢量類insertElementAt()方法 (Vector Class insertElementAt() method) insertElementAt() method is available in java.util package. insertElementAt()方法在java.util包中可用。 insertElementAt() method is used to set the given element (ele) at the given (indices…

Python---查找序列的最長遞增子序列

查找序列的最長遞增子序列 什么是序列的最長遞增子序列&#xff1f; 答&#xff1a;在一個數值序列中&#xff0c;找到一個子序列&#xff0c;使得這個子序列元素的數值依次遞增&#xff0c;并且這個子序列的長度盡可能地大。這就是所謂的最長遞增子序列 from itertools impo…

SendMessage和PostMessage

SendMessage 和 PostMessage 的區別 &#xff11;、首先是返回值意義的區別&#xff0c;我們先看一下 MSDN 里的聲明&#xff1a; LRESULT SendMessage( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);BOOL PostMessage( HWND hWnd…

ffmpeg-從mp4、flv、ts文件中提取264視頻流數據

ffmpeg-從mp4、flv、ts文件中提取264視頻流數據 main.c #include <stdio.h> #include <libavutil/log.h> #include <libavformat/avio.h> #include <libavformat/avformat.h>void proc(int need_to_annexb, char* in_file, char* out_file) {AVForma…

java timezone_Java TimeZone getDSTSavings()方法與示例

java timezoneTimeZone類的getDSTSavings()方法 (TimeZone Class getDSTSavings() method) getDSTSavings() method is available in java.util package. getDSTSavings()方法在java.util包中可用。 getDSTSavings() method is used to get the number of time differences in …

Photoshop 保存PNG格式交錯和不交錯有差別

1.PNG格式是由Netscape公司開發出來的格式&#xff0c;可以用于網絡圖像&#xff0c;但它不同于GIF格式圖像只能保存256色&#xff0c;PNG格式可以保存24位的真彩色圖像&#xff0c;并且支持透明背景和消除鋸齒邊緣的功能&#xff0c;可以在不失真的情況下壓縮保存圖像。但由于…