当前位置:   article > 正文

二叉树(详解)

二叉树

目录

二叉树

一、二叉树概念及结构
    1.概念
    2.数据结构中的二叉树
    3.特殊的二叉树
    4.二叉树的存储结构
        4.1顺序存储
        4.2链式存储
    5.二叉树的性质
二、二叉树顺序结构及概念
    1.二叉树的顺序结构
    2.堆的概念及结构
    3.堆的实现
三、二叉树链式结构及实现
    1.二叉树链式结构的遍历
    2.二叉树的链式实现



 

 一、 二叉树概念及结构

1.概念

一棵二叉树是结点的一个有限集合,该集合或者为空,或者是由一个根节点加上两棵别称为左子树和右子树的二叉树组成。
二叉树的特点

  1. 每个结点最多有两棵子树,即二叉树不存在度大于2的结点。
  2. 二叉树的子树有左右之分,其子树的次序不能颠倒。

2.数据结构中的二叉树 

 3.特殊的二叉树

a.满二叉树:一个二叉树,如果每一个层的结点数都达到最大值,则这个二叉树就是满二叉树。也就是说,如果一个二叉树的层数为K,且结点总数是(2^k) -1 ,则它就是满二叉树。


b.完全二叉树:完全二叉树是效率很高的数据结构,完全二叉树是由满二叉树而引出来的。对于深度为K的,有n个结点的二叉树,当且仅当其每一个结点都与深度为K的满二叉树中编号从1至n的结点一一对应时称之为完全二叉树。 要注意的是满二叉树是一种特殊的完全二叉树。

4.二叉树的存储结构

4.1顺序存储

顺序结构存储就是使用数组来存储,一般使用数组只适合表示完全二叉树,因为不是完全二叉树会有空间的浪费。而现实中使用中只有堆才会使用数组来存储。二叉树顺序存储在物理上是一个数组,在逻辑上是一颗二叉树。

 4.2链式存储

二叉树的链式存储结构是指:用链表来表示一棵二叉树,即用链来指示元素的逻辑关系。 通常的方法是链表中每个结点由三个域组成,数据域和左右指针域,左右指针分别用来给出该结点左孩子和右孩子所在的链结点的存储地址 。链式结构又分为二叉链和三叉链.

  1. // 二叉链
  2. struct BinaryTreeNode
  3. {
  4. struct BinTreeNode* _pLeft; // 指向当前节点左孩子
  5. struct BinTreeNode* _pRight; // 指向当前节点右孩子
  6. BTDataType _data; // 当前节点值域
  7. }
  8. // 三叉链
  9. struct BinaryTreeNode
  10. {
  11. struct BinTreeNode* _pParent; // 指向当前节点的双亲
  12. struct BinTreeNode* _pLeft; // 指向当前节点左孩子
  13. struct BinTreeNode* _pRight; // 指向当前节点右孩子
  14. BTDataType _data; // 当前节点值域
  15. };

  5.二叉树的性质

a.若规定根节点的层数为1,则一棵非空二叉树的第i层上最多有2^(i-1) 个结点.

b.若规定根节点的层数为1,则深度为h的二叉树的最大结点数是2^h- 1.

c.对任何一棵二叉树, 如果度为0其叶结点个数为 n0, 度为2的分支结点个数为 n2,则有n0=n2+1

d.若规定根节点的层数为1,具有n个结点的满二叉树的深度,h=Log2(n+1). (ps:Log2(n+1)是log以2为
底,n+1为对数)

e.对于具有n个结点的完全二叉树,如果按照从上至下从左至右的数组顺序对所有节点从0开始编号,则对于序号为i的结点有:

  (1) 若i>0,i位置节点的双亲序号:(i-1)/2;i=0,i为根节点编号,无双亲节点

(2) 若2i+1<n,左孩子序号:2i+1,2i+1>=n否则无左孩子

(3) 若2i+2<n,右孩子序号:2i+2,2i+2>=n否则无右孩子

二、二叉树顺序结构及概念

1.二叉树的顺序结构

普通的二叉树是不适合用数组来存储的,因为可能会存在大量的空间浪费。而完全二叉树更适合使用顺序结构存储。现实中我们通常把堆(一种二叉树)使用顺序结构的数组来存储,需要注意的是这里的堆和操作系统虚拟进程地址空间中的堆是两回事,一个是数据结构,一个是操作系统中管理内存的一块区域分段。

2.堆的概念及结构

