其他分享
首页 > 其他分享> > 做一个平衡二叉树(c语言)(无讲解)

做一个平衡二叉树(c语言)(无讲解)

作者:互联网

binarytree.h:

 1 #ifndef TREE_H
 2 #define TREE_H 1
 3 #define Well 0
 4 #define LLeftProblem 1
 5 #define RRightProblem 2
 6 #define LRightProblem 3
 7 #define RLeftProblem 4
 8 #define L_LLproblem 5
 9 #define R_RRproblem 6
10 typedef struct Node_tree
11 {
12     int val;
13     struct Node_tree* parent, * leftc, * rightc;
14 }node_tree;
15 
16 typedef struct BinaryTree
17 {
18     node_tree* root;
19 }tree;
20 typedef struct Problem
21 {
22     int proble;
23     node_tree* node;
24 }problem;
25 void insertData(tree*, int);
26 int getHeight(node_tree*);
27 node_tree* getPos(tree*, int);
28 tree* createTree();
29 node_tree* createNode_Tree(int);
30 void checkTree(tree*);
31 problem* traverse(node_tree*);
32 int checkNode(node_tree*);
33 void LLchange(node_tree*);
34 void LRchange(node_tree*);
35 void RLchange(node_tree*);
36 void RRchange(node_tree*);
37 void printTree(tree*);
38 void changeNode(node_tree*, node_tree*);
39 #endif
binarytree.h

