线索二叉树(中序)

2022/1/12 6:03:59

本文主要是介绍线索二叉树(中序),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

线索二叉树的抽象数据类型

1 enum PointerTag{ Link, Thread };
2 typedef struct ThreadNode
3 {
4     char data;
5     enum PointerTag LTag;
6     enum PointerTag RTag;
7     struct ThreadNode* Lchild;
8     struct ThreadNode* Rchild;
9 }BiThrNode, *BiThrTree;/*Threaded Binary Tree Node*/

创建线索二叉树

 1 void CreateTree(BiThrTree* A)
 2 {
 3     char ch = '\0';
 4     scanf(" %c", &ch);
 5     if (ch == '^')
 6     {
 7         (*A) =  NULL;
 8     }
 9     else
10     {
11         (*A) = (BiThrTree)calloc(1, sizeof(BiThrNode));
12         (*A)->data = ch;
13         (*A)->LTag = Link;
14         (*A)->RTag = Link;
15         CreateTree(&(*A)->Lchild);
16         CreateTree(&(*A)->Rchild);
17     }
18 }

二叉树中序线索化

 1 void ThreadTree(BiThrTree A)
 2 {/*二叉树的中序线索化*/
 3     static BiThrTree Pre = NULL;/*局部静态变量 Pre 始终记录刚刚访问过的结点*/
 4     if (A)
 5     {
 6         ThreadTree(A->Lchild);
 7 
 8         if (A->Lchild == NULL)
 9         {
10             A->LTag = Thread;
11             A->Lchild = Pre;
12         }
13         if (Pre != NULL && Pre->Rchild == NULL)
14         {/*当前结点存在,再回头处理上一个右结点为空的线索,让其指向当前结点(根节点)*/
15             Pre->RTag = Thread;
16             Pre->Rchild = A;
17         }
18         Pre = A;
19 
20         ThreadTree(A->Rchild);
21     }
22 }

中序遍历线索二叉树

 1 void InOrderTraverse(BiThrTree A)
 2 {
 3     while (A)
 4     {
 5         while (A->LTag == Link)
 6         {/*中序线索二叉树寻找遍历的首结点*/
 7             A = A->Lchild;
 8         }
 9         printf("%c", A->data);
10         while (A->RTag == Thread && A->Rchild != NULL)
11         {
12             A = A->Rchild;
13             printf("%c", A->data);
14         }
15         A = A->Rchild;
16     }
17 }

源代码

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 
 5 enum PointerTag { Link, Thread };
 6 typedef struct ThreadNode
 7 {
 8     char data;
 9     enum PointerTag LTag;
10     enum PointerTag RTag;
11     struct ThreadNode* Lchild;
12     struct ThreadNode* Rchild;
13 }BiThrNode, * BiThrTree;/*Threaded Binary Tree Node*/
14 
15 void CreateTree(BiThrTree* A);/*创建线索二叉树*/
16 void ThreadTree(BiThrTree A);/*二叉树中序线索化*/
17 void InOrderTraverse(BiThrTree A);/*中序遍历线索二叉树*/
18 
19 int main(void)
20 {
21     BiThrTree A = NULL;
22     printf("请输入该二叉树先序遍历序列(用^表示空子树,叶子结点要跟两个^):\n");
23     CreateTree(&A);
24     ThreadTree(A);
25     InOrderTraverse(A);
26     system("pause");
27     return 0;
28 }
29 
30 void CreateTree(BiThrTree* A)
31 {
32     char ch = '\0';
33     scanf(" %c", &ch);
34     if (ch == '^')
35     {
36         (*A) = NULL;
37     }
38     else
39     {
40         (*A) = (BiThrTree)calloc(1, sizeof(BiThrNode));
41         (*A)->data = ch;
42         (*A)->LTag = Link;
43         (*A)->RTag = Link;
44         CreateTree(&(*A)->Lchild);
45         CreateTree(&(*A)->Rchild);
46     }
47 }
48 
49 void ThreadTree(BiThrTree A)
50 {/*二叉树的中序线索化*/
51     static BiThrTree Pre = NULL;/*局部静态变量 Pre 始终记录刚刚访问过的结点*/
52     if (A)
53     {
54         ThreadTree(A->Lchild);
55 
56         if (A->Lchild == NULL)
57         {
58             A->LTag = Thread;
59             A->Lchild = Pre;
60         }
61         if (Pre != NULL && Pre->Rchild == NULL)
62         {/*当前结点存在,再回头处理上一个右结点为空的线索,让其指向当前结点(根节点)*/
63             Pre->RTag = Thread;
64             Pre->Rchild = A;
65         }
66         Pre = A;
67 
68         ThreadTree(A->Rchild);
69     }
70 }
71 
72 void InOrderTraverse(BiThrTree A)
73 {
74     while (A)
75     {
76         while (A->LTag == Link)
77         {/*中序线索二叉树寻找遍历的首结点*/
78             A = A->Lchild;
79         }
80         printf("%c", A->data);
81         while (A->RTag == Thread && A->Rchild != NULL)
82         {
83             A = A->Rchild;
84             printf("%c", A->data);
85         }
86         A = A->Rchild;
87     }
88 }
View Code

 



这篇关于线索二叉树(中序)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程