如果有一个关键码的集合K = {k0,k1, k2,…,kn-1},把它的所有元素按完全二叉树的顺序存储方式存储在一个一维数组中,并满足:Ki <= K2i+1 且 Ki<= K2i+2 (Ki >= K2i+1 且 Ki >= K2i+2) i = 0,1,2…,则称为小堆(或大堆)。将根节点最大的堆叫做最大堆或大根堆,根节点最小的堆叫做最小堆或小根堆。

注意:

  • 堆中某个节点的值总是不大于或不小于其父节点的值;
  • 堆总是一棵完全二叉树。

3.堆的实现

  1. typedef int HPDataType;
  2. typedef struct Heap
  3. {
  4. HPDataType* _a;
  5. int _size;
  6. int _capacity;
  7. }Heap;
  8. void swap(int *a, int *b);
  9. void AdjustDown(int *a, int parent, int n);
  10. void AdjustUp(int *a, int child, int n);
  11. // 堆的构建
  12. void HeapCreate(Heap* hp, HPDataType* a, int n);
  13. // 堆的销毁
  14. void HeapDestory(Heap* hp);
  15. // 堆的插入
  16. void HeapPush(Heap* hp, HPDataType x);
  17. // 堆的删除
  18. void HeapPop(Heap* hp);
  19. // 取堆顶的数据
  20. HPDataType HeapTop(Heap* hp);
  21. // 堆的数据个数
  22. int HeapSize(Heap* hp);
  23. // 堆的判空
  24. int HeapEmpty(Heap* hp);
  25. // 对数组进行堆排序
  26. void HeapSort(int* a, int n);
  27. void swap(int *a, int *b)
  28. {
  29. int tmp = *a;
  30. *a = *b;
  31. *b = tmp;
  32. }
  33. void AdjustUp(int *a, int child, int n)
  34. {
  35. int parent = (child - 1) / 2;
  36. while (child > 0)
  37. {
  38. if (a[child] > a[parent])
  39. {
  40. swap(&a[child], &a[parent]);
  41. child = parent;
  42. parent = (child - 1) / 2;
  43. }
  44. else
  45. {
  46. break;
  47. }
  48. }
  49. }
  50. void AdjustDown(int *a, int parent,int n)
  51. {
  52. int child = parent * 2 + 1;
  53. while ( child < n)
  54. {
  55. if (child + 1 < n && a[child]<a[child + 1])
  56. {
  57. ++child;
  58. }
  59. if(a[child]>a[parent])
  60. {
  61. swap(&a[child], &a[parent]);
  62. parent = child;
  63. child = (parent * 2) + 1;
  64. }
  65. else
  66. {
  67. break;
  68. }
  69. }
  70. }
  71. void HeapCreate(Heap* hp, HPDataType* a, int n)
  72. {
  73. assert(hp);
  74. hp->_a = (HPDataType*)malloc(sizeof(HPDataType)*n);
  75. if (hp->_a == NULL)
  76. {
  77. printf("malloc fail");
  78. exit(-1);
  79. }
  80. for (int i = 0; i < n; ++i)
  81. {
  82. hp->_a[i] = a[i];
  83. }
  84. hp->_size = hp->_capacity = n;
  85. for (int i = (n - 2) / 2; i >= 0; --i)
  86. {
  87. AdjustDown(hp->_a,i, hp->_size);
  88. }
  89. }
  90. // 堆的销毁
  91. void HeapDestory(Heap* hp)
  92. {
  93. assert(hp);
  94. hp->_size = hp->_capacity = 0;
  95. free(hp);
  96. }
  97. // 堆的插入
  98. void HeapPush(Heap* hp, HPDataType x)
  99. {
  100. assert(hp);
  101. if (hp->_size == hp->_capacity)
  102. {
  103. HPDataType* tmp = (HPDataType*)realloc(hp->_a, sizeof(HPDataType)* 2 * hp->_capacity);
  104. if (tmp == NULL)
  105. {
  106. printf("realloc fail");
  107. exit(-1);
  108. }
  109. hp->_a = tmp;
  110. hp->_a[hp->_size] = x;
  111. ++hp->_size;
  112. hp->_capacity *= 2;
  113. }
  114. else
  115. {
  116. hp->_a[hp->_size] = x;
  117. ++hp->_size;
  118. }
  119. AdjustUp(hp->_a, hp->_size-1, hp->_size);
  120. }
  121. // 堆的删除
  122. void HeapPop(Heap* hp)
  123. {
  124. assert(hp);
  125. assert(hp->_size>0);
  126. swap(&hp->_a[hp->_size-1], &hp->_a[0]);
  127. --hp->_size;
  128. AdjustDown(hp->_a, 0, hp->_size);
  129. }
  130. // 取堆顶的数据
  131. HPDataType HeapTop(Heap* hp)
  132. {
  133. assert(hp);
  134. assert(hp->_size>0);
  135. return hp->_a[0];
  136. }
  137. // 堆的数据个数
  138. int HeapSize(Heap* hp)
  139. {
  140. assert(hp);
  141. return hp->_size;
  142. }
  143. // 堆的判空
  144. int HeapEmpty(Heap* hp)
  145. {
  146. assert(hp);
  147. return hp->_size == 0 ? 1 : 0;
  148. for (int i = 0; i < 3; ++i)}
  149. // 对数组进行堆排序
  150. void HeapSort(int* a, int n)
  151. {
  152. assert(a);
  153. for (int i = (n - 2) / 2; i >= 0; --i)
  154. {
  155. AdjustDown(a, i, n);
  156. }
  157. int end = n - 1;
  158. while (end > 0)
  159. {
  160. swap(&a[0], &a[end]);
  161. AdjustDown(a, 0, end);
  162. --end;
  163. }
  164. }

