數據結構之二叉樹的一些基本操作

二叉樹是樹的特殊一種,具有如下特點:1、每個結點最多有兩顆子樹,結點的度最大為2。2、左子樹和右子樹是有順序的,次序不能顛倒。3、即使某結點只有一個子樹,也要區分左右子樹。
頭文件 BTree.h

#ifndef __BTREE_H__
#define __BTREE_H__#define BLEFT  0                 // 表示插入二叉樹的左邊
#define BRIGHT 1                 // 表示插入二叉樹的右邊#define TRUE   1
#define FALSE  0typedef char BTreeData;
// 二叉樹的結點
typedef struct _btreeNode
{BTreeData data;struct _btreeNode* lchild;   // 指向左孩子結點的指針struct _btreeNode* rchild;   // 指向右孩子結點的指針
}BTreeNode;
// 二叉樹
typedef struct _btree
{BTreeNode *root;             // 指向二叉樹的根節點int  count;                  // 記錄二叉樹結點的個數
}BTree;
typedef void(*Print_BTree)(BTreeNode*);// 創建一棵二叉樹
BTree* Create_BTree();// pos 走的路徑 值類似 110(左右右)  011 (右右左)
// count 代表走的步數
// flag  代表被替換的結點應該插入在新節點的位置,如果是BLEFT 表示插在左邊,BRIGHT表示插在右邊
int Btree_Insert (BTree* tree, BTreeData data, int pos, int count, int flag);// 打印二叉樹
void Display (BTree* tree, Print_BTree pfunc);// 刪除pos處的結點
int Delete   (BTree* tree, int pos, int count);// 求樹的高度
int BTree_Height  (BTree* tree);// 求樹的度
int BTree_Degree  (BTree* tree);// 清除樹
int BTree_Clear   (BTree* tree);// 銷毀樹
int BTree_Destroy (BTree** tree);// 打印
void printA (BTreeNode* node);// 前序遍歷
void pre_order  (BTreeNode* node);// 中序遍歷
void mid_order  (BTreeNode* node);// 后序遍歷
void last_order (BTreeNode* node);#endif // __BTREE_H__

源文件 BTree.c

#include "BTree.h"
#include <stdlib.h>
#include <stdio.h>BTree *Create_BTree()
{BTree* btree = (BTree*) malloc(sizeof(BTree)/sizeof(char));if (NULL == btree){return NULL;}btree->count = 0;btree->root  = NULL;return btree;
}int Btree_Insert (BTree* tree, BTreeData data, int pos, int count, int flag)
{if (NULL == tree || (flag != BLEFT && flag != BRIGHT)){return FALSE;}BTreeNode* node = (BTreeNode*) malloc(sizeof(BTreeNode)/sizeof(char));if (NULL == node){return FALSE;}node->data   = data;node->lchild = NULL;node->rchild = NULL;// 找插入的位置BTreeNode *parent  = NULL;BTreeNode *current = tree->root;     // current 一開始指向根節點,根節點的父節點是空int way;                             // 保存當前走的位置while (count > 0 && current != NULL){way = pos &  1;                  // 取出當前走的方向pos = pos >> 1;                  // 移去走過的路線// 因為當前位置就是走完以后的位置的父節點parent = current;if (way == BLEFT)   // 往左走{current = current->lchild;}else{current = current->rchild;}count --;}// 把被替換掉的結點插入到新節點下面if (flag == BLEFT){node->lchild = current;}else{node->rchild = current;}// 把新節點插入到二叉樹中,way保存了應該插入在父節點的左邊還是右邊if (NULL != parent){if (way == BLEFT){parent->lchild = node;}else{parent->rchild = node;}}else{tree->root = node;  // 替換根節點}tree->count++;return TRUE;
}void r_display (BTreeNode* node, Print_BTree pfunc, int gap)
{int i;if (node == NULL){for (i = 0; i < gap; i++){printf ("-");}printf ("\n");return;}for (i = 0; i < gap; i++){printf ("-");}// 打印結點// printf ("%c\n", node->data);pfunc (node);if (NULL != node->lchild || NULL != node->rchild){// 打印左孩子r_display (node->lchild, pfunc, gap+4);// 打印右孩子r_display (node->rchild, pfunc, gap+4);}
}void Display (BTree* tree, Print_BTree pfunc)
{if (tree == NULL){return;}r_display (tree->root, pfunc, 0);
}void r_delete (BTree* tree, BTreeNode* node)
{if (NULL == node || NULL == tree){return;}// 先刪除左孩子r_delete (tree, node->lchild);// 刪除右孩子r_delete (tree, node->rchild);free (node);tree->count--;
}int Delete (BTree* tree, int pos, int count)
{if (NULL == tree)return FALSE;// 找結點BTreeNode* parent  = NULL;BTreeNode* current = tree->root;int way;while (count > 0 && NULL != current){way = pos &  1;pos = pos >> 1;parent = current;if (way == BLEFT){current = current->lchild;}else{current = current->rchild;}       count--;}if (NULL != parent){if (way == BLEFT){parent->lchild = NULL;}else{parent->rchild = NULL;}}else{tree->root = NULL;}// 釋放結點r_delete (tree, current);return TRUE;
}int r_height (BTreeNode* node)
{if (NULL == node){return 0;}int lh = r_height (node->lchild);int rh = r_height (node->rchild);return (lh > rh ? lh+1 : rh+1);
}int BTree_Height (BTree* tree)
{if (NULL == tree){return FALSE;}int ret = r_height (tree->root);return ret;
}int r_degree (BTreeNode* node)
{if (NULL == node){return 0;}int degree = 0;if (NULL != node->lchild){degree++;}if (NULL != node->rchild){degree++;}if (1 == degree){int ld = r_degree (node->lchild);if (2 == ld){return 2;}int rd = r_degree (node->rchild);if (2 == rd){return 2;}}return degree;
}int BTree_Degree (BTree* tree)
{if (NULL == tree){return FALSE;}int ret = r_degree (tree->root);return ret;
}int BTree_Clear (BTree* tree)
{if (NULL == tree){return FALSE;}Delete (tree, 0, 0);  // 刪除根節點tree->root = NULL;return TRUE;
}int BTree_Destroy (BTree** tree)
{if (NULL == tree){return FALSE;}BTree_Clear (*tree);free (*tree);*tree = NULL;return TRUE;
}void pre_order  (BTreeNode* node)
{if (NULL == node){return;}printf    ("%4c", node->data);pre_order (node->lchild);pre_order (node->rchild);
}void mid_order  (BTreeNode* node)
{if (NULL == node){return;}mid_order (node->lchild);printf    ("%4c", node->data);mid_order (node->rchild);
}void last_order (BTreeNode* node)
{if (NULL == node){return;}last_order (node->lchild);  last_order (node->rchild);printf     ("%4c", node->data);
}void printA (BTreeNode* node)
{printf ("%c\n", node->data);
}

