-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadBinaryTree.c
More file actions
118 lines (92 loc) · 2.26 KB
/
Copy pathThreadBinaryTree.c
File metadata and controls
118 lines (92 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "ThreadBinaryTree.h"
#include <stdlib.h>
#include <stdio.h>
//--helper
Node *newNode(Element data){
Node *new = (Node *)malloc(sizeof(Node));
new->lc = new->rc = NULL;
new->lthread = new->rthread = false;
new->data = data;
return new;
}
//--helper end
Node *successor(Node *now){
Node *temp = now->rc;
if(now->rthread)
return temp;
else{
while(!temp->lthread)
temp = temp->lc;
return temp;
}
}
Node *precessor(Node *now){
Node *temp = now->lc;
if(now->lthread)
return temp;
else{
while(!temp->rthread)
temp = temp->rc;
return temp;
}
}
void inorder(ThreadBinary *tree){
Node *now = tree->head->lc;
while(!now->lthread){
now = now->lc;
}
while(now != tree->head){
printf("%c ", (char)now->data.key);
now = successor(now);
}
printf("\n");
return;
}
ThreadBinary* initThread(Element rootData){
ThreadBinary *tree = (ThreadBinary *)malloc(sizeof(ThreadBinary));
Element headTemp = {.key = -1};
tree->head = newNode(headTemp);
Node *root = newNode(rootData);
//init
tree->head->lc = root;
root->lthread = root->rthread = true;
root->lc = root->rc = tree->head;
return tree;
}
Node *insertleft(Node *original, Element data){
Node *new = newNode(data);
new->lc = original->lc;
new->lthread = original->lthread;
new->rc = original;
new->rthread = true;
original->lc = new;
original->lthread = false;
if(!new->lthread){
precessor(new)->rc = new;
}
return new;
}
Node *insertright(Node *original, Element data){
Node *new = newNode(data);
new->rc = original->rc;
new->rthread = original->rthread;
new->lc = original;
new->lthread = true;
original->rc = new;
original->rthread = false;
if(!new->rthread)
successor(new)->lc = new;
return new;
}
Element deleteleft(Node *parent, Node *target){
Element temp = target->data;
parent->lc = target->lc;
parent->lthread = target->lthread;
return temp;
}
Element deleteright(Node *parent, Node *target){
Element temp = target->data;
parent->rc = target->rc;
parent->rthread = target->rthread;
return temp;
}