BalanceTree.cpp:

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include"binaryTree.h"
  4 #include"queue.h"
  5 /*
  6  * brief:创建一个平衡二叉树!
  7  * 定义节点,定义树,创建一个队列用来层次遍历.
  8  */
  9 node_tree* createNode_Tree(int data)
 10 {
 11     node_tree* newnode = (node_tree*)malloc(sizeof(node_tree));
 12     newnode->leftc = NULL;
 13     newnode->parent = NULL;
 14     newnode->rightc = NULL;
 15     newnode->val = data;
 16     return newnode;
 17 }
 18 tree* createTree()
 19 {
 20     tree* myTree = (tree*)malloc(sizeof(tree));
 21     myTree->root = NULL;
 22     return myTree;
 23 }
 24 node_tree* getPos(tree* thTree,int data)
 25 {
 26     node_tree* np = thTree->root;
 27     node_tree* ans = NULL;
 28     while(np!=NULL)
 29     {
 30         ans = np;
 31         if(data>=np->val)
 32         {
 33             np = np->leftc;
 34         }
 35         else
 36         {
 37             np = np->rightc;
 38         }
 39     }
 40     return ans;
 41 }
 42 int getHeight(node_tree* node)
 43 {
 44     if(node==NULL)
 45     {
 46         return -1;
 47     }
 48     else
 49     {
 50         int left = getHeight(node->leftc);
 51         int right = getHeight(node->rightc);
 52         return 1 + (left > right ? left : right);
 53     }
 54 }
 55 void insertData(tree* myTree,int data)
 56 {
 57     node_tree* newnode = createNode_Tree(data);
 58     if(myTree->root==NULL)
 59     {
 60         myTree->root = newnode;
 61         return;
 62     }
 63     else
 64     {
 65         node_tree* pos = getPos(myTree, data);
 66         if(pos->val<data)
 67         {
 68             pos->leftc = newnode;
 69             newnode->parent = pos;
 70         }
 71         else
 72         {
 73             pos->rightc = newnode;
 74             newnode->parent = pos;
 75         }
 76     }
 77     checkTree(myTree);
 78 }
 79 void checkTree(tree* myTree)
 80 {
 81 first:
 82     problem* theProblem = traverse(myTree->root);
 83     if(theProblem==NULL)
 84     {
 85         return;
 86     }
 87     else
 88     {
 89         node_tree* pronode = theProblem->node;
 90         int protype = theProblem->proble;
 91         if(protype==LLeftProblem)
 92         {
 93             LLchange(pronode);
 94         }
 95         else if(protype==LRightProblem)
 96         {
 97             LRchange(pronode);
 98         }
 99         else if(protype==RRightProblem)
100         {
101             RRchange(pronode);
102         }
103         else
104         {
105             RLchange(pronode);
106         }
107         goto first;
108     }
109 }
110 problem* traverse(node_tree* root)
111 {
112     node_tree* np = root;
113     queue* myQueue = createQueue();
114     push_queue(myQueue, np);
115     while(myQueue->length!=0)
116     {
117         np = myQueue->head->value;
118         if(np->rightc!=NULL)
119         {
120             push_queue(myQueue,np->rightc);
121         }
122         if(np->leftc!=NULL)
123         {
124             push_queue(myQueue, np->leftc);
125         }
126         if(checkNode(np)!=Well)
127         {
128             problem* pp = (problem*)malloc(sizeof(problem));
129             pp->node = np;
130             pp->proble = checkNode(np);
131             if(checkNode(np)==L_LLproblem)
132             {
133                 pp->node = np->leftc;
134                 pp->proble = LLeftProblem;
135                 if(traverse(np->leftc)!=NULL)
136                 {
137                     pp->node = traverse(np->leftc)->node;
138                     pp->proble = traverse(np->leftc)->proble;
139                 }
140             }
141             if(checkNode(np)==R_RRproblem)
142             {
143                 pp->node = np->rightc;
144                 pp->proble = RLeftProblem;
145                 if (traverse(np->rightc) != NULL)
146                 {
147                     pp->node = traverse(np->rightc)->node;
148                     pp->proble = traverse(np->rightc)->proble;
149                 }
150             }
151             return pp;
152         }
153         pop_queue(myQueue);
154     }
155     return NULL;
156 }
157 int checkNode(node_tree* node)
158 {
159     int left = getHeight(node->leftc);
160     int right = getHeight(node->rightc);
161     if(abs(left-right)>1)
162     {
163         if(left>right)
164         {
165             int ll = getHeight(node->leftc->leftc);
166             int lr = getHeight(node->leftc->rightc);
167             if(ll-lr>1)
168             {
169                 return L_LLproblem;
170             }
171             return ll < lr ? LRightProblem : LLeftProblem;
172         }
173         else
174         {
175             int rr = getHeight(node->rightc->rightc);
176             int rl = getHeight(node->rightc->leftc);
177             if(rr-rl>1)
178             {
179                 return R_RRproblem;
180             }
181             return rr > rl ? RRightProblem : RLeftProblem;
182         }
183     }
184     return Well;
185 }
186 void LLchange(node_tree* node)
187 {
188     node_tree* root = node;
189     node_tree* rootLeft = node->leftc;
190     node_tree* lright = rootLeft->rightc;
191     root->leftc = NULL;
192     rootLeft->parent = NULL;
193     changeNode(root, rootLeft);
194     root->rightc = rootLeft;
195     rootLeft->parent = root;
196     rootLeft->leftc = lright;
197 }
198 void LRchange(node_tree* node)
199 {
200     RRchange(node->leftc);
201     LLchange(node);
202 }
203 void RRchange(node_tree* node)
204 {
205     node_tree* root = node;
206     node_tree* rootRight = node->rightc;
207     node_tree* rleft = rootRight->leftc;
208     root->rightc = NULL;
209     rootRight->parent = NULL;
210     changeNode(root, rootRight);
211     root->leftc = rootRight;
212     rootRight->parent = root;
213     rootRight->rightc = rleft;
214 }
215 void RLchange(node_tree* node)
216 {
217     LLchange(node->rightc);
218     RRchange(node);
219 }
220 void printTree(tree* myTree)
221 {
222     node_tree* np = myTree->root;
223     queue* myQueue = createQueue();
224     push_queue(myQueue, np);
225     while (myQueue->length != 0)
226     {
227         np = myQueue->head->value;
228         if (np->rightc != NULL)
229         {
230             push_queue(myQueue, np->rightc);
231         }
232         if (np->leftc != NULL)
233         {
234             push_queue(myQueue, np->leftc);
235         }
236         printf("%d\t", np->val);
237         pop_queue(myQueue);
238     }
239 }
240 void changeNode(node_tree* np1, node_tree* np2)
241 {
242     if(np1==NULL)
243     {
244         np2->parent = NULL;
245         return;
246     }
247     else if(np2==NULL)
248     {
249         np1->parent = NULL;
250         return;
251     }
252     node_tree* p1r = np1->rightc;
253     node_tree* p1l = np1->leftc;
254     node_tree* p1p = np1->parent;
255     int p1v = np1->val;
256     np1->rightc = np2->rightc;
257     np1->leftc = np2->leftc;
258     np1->parent = np2->parent;
259     np1->val = np2->val;
260     np2->rightc = p1r;
261     np2->leftc = p1l;
262     np2->parent = p1p;
263     np2->val = p1v;
264 }
balanceTree.cpp