主函數 main.c

#include "BTree.h"
#include <stdio.h>int main()
{BTree* btree = Create_BTree();if (NULL == btree){printf ("創建失敗\n");}else{printf ("創建成功\n");}Btree_Insert (btree, 'A', 0, 0, 0);Btree_Insert (btree, 'B', 0, 1, 0);Btree_Insert (btree, 'C', 1, 1, 0);Btree_Insert (btree, 'D', 0, 2, 0);Btree_Insert (btree, 'E', 2, 2, 0);Btree_Insert (btree, 'F', 0, 3, 0);Btree_Insert (btree, 'G', 4, 3, 0);Btree_Insert (btree, 'H', 3, 2, 0);Display (btree, printA);printf ("前序遍歷:\n");pre_order (btree->root);printf ("\n");printf ("中序遍歷:\n");mid_order (btree->root);printf ("\n");printf ("后序遍歷:\n");last_order (btree->root);printf ("\n");#if 0Delete (btree, 0, 1);printf ("刪除后--------------\n");Display (btree, printA);printf ("高度: %d\n", BTree_Height (btree));printf ("度 :  %d\n", BTree_Degree (btree));printf ("清空后--------------\n");BTree_Clear (btree);Display (btree, printA);BTree_Destroy (&btree);//btree = NULL;
#endif  return 0;
}

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

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

相關文章

【Arduino】使用C#實現Arduino與電腦進行串行通訊

在給Arduino編程的時候&#xff0c;因為沒有調試工具&#xff0c;經常要通過使用串口通訊的方式調用Serial.print和Serial.println輸出Arduino運行過程中的相關信息&#xff0c;然后在電腦上用Arduino IDE的Serial Monitor來查看print出來的信息。Serial Monitor不僅可以接受Ar…

虛擬機NAT模式聯網

阿里開源鏡像軟件&#xff1a;https://opsx.alibaba.com/mirror 如何使VMware ip與本機ip處于同一網段 https://blog.csdn.net/kakuma_chen/article/details/71425620 轉載于:https://www.cnblogs.com/cdy0626/p/11131440.html

VS2008下最新X264(svn 2009.9)編譯不過的解決辦法

總有人說最新的版本 編譯不過&#xff0c;搞的群、 論壇里到處都是這種求助貼。建議斑竹把這個解決辦法放到醒目的位置&#xff0c;以減少噪音。科普開始1、編譯問題由于MS的VS編譯器對C99標準支持不好&#xff0c;不支持函數當中混合定義、聲明變量。解決辦法&#xff1a;在函…

node、npm、vue安裝 -- VUE 項目 demo 實例

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 1. 安裝node&#xff1a; sudo yum install epel-release sudo yum install nodejs node --version // 安裝好后查看版本2. 安裝 npm …

用C語言實現簡單的停車場管理

這個程序是利用棧和循環隊列實現的&#xff0c;自己得先處理好邏輯關系就好了。由于題目沒有要求&#xff0c;這個程序就沒加重復判斷&#xff0c;比如一輛車已經停在車位上或者便道上&#xff0c;再來一輛就判斷不了了。關于棧&#xff0c;就是先進后出的思想&#xff0c;隊列…

推薦一個配置linux服務的網站

該網站的各種linux服務的配置都是基于CentOS系統的 基本上各種linux服務都有了 http://www.server-world.info/en/轉載于:https://www.cnblogs.com/Skyar/p/3582389.html