三、二叉树链式结构及实现


1.二叉树链式结构的遍历

所谓遍历(Traversal)是指沿着某条搜索路线,依次对树中每个结点均做一次且仅做一次访问。访问结点所做的操作依赖于具体的应用问 题。 遍历是二叉树上最重要的运算之一,是二叉树上进行其它运算之基础。
前序/中序/后序的递归结构遍历:是根据访问结点操作发生位置命名

前序遍历(Preorder Traversal 亦称先序遍历)——访问根结点的操作发生在遍历其左右子树之前。
中序遍历(Inorder Traversal)——访问根结点的操作发生在遍历其左右子树之中(间)。
后序遍历(Postorder Traversal)——访问根结点的操作发生在遍历其左右子树之后。
由于被访问的结点必是某子树的根,所以N(Node)、L(Left subtree)和R(Right subtree)又可解释为根、根的左子树和根的右子树。NLR、LNR和LRN分别又称为先根遍历、中根遍历和后根遍历。
层序遍历:除了先序遍历、中序遍历、后序遍历外,还可以对二叉树进行层序遍历。设二叉树的根节点所在层数为1,层序遍历就是从所在二叉树的根节点出发,首先访问第一层的树根节点,然后从左到右访问第2层上的节点,接着是第三层的节点,以此类推,自上而下,自左至右逐层访问树的结点的过程就是层序遍历。