queue.h:

做一个队列来实现二叉树的层次遍历

 1 #ifndef QUEUE_H
 2 #define QUEUE_H 1
 3 #include"binaryTree.h"
 4 /*
 5  * brief:创建一个链式队列
 6  */
 7 typedef struct Node_queue
 8 {
 9     node_tree* value;
10     Node_queue* next;
11 }node_queue;
12 
13 typedef struct Queue
14 {
15     node_queue* head, * tail;
16     int length;
17 }queue;
18 
19 node_queue* createNode_queue(node_tree*);
20 queue* createQueue();
21 void push_queue(queue* myQueue, node_tree* data);
22 void pop_queue(queue*);
23 #endif
Queue.h

Queue.cpp:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include"queue.h"
 4 node_queue* createNode_queue(node_tree* data)
 5 {
 6     node_queue* newnode = (node_queue*)malloc(sizeof(node_queue));
 7     newnode->next = NULL;
 8     newnode->value = data;
 9     return newnode;
10 }
11 queue* createQueue()
12 {
13     queue* myQueue = (queue*)malloc(sizeof(queue));
14     myQueue->head = NULL;
15     myQueue->tail = NULL;
16     myQueue->length = 0;
17     return myQueue;
18 }
19 void push_queue(queue* myQueue,node_tree* data)
20 {
21     if(myQueue->length==0)
22     {
23         node_queue* newnode = createNode_queue(data);
24         myQueue->head = newnode;
25         myQueue->tail = newnode;
26         myQueue->length++;
27         return;
28     }
29     else
30     {
31         node_queue* newnode = createNode_queue(data);
32         myQueue->tail->next = newnode;
33         myQueue->tail = newnode;
34         myQueue->length++;
35         return;
36     }
37 }
38 void pop_queue(queue* myQueue)
39 {
40     if(myQueue->length==0)
41     {
42         return;
43     }
44     else
45     {
46         node_queue* thnode = myQueue->head->next;
47         myQueue->head->next = NULL;
48         free(myQueue->head);
49         myQueue->head = thnode;
50         myQueue->length--;
51     }
52 }
Queue.cpp

main.cpp:

这个在将这个数组插入到二叉树的过程中,包含了左旋,右旋,左右旋,右左旋的情况。

 1 #include<stdio.h>
 2 #include"binaryTree.h"
 3 
 4 
 5 int  main()
 6 {
 7     tree* myTree=createTree();
 8     int nums[10]={3,2,1,4,5,6,7,10,9,8};
 9     for(int i=0;i<10;i++)
10     {
11         insertData(myTree, nums[i]);
12         printTree(myTree);
13         printf("\n");
14     }
15     printf("\n");
16     printTree(myTree);
17     printf("\n");
18     return 0;
19 }

 

标签:node,语言,rightc,tree,queue,myQueue,二叉树,讲解,np
来源: https://www.cnblogs.com/dongtianloveblog/p/16517027.html