mariadb數據庫增刪改查

1.常用數據類型 1&#xff09;整數:int, bit 2&#xff09;小數:decimal    #decimal(5,2)表示共有五位數&#xff0c;保留兩位小數 3&#xff09;字符串:varchar, char   4&#xff09;日期時間:date, time, datetime 5&#xff09;枚舉類型(enu…

為什么你工作努力卻沒有起色?

成為職場達人&#xff0c;未必要經常挑燈夜戰。相反&#xff0c;注意到下面幾條&#xff0c;會讓你少走彎路。 1&#xff09;成長的機會永遠比眼前的待遇重要——做重要的事比多拿錢重要。 我知道在水木bbs上的worklife版本&#xff0c;每天都在上演的就是比較自己的第一個o…

《 Spring 實戰 》(第4版) 讀書筆記 (未完結,更新中...)

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 Pxx 表示在書的第 xx 頁。 Spring 框架的核心是 Spring 容器。 1. (P7.) 構造器注入是依賴注入的方式之一。 緊耦合&#xff1a;在 …

數據結構排序法之希爾排序法(Shell Sort)

希爾排序&#xff0c;也叫遞減增量排序&#xff0c;是插入排序的一種更高效的改進版本。希爾排序是不穩定的排序算法。 希爾排序是基于插入排序的以下兩點性質而提出改進方法的&#xff1a; 1、插入排序在對幾乎已經排好序的數據操作時&#xff0c;效率高&#xff0c;即可以達…

Windows To Ghost系統封裝之必備軟件集 - 好壓

好壓壓縮軟件&#xff08;HaoZip&#xff09;是強大的壓縮文件管理器&#xff0c;是完全免費的新一代壓縮軟件&#xff0c;相比其它壓縮軟件系統資源占用更少&#xff0c;有更好的兼容性&#xff0c;壓縮率比較高。 它提供了對ZIP、7Z和TAR文件的完整支持&#xff0c;能解壓RAR…

js 彈窗并定時關閉

1. $(input).click(function() {prompt(點擊成功, 2000) })function prompt(newName, time, fn) {var $div $(<div></div>);$div.css({position: fixed,top: 0,left: 0,width: 100%,height: 100%,z-index: 200,background-color: rgba(0,0,0,0.4),// background-c…

數據結構排序法之插入法

插入排序是一種簡單直觀的排序算法。它的工作原理非常類似于我們抓撲克牌。 對于未排序數據(右手抓到的牌)&#xff0c;在已排序序列(左手已經排好序的手牌)中從后向前掃描&#xff0c;找到相應位置并插入。 插入排序在實現上&#xff0c;通常采用in-place排序&#xff08;即…

XSLT學習筆記

1. 樣式聲明&#xff1a;<xsl:stylesheet>或<xsl:transform> 2. XSLT常用元素&#xff1a; 2.1 <xsl:template>&#xff1a;創建模板 Match屬性的作用是使模板和XML元素相關聯 e.g.:<xsl:template match"\">......</xsl:template&g…

職場:人生從沒有最佳時機!一個離職客服人員的領悟

每個人都有感到失落迷惘的時候。 人生用專制又霸道的方式運行著&#xff0c;每當我們心想一切塵埃落定、生活穩固的時候&#xff0c;生活總愛給我們驚喜&#xff0c;粉碎我們短暫的安逸&#xff0c;讓我們不得不重新思考。 「我走對路了嗎?」 「我能夠賺更多錢、爬到更高的地位…

VS Code 的常用快捷鍵

VS Code 的常用快捷鍵和插件 一、vs code 的常用快捷鍵 前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 1、注釋&#xff1a; a) 單行注釋&#xff1a;[ctrlk,ctrlc] 或 ctrl/ b) 取消…

vue-axios interceptors

import axios from axios import cookie from js-cookie const options {baseURL: window.location.protocol process.env.BASE_API,headers: {},timeout: 20000 } const fetch axios.create(options)// request攔截器 fetch.interceptors.request.use(config > {if (coo…

數據結構排序法之雞尾酒排序法he快速排序法

雞尾酒排序&#xff0c;也叫定向冒泡排序&#xff0c;是冒泡排序的一種改進。此算法與冒泡排序的不同處在于從低到高然后從高到低&#xff0c;而冒泡排序則僅從低到高去比較序列里的每個元素。他可以得到比冒泡排序稍微好一點的效能。 // 兩兩互換 void swap (int* a, int i, …

VSCode 多開、環境對比

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 多開&#xff1a; 第一種&#xff1a;win10的開始菜單&#xff0c;在vscode圖標右鍵選擇“新開窗口”&#xff0c;這樣就多了一個vscode…

前言_工作兩年自我感觸

17年大學畢業&#xff0c;到今天整整工作兩年&#xff0c;從前端到數據分析&#xff0c;從上家公司&#xff08;簡稱A&#xff09;到現公司&#xff0c;想趁著今天是參加工作兩年的紀念日&#xff0c;回憶過往&#xff0c;結合現狀有感而發。 剛畢業的時候&#xff0c;啥都學&a…