2.二叉树的链式实现

  1. typedef char BTDataType;
  2. typedef struct BinaryTreeNode
  3. {
  4. BTDataType _data;
  5. struct BinaryTreeNode* _left;
  6. struct BinaryTreeNode* _right;
  7. }BTNode;
  8. typedef BTNode* QDataType;
  9. // 链式结构:表示队列
  10. typedef struct QListNode
  11. {
  12. struct QListNode* _next;
  13. QDataType _data;
  14. }QNode;
  15. // 队列的结构
  16. typedef struct Queue
  17. {
  18. QNode* _front;
  19. QNode* _rear;
  20. }Queue;
  21. BTNode* CreateBTNode(BTDataType x);
  22. // 通过前序遍历的数组"ABD##E#H##CF##G##"构建二叉树
  23. BTNode* BinaryTreeCreate(BTDataType* a, int n, int* pi);
  24. // 二叉树销毁
  25. void BinaryTreeDestory(BTNode** root);
  26. // 二叉树节点个数
  27. int BinaryTreeSize(BTNode* root);
  28. // 二叉树叶子节点个数
  29. int BinaryTreeLeafSize(BTNode* root);
  30. // 二叉树第k层节点个数
  31. int BinaryTreeLevelKSize(BTNode* root, int k);
  32. // 二叉树查找值为x的节点
  33. BTNode* BinaryTreeFind(BTNode* root, BTDataType x);
  34. // 二叉树前序遍历
  35. void BinaryTreePrevOrder(BTNode* root);
  36. // 二叉树中序遍历
  37. void BinaryTreeInOrder(BTNode* root);
  38. // 二叉树后序遍历
  39. void BinaryTreePostOrder(BTNode* root);
  40. // 初始化队列
  41. void QueueInit(Queue* q);
  42. // 队尾入队列
  43. void QueuePush(Queue* q, QDataType data);
  44. // 队头出队列
  45. void QueuePop(Queue* q);
  46. // 获取队列头部元素
  47. QDataType QueueFront(Queue* q);
  48. // 获取队列队尾元素
  49. QDataType QueueBack(Queue* q);
  50. // 获取队列中有效元素个数
  51. int QueueSize(Queue* q);
  52. // 检测队列是否为空,如果为空返回非零结果,如果非空返回0
  53. int QueueEmpty(Queue* q);
  54. // 销毁队列
  55. void QueueDestroy(Queue* q);
  56. // 层序遍历
  57. void BinaryTreeLevelOrder(BTNode* root);
  58. // 判断二叉树是否是完全二叉树
  59. int BinaryTreeComplete(BTNode* root);
  60. // 初始化队列
  61. void QueueInit(Queue* q)
  62. {
  63. assert(q);
  64. q->_front = q->_rear = NULL;
  65. }
  66. // 队尾入队列
  67. void QueuePush(Queue* q, QDataType data)
  68. {
  69. assert(q);
  70. QNode *newnode = ((QNode*)malloc(sizeof(QNode)));
  71. newnode->_data = data;
  72. newnode->_next = NULL;
  73. if (q->_rear == NULL)
  74. {
  75. q->_front = q->_rear = newnode;
  76. }
  77. else
  78. {
  79. q->_rear->_next = newnode;
  80. //q->_rear = q->_rear->_next;
  81. q->_rear = newnode;
  82. }
  83. }
  84. // 队头出队列
  85. void QueuePop(Queue* q)
  86. {
  87. assert(q);
  88. assert(!QueueEmpty(q));
  89. if (q->_front == q->_rear)
  90. {
  91. free(q->_front);
  92. //free(q->_rear);
  93. q->_front = q->_rear = NULL;
  94. }
  95. else
  96. {
  97. QNode *cur = q->_front->_next;
  98. free(q->_front);
  99. q->_front = cur;
  100. }
  101. }
  102. // 获取队列头部元素
  103. QDataType QueueFront(Queue* q)
  104. {
  105. assert(q);
  106. assert(!QueueEmpty(q));
  107. return q->_front->_data;
  108. }
  109. // 获取队列队尾元素
  110. QDataType QueueBack(Queue* q)
  111. {
  112. assert(q);
  113. assert(!QueueEmpty(q));
  114. return q->_rear->_data;
  115. }
  116. // 获取队列中有效元素个数
  117. int QueueSize(Queue* q)
  118. {
  119. assert(q);
  120. int size = 0;
  121. QNode* cur = q->_front;
  122. while (cur)
  123. {
  124. ++size;
  125. cur = cur->_next;
  126. }
  127. return size;
  128. }
  129. // 检测队列是否为空,如果为空返回非零结果,如果非空返回0
  130. int QueueEmpty(Queue* q)
  131. {
  132. assert(q);
  133. return q->_front == NULL ? 1 : 0;
  134. }
  135. // 销毁队列
  136. void QueueDestroy(Queue* q)
  137. {
  138. assert(q);
  139. QNode *cur = q->_front;
  140. while (cur)
  141. {
  142. QNode *next = cur->_next;
  143. free(cur);
  144. cur = next;
  145. }
  146. q->_front = q->_rear = NULL;
  147. }
  148. BTNode* CreateBTNode(BTDataType x)
  149. {
  150. BTNode *node = (BTNode*)malloc(sizeof(BTNode));
  151. node->_data = x;
  152. node->_left = NULL;
  153. node->_right = NULL;
  154. return node;
  155. }
  156. // 通过前序遍历的数组"ABD##E#H##CF##G##"构建二叉树
  157. BTNode* BinaryTreeCreate(BTDataType* a, int n, int* pi)
  158. {
  159. if (a[*pi] == '#')
  160. {
  161. return NULL;
  162. }
  163. BTNode *node = (BTNode*)malloc(sizeof(BTNode));
  164. node->_data = a[*pi];
  165. ++*pi;
  166. node->_left = BinaryTreeCreate(a, n, pi);
  167. ++*pi;
  168. node->_right = BinaryTreeCreate(a, n, pi);
  169. return node;
  170. }
  171. // 二叉树销毁
  172. void BinaryTreeDestory(BTNode** root)
  173. {
  174. if (*root != NULL)
  175. {
  176. if ((*root)->_left) // 有左孩子
  177. BinaryTreeDestory(&(*root)->_left); // 销毁左孩子子树
  178. if ((*root)->_right) // 有右孩子
  179. BinaryTreeDestory(&(*root)->_right); // 销毁右孩子子树
  180. free(*root); // 释放根结点
  181. *root = NULL; // 空指针赋NULL
  182. }
  183. }
  184. // 二叉树节点个数
  185. int BinaryTreeSize(BTNode* root)
  186. {
  187. if (root == NULL)
  188. {
  189. return 0;
  190. }
  191. return BinaryTreeSize(root->_left) + BinaryTreeSize(root->_right) + 1;
  192. }
  193. // 二叉树叶子节点个数
  194. int BinaryTreeLeafSize(BTNode* root)
  195. {
  196. if (root == NULL)
  197. {
  198. return 0;
  199. }
  200. if (root->_left == NULL&&root->_right == NULL)
  201. {
  202. return 1;
  203. }
  204. return BinaryTreeLeafSize(root->_left) + BinaryTreeLeafSize(root->_right);
  205. }
  206. // 二叉树第k层节点个数
  207. int BinaryTreeLevelKSize(BTNode* root, int k)
  208. {
  209. if (root == NULL)
  210. {
  211. return 0;
  212. }
  213. if (k == 1)
  214. {
  215. return 1;
  216. }
  217. return BinaryTreeLevelKSize(root->_left, k - 1) + BinaryTreeLevelKSize(root->_right, k - 1);
  218. }
  219. // 二叉树查找值为x的节点
  220. BTNode* BinaryTreeFind(BTNode* root, BTDataType x)
  221. {
  222. if (root == NULL)
  223. {
  224. return NULL;
  225. }
  226. if (root->_data == x)
  227. {
  228. return root;
  229. }
  230. BTNode* ret=BinaryTreeFind(root->_left,x);
  231. if (ret != NULL)
  232. {
  233. return ret;
  234. }
  235. ret = BinaryTreeFind(root->_right, x);
  236. if (ret != NULL)
  237. {
  238. return ret;
  239. }
  240. return NULL;
  241. }
  242. // 二叉树前序遍历
  243. void BinaryTreePrevOrder(BTNode* root)
  244. {
  245. if (root == NULL)
  246. {
  247. //printf("NULL ");
  248. return;
  249. }
  250. printf("%c ", root->_data);
  251. BinaryTreePrevOrder(root->_left);
  252. BinaryTreePrevOrder(root->_right);
  253. }
  254. // 二叉树中序遍历
  255. void BinaryTreeInOrder(BTNode* root)
  256. {
  257. if (root == NULL)
  258. {
  259. //printf("NULL ");
  260. return;
  261. }
  262. BinaryTreeInOrder(root->_left);
  263. printf("%c ", root->_data);
  264. BinaryTreeInOrder(root->_right);
  265. }
  266. // 二叉树后序遍历
  267. void BinaryTreePostOrder(BTNode* root)
  268. {
  269. if (root == NULL)
  270. {
  271. //printf("NULL ");
  272. return;
  273. }
  274. BinaryTreePostOrder(root->_left);
  275. BinaryTreePostOrder(root->_right);
  276. printf("%c ", root->_data);
  277. }
  278. // 层序遍历
  279. void BinaryTreeLevelOrder(BTNode* root)
  280. {
  281. Queue q;
  282. QueueInit(&q);
  283. if (root)
  284. {
  285. QueuePush(&q, root);
  286. }
  287. while (!QueueEmpty(&q))
  288. {
  289. BTNode *front = QueueFront(&q);
  290. QueuePop(&q);
  291. printf("%c ", front->_data);
  292. if (front->_left)
  293. {
  294. QueuePush(&q, front->_left);
  295. }
  296. if (front->_right)
  297. {
  298. QueuePush(&q, front->_right);
  299. }
  300. }
  301. }
  302. // 判断二叉树是否是完全二叉树
  303. int BinaryTreeComplete(BTNode* root)
  304. {
  305. Queue q;
  306. QueueInit(&q);
  307. if (root)
  308. {
  309. QueuePush(&q, root);
  310. }
  311. while (!QueueEmpty(&q))
  312. {
  313. BTNode *front = QueueFront(&q);
  314. QueuePop(&q);
  315. if (front == NULL)
  316. {
  317. break;
  318. }
  319. printf("%s ", front->_data);
  320. if (front->_left)
  321. {
  322. QueuePush(&q, front->_left);
  323. }
  324. if (front->_right)
  325. {
  326. QueuePush(&q, front->_right);
  327. }
  328. }
  329. while (!QueueEmpty(&q))
  330. {
  331. BTNode *front = QueueFront(&q);
  332. QueuePop(&q);
  333. if (front != NULL)
  334. {
  335. return 0;
  336. }
  337. }
  338. return 1;
  339. }

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号