平衡二叉树

成绩 10 开启时间 2015年12月7日 星期一 10:00
折扣 0.8 折扣时间 2015年12月25日 星期五 23:55
允许迟交 关闭时间 2015年12月31日 星期四 23:55

    程序输入一个字符串(只包含小写字母),请按照字符的输入顺序建立平衡二叉排序树,并分别输出二叉树的先序序列、中序序列和后序序列,最后输出该二叉树向左旋转 90 度后的结构。

例如:向左旋转 90 度后,以每层向里缩进 4 个空格的方式输出,输出结果为:

        i     g         f a         d     c         b

输入:agxnzyimk

输出: Preorder: xigamknzy Inorder: agikmnxyz Postorder: agknmiyzx Tree:     z         y x             n         m             k     i         g             a

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 
  4 typedef struct Avnode
  5 {
  6     char data;
  7     struct Avnode * left, *right;
  8 }node, *pnode;
  9 pnode root = NULL;
 10 
 11 int Height(pnode P)
 12 {
 13     int hl, hr;
 14     if (P == NULL)
 15         return(0);
 16     else
 17     {
 18         hl = Height(P->left);
 19         hr = Height(P->right);
 20         return (hl > hr ? hl + 1 : hr + 1);
 21     }
 22 }
 23 void Inorder(pnode head)
 24 {
 25     if (head->left)
 26         Inorder(head->left);
 27     printf("%c", head->data);
 28     if (head->right)
 29         Inorder(head->right);
 30 }
 31 
 32 
 33 void Preorder(pnode head)
 34 {
 35     printf("%c", head->data);
 36     if (head->left)
 37         Preorder(head->left);
 38     if (head->right)
 39         Preorder(head->right);
 40 }
 41 void Postorder(pnode head)
 42 {
 43     if (head->left)
 44         Postorder(head->left);
 45     if (head->right)
 46         Postorder(head->right);
 47     printf("%c", head->data);
 48 }
 49 pnode SingleRotationLeft(pnode K)
 50 {
 51     pnode P;
 52     P = K->left;
 53     K->left = P->right;
 54     P->right = K;
 55     return(P);
 56 }
 57 pnode SingleRotationRight(pnode K)
 58 {
 59     pnode P;
 60     P = K->right;
 61     K->right = P->left;
 62     P->left = K;
 63     return(P);
 64 }
 65 pnode DoubleRotationLR(pnode K3)
 66 {
 67     pnode K2, K1;
 68     K1 = K3->left;
 69     K2 = K1->right;
 70     K1->right = K2->left;
 71     K3->left = K2->right;
 72     K2->left = K1;
 73     K2->right = K3;
 74     return(K2);
 75 }
 76 pnode DoubleRotationRL(pnode K1)
 77 {
 78     pnode K2, K3;
 79     K3 = K1->right;
 80     K2 = K3->left;
 81     K1->right = K2->left;
 82     K3->left = K2->right;
 83     K2->left = K1;
 84     K2->right = K3;
 85     return(K2);
 86 }
 87 pnode Insert(char c, pnode T)
 88 {
 89     if (T == NULL)
 90     {
 91         T = (pnode)malloc(sizeof(node));
 92         T->data = c;
 93         T->left = T->right = NULL;
 94     }
 95     else
 96         if (c < T->data)
 97         {
 98             T->left = Insert(c, T->left);
 99             if (Height(T->left) - Height(T->right) == 2)
100                 if (c < T->left->data)
101                     T = SingleRotationLeft(T);
102                 else
103                     T = DoubleRotationLR(T);
104         }
105         else
106             if (c > T->data)
107             {
108                 T->right = Insert(c, T->right);
109                 if (Height(T->right) - Height(T->left) == 2)
110                     if (c > T->right->data)
111                         T = SingleRotationRight(T);
112                     else
113                         T = DoubleRotationRL(T);
114             }
115     return(T);
116 }
117 void Turn(pnode cur, int deep)
118 {
119     int i;
120     if (cur->right)
121         Turn(cur->right, deep + 1);
122     for (i = 1; i < deep; i++)
123         printf("    ");
124     printf("%c
", cur->data);
125     if (cur->left)
126         Turn(cur->left, deep + 1);
127 }
128 main()
129 {
130     char c;
131     while (1)
132     {
133         c = getchar();
134         if (c == '
')
135             break;
136         root = Insert(c, root);
137     }
138     
139     printf("Preorder: ");
140     Preorder(root);
141     printf("
");
142     printf("Inorder: ");
143     Inorder(root);
144     printf("
");
145     printf("Postorder: ");
146     Postorder(root);
147     printf("
");
148     printf("Tree:
");
149     Turn(root, 1);
150     return(0);
151 }
原文地址:https://www.cnblogs.com/yixianyong/p/5091812.html