Data_Structure/作業/unit7/Threaded Binary Tree_stack.cpp

417 lines
12 KiB
C++
Raw Normal View History

2025-01-20 21:30:53 +08:00
/*Notes: <20><><EFBFBD>J<EFBFBD>i<EFBFBD><69>
w={"Mary":10, "Tom":3, "Charlie":5, "Bob":6, "Dickens":20, 4:9, "z":0, "Za":12, "aZ":8}
w={10:10, 20:3, 5:5, 17:6, 23:20, 18:9, 3:0, 9:12}
w={10:10, 5:5, 9:6, 3:20, 7:9}
w={"A":10, "B":3, "Charlie":5, "Bob":6, "Dickens":20}
w={"D":10, "C":3, "B":5, "A":6}
del w["A"]
del w["Dickens"]
del w["Mary"]
del w["aZ"]
del w
w["Mary"]=1
w["Tom"]+=2
w[4]?
w?
*/
#include <stdio.h> /* for sscanf(), printf() */
#include <stdlib.h>
#include <string.h> /* for strstr(), strtok(), strtok_r() */
#include <conio.h>
#define MAX_STACK 100 // <20>w<EFBFBD>q<EFBFBD><71><EFBFBD>|<7C>j<EFBFBD>p
/* <20>w<EFBFBD>qdict<63><74><EFBFBD>c */
typedef struct dict { /* <20>޽u<DEBD>G<EFBFBD><47><EFBFBD>j<EFBFBD>M<EFBFBD><4D><EFBFBD>`<60>I<EFBFBD><49><EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5>c<EFBFBD>A<EFBFBD><41><EFBFBD>@<40>r<EFBFBD><72> */
char key[20]; /* <20><><EFBFBD><EFBFBD><EFBFBD>Ӹ`<60>I<EFBFBD><49><EFBFBD>u<EFBFBD><75><EFBFBD>v(key)<29><><EFBFBD>e */
int value; /* <20><><EFBFBD><EFBFBD><EFBFBD>u<EFBFBD><75><EFBFBD>v<EFBFBD><76><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>u<EFBFBD>ȡv(value)<29><><EFBFBD>e */
struct dict *llink; /* <20><><EFBFBD>V<EFBFBD><56><EFBFBD>l<EFBFBD><6C><EFBFBD>کΤ޽u<DEBD>γ~ */
struct dict *rlink; /* <20><><EFBFBD>V<EFBFBD>k<EFBFBD>l<EFBFBD><6C><EFBFBD>کΤ޽u<DEBD>γ~ */
int lbit; /* <20><><EFBFBD><EFBFBD> llink <20>O<EFBFBD>_<EFBFBD><5F><EFBFBD><EFBFBD><EFBFBD>`<60><><EFBFBD><EFBFBD> */
int rbit; /* <20><><EFBFBD><EFBFBD> rlink <20>O<EFBFBD>_<EFBFBD><5F><EFBFBD><EFBFBD><EFBFBD>`<60><><EFBFBD><EFBFBD> */
} dict;
// <20>w<EFBFBD>q<EFBFBD><71><EFBFBD>|<7C><><EFBFBD>c
typedef struct {
dict* data[MAX_STACK];
int top;
} Stack;
void show(void); /* <20><><EFBFBD>X<EFBFBD><58><EFBFBD><EFBFBD> */
void insert(char [], int); /* <20>N<EFBFBD><4E><EFBFBD>ƥ[<5B>J<EFBFBD>G<EFBFBD><47><EFBFBD>j<EFBFBD>M<EFBFBD><4D> */
void removing(char []); /* <20>N<EFBFBD><4E><EFBFBD>Ʊq<C6B1>G<EFBFBD><47><EFBFBD>j<EFBFBD>M<EFBFBD>𤤲<EFBFBD><F0A4A4B2><EFBFBD> */
void inorder(struct dict *); /* <20><><EFBFBD>ƥH<C6A5><48><EFBFBD>Ǫk<C7AA><6B><EFBFBD>X */
void free_all_nodes(struct dict *node);
void find_newroot(void); /* <20>j<EFBFBD>M<EFBFBD>s<EFBFBD><73><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>`<60>I */
dict *search_p(struct dict *node);
dict *search(char []); /* <20>j<EFBFBD>M<EFBFBD>`<60>I */
dict *root = NULL, *ptr, *prev ,*head = NULL ;
// <20><><EFBFBD>|<7C>ާ@<40><><EFBFBD><EFBFBD>
void initStack(Stack *s) {
s->top = -1;
}
int isEmpty(Stack *s) {
return s->top == -1;
}
int isFull(Stack *s) {
return s->top == MAX_STACK - 1;
}
void push(Stack *s, dict* node) {
if (!isFull(s)) {
s->data[++(s->top)] = node;
}
}
dict* pop(Stack *s) {
if (!isEmpty(s)) {
return s->data[(s->top)--];
}
return NULL;
}
void inorder(struct dict *root) {
if (root == NULL) {
return;
}
Stack stack;
initStack(&stack);
dict *current = root;
while (current != NULL || !isEmpty(&stack)) {
// <20>N<EFBFBD>Ҧ<EFBFBD><D2A6><EFBFBD><EFBFBD>l<EFBFBD>`<60>I<EFBFBD><49><EFBFBD>J<EFBFBD><4A><EFBFBD>|
while (current != NULL) {
push(&stack, current);
current = current->llink;
}
// <20>q<EFBFBD><71><EFBFBD>|<7C><><EFBFBD><EFBFBD><EFBFBD>X<EFBFBD>`<60>I<EFBFBD>óB<C3B3>z
current = pop(&stack);
if (current != NULL) {
printf("%s<>G%d\n", current->key, current->value);
// <20><><EFBFBD>ʨ<EFBFBD><CAA8>k<EFBFBD>l<EFBFBD>`<60>I
current = current->rlink;
}
}
}
// <20>D<EFBFBD><44><EFBFBD>j<EFBFBD><EFBFBD><E8A6A1><EFBFBD><EFBFBD><EFBFBD>Ҧ<EFBFBD><D2A6>`<60>I
void free_all_nodes(struct dict *root) {
if (root == NULL) {
return;
}
Stack stack;
initStack(&stack);
dict *current = root;
dict *last_processed = NULL;
while (current != NULL || !isEmpty(&stack)) {
if (current != NULL) {
push(&stack, current);
current = current->llink;
} else {
dict *peek = stack.data[stack.top];
// <20>p<EFBFBD>G<EFBFBD>k<EFBFBD>l<EFBFBD>`<60>I<EFBFBD>s<EFBFBD>b<EFBFBD>B<EFBFBD>|<7C><><EFBFBD>B<EFBFBD>z
if (peek->rlink != NULL && last_processed != peek->rlink) {
current = peek->rlink;
} else {
peek = pop(&stack);
printf("<EFBFBD>R<EFBFBD><EFBFBD>'w[%s]=%d'<27>`<60>I\n", peek->key, peek->value);
last_processed = peek;
free(peek);
current = NULL;
}
}
}
}
void show(void) {
if (root == NULL) {
puts("<EFBFBD>G<EFBFBD><EFBFBD><EFBFBD>j<EFBFBD>M<EFBFBD><EFBFBD><EFBFBD>O<EFBFBD>Ū<EFBFBD>!");
return;
}
printf("<EFBFBD>r<EFBFBD><EFBFBD><EFBFBD>ܼƤ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>x<EFBFBD>s<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(key<65>Gvalue)<29>p<EFBFBD>U\n");
inorder(root);
}
int main(){
head = (dict *)malloc(sizeof(dict));
if (head == NULL) {
printf("<EFBFBD>O<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>t<EFBFBD><EFBFBD><EFBFBD>ѡI\n");
return 1;
}
head->lbit = head->rbit = 1;
head->rlink = head;
head->llink = root;
char *token, key[12], line[128];
const char *s = " ={},"; /* <20><><EFBFBD>j<EFBFBD>ŰO<C5B0><4F><EFBFBD>r<EFBFBD><72> */
int value;
char *rest ,option;
printf("<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD>s<EFBFBD><EFBFBD><EFBFBD>r<EFBFBD><EFBFBD><EFBFBD><EFBFBD>z<EFBFBD>A<EFBFBD>w<EFBFBD><EFBFBD><Enter><3E><><EFBFBD><EFBFBD><EFBFBD>{<7B><>\n");
while (1) {
printf("\n<EFBFBD><EFBFBD><EFBFBD>O => ");
fgets(line,128,stdin); /* <20>ϥ<EFBFBD>gets()Ū<><C5AA><EFBFBD>]<5D>i<EFBFBD>H<EFBFBD>A<EFBFBD><41><EFBFBD>]gets()<29><><EFBFBD><>J<EFBFBD>r<EFBFBD><72><EFBFBD>ƪ<EFBFBD><C6AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>A<EFBFBD>i<EFBFBD><69><EFBFBD>ɭP<C9AD>x<EFBFBD>s<EFBFBD><73><EFBFBD>J<EFBFBD>r<EFBFBD><72><EFBFBD><EFBFBD><EFBFBD>}<7D>C<EFBFBD>Ŷ<EFBFBD><C5B6>z<EFBFBD><7A> */
if (line[0]=='\n') break; /* <20><><EFBFBD>J<EFBFBD>Ŧr<C5A6><72><EFBFBD>h<EFBFBD><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>j<EFBFBD><6A> */
if (strstr(line,"{")) { /* line<6E>t<EFBFBD><74>"{" */
sscanf(line,"w = {%[^}]}",line);
rest = line;
while (token = strtok_r(rest,s,&rest)) { /* strtok_r()<29><><EFBFBD>Ϊk<CEAA>i<EFBFBD>Ѩ<EFBFBD> http://bit.ly/3VcvbbZ */
/* printf("%s\n",token); */
if (strstr(token,":")) {/* token<65>t<EFBFBD><74>":" */
sscanf(token,"%[^:]:%d",key,&value);
insert(key,value);
// printf("Token #%d: <20><>=%s; <20><>=%d\n",++i,key,value);
}
}
}else if (strstr(line,"del")) {
if(sscanf(line,"del w[%[^]]]",key)){
if(strcmp(key,root->key)==0){
// printf("<22>R<EFBFBD><52><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>w[%s]<5D><>root<6F>`<60>I\n",key);
find_newroot();
// printf("<22>s<EFBFBD><73>root<6F>`<60>I<EFBFBD><49>w[%s]\n",root->key);
}else removing(key);
}else{
printf("<EFBFBD>L<EFBFBD>k<EFBFBD><EFBFBD>N<EFBFBD>R<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>@<40>Ӥ<EFBFBD><D3A4><EFBFBD><EFBFBD>A<EFBFBD>N<EFBFBD>R<EFBFBD><52><EFBFBD><EFBFBD><EFBFBD>Ӧr<D3A6><72>? (y/n) => ");
scanf("%c",&option);
fflush(stdin);
if (option == 'y' || option == 'Y') {
free_all_nodes(root);
root = NULL;
}else{
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>R<EFBFBD><EFBFBD><EFBFBD>Ҧ<EFBFBD><EFBFBD>r<EFBFBD><EFBFBD>\n");
}
}
}else if (strstr(line,"+=")) { /* line<6E>t<EFBFBD><74>"+=" */
sscanf(line,"w[%[^]]] +=%d",key,&value);
dict *modify_node;
if((modify_node = search(key)) == NULL){ /* <20><EFBFBD><E4A4A3><EFBFBD><EFBFBD><EFBFBD>ƫh<C6AB><68><EFBFBD>ܿ<EFBFBD><DCBF>~ */
printf("<EFBFBD><EFBFBD><EFBFBD>~! %s <20><><EFBFBD>b<EFBFBD><62><EFBFBD>G<EFBFBD><47><EFBFBD>j<EFBFBD>M<EFBFBD><4D><EFBFBD><EFBFBD>!\n", key);
}else{
modify_node->value += value;
printf("<EFBFBD>s<EFBFBD>]w[%s]=%d\n",key,modify_node->value);
}
}else if (strstr(line,"=")) { /* line<6E>t<EFBFBD><74>"=" */
sscanf(line,"w[%[^]]] = %d",key,&value);
dict *add_node;
if (root == NULL){
insert(key,value);
printf("<EFBFBD>s<EFBFBD>]w[%s]=%d\n",key,root->value);
}else{
if((add_node = search(key)) == NULL){ /* <20><EFBFBD><E4A4A3><EFBFBD><EFBFBD><EFBFBD>ƫh<C6AB>s<EFBFBD>W<EFBFBD><57><EFBFBD><EFBFBD> */
insert(key,value);
printf("%s <20><><EFBFBD>b<EFBFBD><62><EFBFBD>G<EFBFBD><47><EFBFBD>j<EFBFBD>M<EFBFBD><4D><EFBFBD><EFBFBD>!\n", key);
printf("<EFBFBD>s<EFBFBD>]w[%s]=%d\n",key,value);
}else{
add_node->value = value;
printf("<EFBFBD>s<EFBFBD>]w[%s]=%d\n",key,add_node->value);
}
}
}else if (sscanf(line,"w[%[^]]]?",key)==1){
dict *read_node;
if((read_node = search(key)) == NULL){ /* <20><EFBFBD><E4A4A3><EFBFBD><EFBFBD><EFBFBD>ƫh<C6AB>s<EFBFBD>W<EFBFBD><57><EFBFBD><EFBFBD> */
printf("<EFBFBD><EFBFBD><EFBFBD>~! %s <20><><EFBFBD>b<EFBFBD><62><EFBFBD>G<EFBFBD><47><EFBFBD>j<EFBFBD>M<EFBFBD><4D><EFBFBD><EFBFBD>!\n", key);
}else{
printf("<EFBFBD>r<EFBFBD><EFBFBD><EFBFBD>w[%s]=%d\n",key,read_node->value);
}
}else if (strstr(line,"?")) show();
else printf("<EFBFBD>y<EFBFBD>k<EFBFBD>L<EFBFBD>k<EFBFBD><EFBFBD><EFBFBD>{<7B>Asorry!\n");
}
printf("\n<EFBFBD>{<7B><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>A<EFBFBD>R<EFBFBD><52><EFBFBD>Ҧ<EFBFBD><D2A6>`<60>I<EFBFBD>Abye ~\n");
free_all_nodes(root);
free(head);
return 0;
}
/* <20>B<EFBFBD>z<EFBFBD>G<EFBFBD><47><EFBFBD>j<EFBFBD>M<EFBFBD><4D><EFBFBD>A<EFBFBD>N<EFBFBD>s<EFBFBD>W<EFBFBD><57><EFBFBD>ƥ[<5B>J<EFBFBD>ܤG<DCA4><47><EFBFBD>j<EFBFBD>M<EFBFBD><4D><EFBFBD><EFBFBD> */
void insert(char key[], int value){
struct dict *node, *prev;
/* <20><><EFBFBD>Ƥw<C6A4>s<EFBFBD>b<EFBFBD>h<EFBFBD><68><EFBFBD>ܿ<EFBFBD><DCBF>~ */
if (search(key) != NULL) {
printf("%s <20>w<EFBFBD>s<EFBFBD>b!\n", key);
return;
}
ptr = (struct dict *) malloc(sizeof(struct dict));
strcpy(ptr->key, key);
ptr->value = value;
ptr->llink = ptr->rlink = NULL;
ptr->lbit = ptr->rbit = 0;
if (root == NULL){/* <20><><EFBFBD>ڸ`<60>I<EFBFBD><49>NULL<4C><4C><EFBFBD><EFBFBD><EFBFBD>p */
root = ptr;
ptr->lbit = ptr->rbit = 1;
}else{ /* <20><><EFBFBD>ڸ`<60>I<EFBFBD><49><EFBFBD><EFBFBD>NULL<4C><4C><EFBFBD><EFBFBD><EFBFBD>p */
node = root;
while (node != NULL) { /* <20>j<EFBFBD>M<EFBFBD><4D><EFBFBD>ƴ<EFBFBD><C6B4>J<EFBFBD>I */
prev = node;
if(strcmp(ptr->key, node->key) < 0)
node = node->llink;
else
node = node->rlink;
}
if (strcmp(ptr->key, prev->key) < 0){
prev->llink = ptr;
prev->lbit = 1;
}else{
prev->rlink = ptr;
prev->rbit = 1;
}
}
}
/* <20>N<EFBFBD><4E><EFBFBD>Ʊq<C6B1>G<EFBFBD><47><EFBFBD>j<EFBFBD>M<EFBFBD>𤤲<EFBFBD><F0A4A4B2><EFBFBD> */
void removing(char key[]){
dict *del_node = search(key);
if (del_node == NULL) {
printf("%s <20><><EFBFBD>b<EFBFBD><62><EFBFBD>G<EFBFBD><47><EFBFBD>j<EFBFBD>M<EFBFBD><4D><EFBFBD><EFBFBD>!\n", key);
return;
}
dict *parent = search_p(del_node);
// <20>B<EFBFBD>z<EFBFBD>S<EFBFBD><53><EFBFBD>l<EFBFBD>`<60>I<EFBFBD><49><EFBFBD><EFBFBD><EFBFBD>p
if (del_node->llink == NULL && del_node->rlink == NULL) {
if (parent == NULL) { // <20>R<EFBFBD><52><EFBFBD><EFBFBD><EFBFBD>O<EFBFBD>ڸ`<60>I
root = NULL;
} else if (parent->llink == del_node) {
parent->llink = NULL;
} else {
parent->rlink = NULL;
}
}else if (del_node->llink == NULL || del_node->rlink == NULL) { // <20>B<EFBFBD>z<EFBFBD>@<40><EFBFBD>l<EFBFBD>`<60>I<EFBFBD><49><EFBFBD><EFBFBD><EFBFBD>p
dict *child = (del_node->llink != NULL) ? del_node->llink : del_node->rlink;
if (parent == NULL) { //<2F>R<EFBFBD><52><EFBFBD><EFBFBD><EFBFBD>O<EFBFBD>ڸ`<60>I
root = child;
} else if (parent->llink == del_node) {
parent->llink = child;
} else {
parent->rlink = child;
}
}else{ //<2F>B<EFBFBD>z<EFBFBD><7A><EFBFBD><EFBFBD>l<EFBFBD>`<60>I<EFBFBD><49><EFBFBD><EFBFBD><EFBFBD>p
if(del_node == parent->rlink){
parent->rlink = del_node->llink;
if(del_node->llink->rlink != NULL){ //<2F>Y<EFBFBD>R<EFBFBD><52><EFBFBD><EFBFBD><EFBFBD>`<60>I<EFBFBD><49><EFBFBD>l<EFBFBD>`<60>I<EFBFBD>٦<EFBFBD><D9A6>k<EFBFBD>`<60>I<EFBFBD><49><EFBFBD><EFBFBD><EFBFBD>p
dict *child = del_node->llink->rlink;
printf("%s\n",child->key);
while(child->rlink!= NULL){ //<2F><><EFBFBD>k<EFBFBD>`<60>I<EFBFBD><49><EFBFBD><EFBFBD><EFBFBD>̩<EFBFBD>
child = child->rlink;
printf("%s\n",child->key);
}
child->rlink = del_node->rlink;
}else parent->rlink->rlink = del_node->rlink; //<2F>S<EFBFBD><53><EFBFBD>h<EFBFBD><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}else{
parent->llink = del_node->llink;
if(del_node->rlink->llink != NULL){ //<2F>Y<EFBFBD>R<EFBFBD><52><EFBFBD><EFBFBD><EFBFBD>`<60>I<EFBFBD><49><EFBFBD>l<EFBFBD>`<60>I<EFBFBD>٦<EFBFBD><D9A6><EFBFBD><EFBFBD>`<60>I<EFBFBD><49><EFBFBD><EFBFBD><EFBFBD>p
dict *child = del_node->rlink->llink;
printf("%s\n",child->key);
while(child->llink!= NULL){ //<2F><EFBFBD>`<60>I<EFBFBD><49><EFBFBD><EFBFBD><EFBFBD>̩<EFBFBD>
child = child->llink;
printf("%s\n",child->key);
}
child->llink = del_node->llink;
}else parent->llink->rlink = del_node->rlink; //<2F>S<EFBFBD><53><EFBFBD>h<EFBFBD><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
}
free(del_node); /* <20><><EFBFBD><EFBFBD><EFBFBD>O<EFBFBD><4F><EFBFBD><EFBFBD> */
printf("<EFBFBD>R<EFBFBD><EFBFBD>w[%s]<5D><><EFBFBD><EFBFBD>\n",key);
}
void find_newroot(){ /* <20>j<EFBFBD>M<EFBFBD>s<EFBFBD><73><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>`<60>I <20><><EFBFBD>s<EFBFBD><73><EFBFBD>̤j<CCA4><6A>*/
struct dict *newroot_node ,*prev;
if(root->llink!=NULL){ //<2F><EFBFBD>s<EFBFBD><73><EFBFBD><EFBFBD><EFBFBD>̤j<CCA4><6A>
newroot_node = root->llink;
while(newroot_node->rlink!=NULL){ //<2F><><EFBFBD>ڪ<EFBFBD><DAAA><EFBFBD><EFBFBD>N<EFBFBD>`<60>I
prev = newroot_node;
newroot_node = newroot_node->rlink;
}
if(newroot_node->llink!=NULL){ //<2F>Y<EFBFBD>s<EFBFBD><73><EFBFBD>ڸ`<60>I<EFBFBD><49><EFBFBD><EFBFBD><EFBFBD>`<60>I
prev->rlink = newroot_node->llink;
}else prev->rlink = NULL;
if(newroot_node != root->llink ){ //<2F>Y<EFBFBD>s<EFBFBD><73><EFBFBD>ڸ`<60>I<EFBFBD><49><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڪ<EFBFBD><DAAA><EFBFBD><EFBFBD>`<60>I
newroot_node->llink = root->llink;
newroot_node->rlink = root->rlink;
}else{ //<2F>Y<EFBFBD>s<EFBFBD><73><EFBFBD>ڸ`<60>I<EFBFBD><49><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڪ<EFBFBD><DAAA><EFBFBD><EFBFBD>`<60>I
newroot_node->rlink = root->rlink;
}
printf("<EFBFBD>R<EFBFBD><EFBFBD>w[%s]<5D><><EFBFBD><EFBFBD>\n",root->key);
free(root);
root = newroot_node;
}else if(root->rlink!=NULL){ //<2F><><EFBFBD>k<EFBFBD>s<EFBFBD><73><EFBFBD><EFBFBD><EFBFBD>̤p<CCA4><70>
newroot_node = root->rlink;
while(newroot_node->llink!=NULL){ //<2F><><EFBFBD>ڪ<EFBFBD><DAAA><EFBFBD><EFBFBD>N<EFBFBD>`<60>I
prev = newroot_node;
newroot_node = newroot_node->llink;
}
if(newroot_node->rlink!=NULL){ //<2F>Y<EFBFBD>s<EFBFBD><73><EFBFBD>ڸ`<60>I<EFBFBD><49><EFBFBD>k<EFBFBD>`<60>I
prev->llink = newroot_node->rlink;
}else prev->llink = NULL;
if(newroot_node != root->rlink ){ //<2F>Y<EFBFBD>s<EFBFBD><73><EFBFBD>ڸ`<60>I<EFBFBD><49><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڪ<EFBFBD><DAAA>k<EFBFBD>`<60>I
newroot_node->llink = root->llink;
newroot_node->rlink = root->rlink;
}else{ //<2F>Y<EFBFBD>s<EFBFBD><73><EFBFBD>ڸ`<60>I<EFBFBD><49><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڪ<EFBFBD><DAAA>k<EFBFBD>`<60>I
newroot_node->llink = root->llink;
}
printf("<EFBFBD>R<EFBFBD><EFBFBD>w[%s]<5D><><EFBFBD><EFBFBD>\n",root->key);
free(root);
root = newroot_node;
}
}
/* <20>j<EFBFBD>Mtarget<65>Ҧb<D2A6>`<60>I */
struct dict *search(char target[]){
struct dict *node;
node = root;
while(node != NULL)
{
if (strcmp(target, node->key) == 0)
return node;
else
/* target<65>p<EFBFBD><70><EFBFBD>ثe<D8AB>`<60>I<EFBFBD>A<EFBFBD><41><EFBFBD><EFBFBD><EFBFBD>j<EFBFBD>M */
if (strcmp(target, node->key) < 0)
node = node->llink;
else /* target<65>j<EFBFBD><6A><EFBFBD>ثe<D8AB>`<60>I<EFBFBD>A<EFBFBD><41><EFBFBD>k<EFBFBD>j<EFBFBD>M */
node = node->rlink;
}
return node;
}
/* <20>j<EFBFBD>Mnode<64><65><EFBFBD><EFBFBD><EFBFBD>`<60>I */
struct dict *search_p(struct dict *node){
struct dict *parent;
parent = root;
while (parent != NULL) {
if (strcmp(node->key, parent->key) < 0) {
if (strcmp(node->key, parent->llink->key) == 0)
return parent;
else
parent = parent->llink;
}
else {
if (strcmp(node->key, parent->rlink->key) == 0)
return parent;
else
parent = parent->rlink;
}
}
return NULL;
}