mirror of
https://github.com/ada-dmitry/prog.l_ada.git
synced 2026-09-24 09:10:14 +00:00
add calkBT
This commit is contained in:
Binary file not shown.
+105
-109
@@ -5,7 +5,9 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
void pr(tower *tmp)
|
#define max(x, y) (((x) > (y)) ? (x) : (y))
|
||||||
|
|
||||||
|
void pr(node *tmp)
|
||||||
{
|
{
|
||||||
if (tmp == NULL)
|
if (tmp == NULL)
|
||||||
{
|
{
|
||||||
@@ -14,63 +16,52 @@ void pr(tower *tmp)
|
|||||||
}
|
}
|
||||||
while (tmp)
|
while (tmp)
|
||||||
{
|
{
|
||||||
printf("[%14p]<-[%14p]->[%14p] ", tmp->prev, tmp, tmp->next);
|
printf("[%14p]->[%14p] ", tmp, tmp->next);
|
||||||
printf("data: [%4ld] s[%s]\n", tmp->data, tmp->s);
|
printf("size: [%4d]\n", tmp->size);
|
||||||
tmp = tmp->next;
|
tmp = tmp->next;
|
||||||
}
|
}
|
||||||
printf("-------------\n");
|
printf("-------------\n");
|
||||||
}
|
}
|
||||||
tower *add_head(tower *crown, int hh)
|
node *add_head(node *head, int hh)
|
||||||
{
|
{
|
||||||
tower *tmp = NULL;
|
node *tmp = NULL;
|
||||||
int i;
|
if ((tmp = malloc(sizeof(node))) == NULL)
|
||||||
if ((tmp = malloc(sizeof(tower))) == NULL)
|
|
||||||
{
|
{
|
||||||
perror("malloc: NULL");
|
perror("malloc: NULL");
|
||||||
exit(2);
|
exit(2);
|
||||||
}
|
}
|
||||||
tmp->prev = NULL;
|
tmp->size = hh;
|
||||||
tmp->data = hh;
|
if (head == NULL)
|
||||||
for (i = 0; i < N - 1; i++)
|
|
||||||
tmp->s[i] = ((char)(65 + rand() % 25));
|
|
||||||
tmp->s[N - 1] = '\0';
|
|
||||||
if (crown == NULL)
|
|
||||||
tmp->next = NULL;
|
tmp->next = NULL;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
tmp->next = crown;
|
tmp->next = head;
|
||||||
crown->prev = tmp;
|
|
||||||
}
|
}
|
||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
tower *del_head(tower *crown)
|
node *del_head(node *head)
|
||||||
{
|
{
|
||||||
tower *tmp = crown;
|
node *tmp = head;
|
||||||
if (crown == NULL)
|
if (head == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
if (crown->next == NULL)
|
if (head->next == NULL)
|
||||||
{
|
{
|
||||||
free(crown);
|
free(head);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
crown = crown->next;
|
head = head->next;
|
||||||
crown->prev = NULL;
|
|
||||||
free(tmp);
|
free(tmp);
|
||||||
return crown;
|
return head;
|
||||||
}
|
}
|
||||||
|
|
||||||
int length(tower *list)
|
int length(node *list)
|
||||||
{
|
{
|
||||||
tower *tmp = list;
|
node *tmp = list;
|
||||||
int res = 0;
|
int res = 0;
|
||||||
if (list == NULL)
|
if (list == NULL || list->size == 0)
|
||||||
return 0;
|
|
||||||
if (list->data == 0 && !strcmp(list->s, "\0"))
|
|
||||||
return 0;
|
return 0;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
while (tmp->prev != NULL)
|
|
||||||
tmp = tmp->prev;
|
|
||||||
while (tmp != NULL)
|
while (tmp != NULL)
|
||||||
{
|
{
|
||||||
res++;
|
res++;
|
||||||
@@ -80,41 +71,18 @@ int length(tower *list)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
tower *adv_add_head(tower *crown, int hh, char string[])
|
void swap(node *a, node *b)
|
||||||
{
|
{
|
||||||
tower *tmp = NULL;
|
node *tmp = NULL;
|
||||||
if ((tmp = malloc(sizeof(tower))) == NULL)
|
if ((tmp = malloc(sizeof(node))) == NULL)
|
||||||
{
|
|
||||||
perror("malloc: NULL");
|
|
||||||
exit(2);
|
|
||||||
}
|
|
||||||
tmp->prev = NULL;
|
|
||||||
tmp->data = hh;
|
|
||||||
memcpy(tmp->s, string, N);
|
|
||||||
if (crown == NULL)
|
|
||||||
tmp->next = NULL;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
tmp->next = crown;
|
|
||||||
crown->prev = tmp;
|
|
||||||
}
|
|
||||||
return tmp;
|
|
||||||
}
|
|
||||||
void swap(tower *a, tower *b)
|
|
||||||
{
|
|
||||||
tower *tmp = NULL;
|
|
||||||
if ((tmp = malloc(sizeof(tower))) == NULL)
|
|
||||||
exit(1);
|
exit(1);
|
||||||
tmp->data = a->data;
|
tmp->size = a->size;
|
||||||
a->data = b->data;
|
a->size = b->size;
|
||||||
b->data = tmp->data;
|
b->size = tmp->size;
|
||||||
memcpy(tmp->s, a->s, N);
|
|
||||||
memcpy(a->s, b->s, N);
|
|
||||||
memcpy(b->s, tmp->s, N);
|
|
||||||
free(tmp);
|
free(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
tower *get(tower *list, int n)
|
node *get(node *list, int n)
|
||||||
{
|
{
|
||||||
if (n >= length(list))
|
if (n >= length(list))
|
||||||
exit(3);
|
exit(3);
|
||||||
@@ -124,73 +92,101 @@ tower *get(tower *list, int n)
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
tower *sort(tower *crown)
|
node *sort(node *head)
|
||||||
{
|
{
|
||||||
if (crown == NULL || crown->next == NULL)
|
if (head == NULL || head->next == NULL)
|
||||||
return crown;
|
return head;
|
||||||
int i, j, n = length(crown);
|
int i, j, n = length(head);
|
||||||
for (i = 0; i < n - 1; i++)
|
for (i = 0; i < n - 1; i++)
|
||||||
{
|
{
|
||||||
for (j = i + 1; j < n; j++)
|
for (j = i + 1; j < n; j++)
|
||||||
{
|
{
|
||||||
if (get(crown, j)->data < get(crown, i)->data)
|
if (get(head, j)->size < get(head, i)->size)
|
||||||
swap(get(crown, i), get(crown, j));
|
swap(get(head, i), get(head, j));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return crown;
|
return head;
|
||||||
}
|
}
|
||||||
tower *printextra(tower *crown, int len, int i)
|
pointer *add_head_address(pointer *head, node **address)
|
||||||
{
|
{
|
||||||
if (len < i || len == 0)
|
pointer *tmp = NULL;
|
||||||
|
if ((tmp = malloc(sizeof(pointer))) == NULL)
|
||||||
{
|
{
|
||||||
printf("*********************************|");
|
perror("malloc: NULL");
|
||||||
return crown;
|
exit(2);
|
||||||
}
|
}
|
||||||
|
tmp->address = address;
|
||||||
|
if (head == NULL)
|
||||||
|
tmp->next = NULL;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("[%14p] data: [%4ld] s[%s]|", crown, crown->data, crown->s);
|
tmp->next = head;
|
||||||
return crown->prev;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
tower *end(tower *crown)
|
|
||||||
{
|
|
||||||
tower *tmp = NULL;
|
|
||||||
tmp = crown;
|
|
||||||
if (crown == NULL)
|
|
||||||
return crown;
|
|
||||||
while (tmp->next != NULL)
|
|
||||||
tmp = tmp->next;
|
|
||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
void printftof(tower *head1, tower *head2, tower *head3)
|
node *printextra(node *head, int len, int i)
|
||||||
{
|
{
|
||||||
int a = length(head1), b = length(head2), c = length(head3), maximum, i;
|
if (len < i || len == 0)
|
||||||
maximum = max(a, b);
|
{
|
||||||
maximum = max(maximum, c);
|
for (i = 0; i < T - 1; i++)
|
||||||
head1 = end(head1);
|
printf(" ");
|
||||||
head2 = end(head2);
|
printf("|");
|
||||||
head3 = end(head3);
|
return head;
|
||||||
for (i = maximum; i > 0; i--)
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
disk(head->size);
|
||||||
|
printf("|");
|
||||||
|
return head->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void prcount(char s, int n)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
printf("%c", s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void disk(int size)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
prcount(' ', T / 2 - size);
|
||||||
|
for (i = 0; i < size - 1; i++)
|
||||||
|
{
|
||||||
|
printf("* ");
|
||||||
|
}
|
||||||
|
printf("*");
|
||||||
|
prcount(' ', T / 2 - size);
|
||||||
|
}
|
||||||
|
void printftof(pointer *adresses)
|
||||||
|
{
|
||||||
|
node *head1 = *(adresses->address), *head2 = *(adresses->next->address), *head3 = *(adresses->next->next->address);
|
||||||
|
int a = length(head1), b = length(head2), c = length(head3), i; // maximum
|
||||||
|
// maximum = max(max(a, b), c);
|
||||||
|
for (i = T / 2; i > 0; i--)
|
||||||
{
|
{
|
||||||
head1 = printextra(head1, a, i);
|
head1 = printextra(head1, a, i);
|
||||||
head2 = printextra(head2, b, i);
|
head2 = printextra(head2, b, i);
|
||||||
head3 = printextra(head3, c, i);
|
head3 = printextra(head3, c, i);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
printf("----------------------------------------------------------------------------------------------------------\n");
|
prcount('-', 3 * T);
|
||||||
printf("Tower 1 |Tower 2 |Tower 3 |\n");
|
printf("\n");
|
||||||
printf("----------------------------------------------------------------------------------------------------------\n\n");
|
printf("Tower 1");
|
||||||
}
|
prcount(' ', T - 8);
|
||||||
int max(int a, int b)
|
printf("|Tower 2");
|
||||||
{
|
prcount(' ', T - 8);
|
||||||
if (a < b)
|
printf("|Tower 3");
|
||||||
return b;
|
prcount(' ', T - 8);
|
||||||
else
|
printf("|\n");
|
||||||
return a;
|
prcount('-', 3 * T);
|
||||||
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void hanoi_tower(int n, tower **head_from, tower **head_to, tower **head_aux, tower **const tower1, tower **const tower2, tower **const tower3)
|
void hanoi_tower(int n, node **head_from, node **head_to, node **head_aux, pointer *adresses)
|
||||||
{
|
{
|
||||||
if (n == 0)
|
if (n == 0)
|
||||||
{
|
{
|
||||||
@@ -200,17 +196,17 @@ void hanoi_tower(int n, tower **head_from, tower **head_to, tower **head_aux, to
|
|||||||
else if (n == 1)
|
else if (n == 1)
|
||||||
{
|
{
|
||||||
if (length(*head_to) == 0)
|
if (length(*head_to) == 0)
|
||||||
printftof(*tower1, *tower2, *tower3);
|
printftof(adresses);
|
||||||
*head_to = adv_add_head(*head_to, (*head_from)->data, (*head_from)->s);
|
*head_to = add_head(*head_to, (*head_from)->size);
|
||||||
*head_from = del_head(*head_from);
|
*head_from = del_head(*head_from);
|
||||||
if (length(*head_from) == 0)
|
if (length(*head_from) == 0)
|
||||||
printftof(*tower1, *tower2, *tower3);
|
printftof(adresses);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
hanoi_tower(n - 1, head_from, head_aux, head_to, tower1, tower2, tower3);
|
hanoi_tower(n - 1, head_from, head_aux, head_to, adresses);
|
||||||
printftof(*tower1, *tower2, *tower3);
|
printftof(adresses);
|
||||||
*head_to = adv_add_head(*head_to, (*head_from)->data, (*head_from)->s);
|
*head_to = add_head(*head_to, (*head_from)->size);
|
||||||
*head_from = del_head(*head_from);
|
*head_from = del_head(*head_from);
|
||||||
printftof(*tower1, *tower2, *tower3);
|
printftof(adresses);
|
||||||
hanoi_tower(n - 1, head_aux, head_to, head_from, tower1, tower2, tower3);
|
hanoi_tower(n - 1, head_aux, head_to, head_from, adresses);
|
||||||
}
|
}
|
||||||
@@ -1,28 +1,35 @@
|
|||||||
#include "prot.h"
|
#include "prot.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
|
||||||
tower *crown1 = NULL, *crown2 = NULL, *crown3 = NULL;
|
node *head = NULL, *head2 = NULL, *head3 = NULL;
|
||||||
|
pointer *addresses = NULL;
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
|
||||||
printf("Generated linked list (method of crown1)\n");
|
printf("Generated linked list (method of head)\n");
|
||||||
while ((r = (rand() % 100)) < 75)
|
/*while ((r=(rand()%T)) < T/2){
|
||||||
crown1 = add_head(crown1, r);
|
if (r==0) r+=1;
|
||||||
pr(crown1);
|
head = add_head(head, r);
|
||||||
|
}*/
|
||||||
printf("Length of linked list is: %d\n", length(crown1));
|
for (r = 1; r <= T / 2; r++)
|
||||||
|
{
|
||||||
|
head = add_head(head, r);
|
||||||
|
}
|
||||||
|
pr(head);
|
||||||
|
printf("Length of linked list is: %d\n", length(head));
|
||||||
printf("Sorted linked list\n");
|
printf("Sorted linked list\n");
|
||||||
sort(crown1);
|
sort(head);
|
||||||
pr(crown1);
|
pr(head);
|
||||||
|
addresses = add_head_address(addresses, &head3);
|
||||||
hanoi_tower(length(crown1), &crown1, &crown3, &crown2, &crown1, &crown2, &crown3);
|
addresses = add_head_address(addresses, &head2);
|
||||||
|
addresses = add_head_address(addresses, &head);
|
||||||
|
hanoi_tower(length(head), &head, &head3, &head2, addresses);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -1,23 +1,27 @@
|
|||||||
#define N 10
|
#define N 10
|
||||||
|
#define T 20
|
||||||
|
|
||||||
typedef struct Node
|
typedef struct Node
|
||||||
{
|
{
|
||||||
long int data;
|
int size;
|
||||||
struct Node *next;
|
struct Node *next;
|
||||||
struct Node *prev;
|
} node;
|
||||||
char s[N];
|
typedef struct nodepointer
|
||||||
} tower;
|
{
|
||||||
|
struct nodepointer *next;
|
||||||
|
struct Node **address;
|
||||||
|
} pointer;
|
||||||
|
|
||||||
void pr(tower *tmp);
|
void pr(node *tmp);
|
||||||
tower *add_head(tower *crown, int hh);
|
node *add_head(node *head, int hh);
|
||||||
tower *del_head(tower *crown);
|
node *del_head(node *head);
|
||||||
int length(tower *list);
|
pointer *add_head_address(pointer *head, node **address);
|
||||||
tower *sort(tower *crown);
|
int length(node *list);
|
||||||
tower *get(tower *list, int n);
|
node *sort(node *head);
|
||||||
void swap(tower *a, tower *b);
|
node *get(node *list, int n);
|
||||||
tower *adv_add_head(tower *crown, int hh, char string[]);
|
void swap(node *a, node *b);
|
||||||
void hanoi_tower(int n, tower **head_from, tower **head_to, tower **head_aux, tower **const tower1, tower **const tower2, tower **const tower3);
|
void hanoi_tower(int n, node **head_from, node **head_to, node **head_aux, pointer *adresses);
|
||||||
void printftof(tower *head1, tower *head2, tower *head3);
|
void printftof(pointer *adresses);
|
||||||
tower *end(tower *crown);
|
node *printextra(node *head, int len, int i);
|
||||||
tower *printextra(tower *crown, int len, int i);
|
void disk(int size);
|
||||||
int max(int a, int b);
|
void prcount(char s, int n);
|
||||||
Binary file not shown.
@@ -0,0 +1,312 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <error.h>
|
||||||
|
#include "head.h"
|
||||||
|
|
||||||
|
Tree *tnew(void)
|
||||||
|
{
|
||||||
|
Tree *tmp;
|
||||||
|
if ((tmp = malloc(sizeof(Tree))) == NULL)
|
||||||
|
{
|
||||||
|
perror("malloc:");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
tmp->key = 0;
|
||||||
|
tmp->left = NULL;
|
||||||
|
tmp->right = NULL;
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
Tree *leaf(char c)
|
||||||
|
{
|
||||||
|
Tree *tmp;
|
||||||
|
tmp = tnew();
|
||||||
|
tmp->key = c;
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
Tree *maketree(char expr[], int first, int last)
|
||||||
|
{
|
||||||
|
Tree *root;
|
||||||
|
int k;
|
||||||
|
|
||||||
|
if (first == last)
|
||||||
|
{
|
||||||
|
return leaf(expr[first]);
|
||||||
|
}
|
||||||
|
|
||||||
|
k = lastop(expr, first, last);
|
||||||
|
root = tnew();
|
||||||
|
root->key = expr[k];
|
||||||
|
root->left = maketree(expr, first, k - 1);
|
||||||
|
root->right = maketree(expr, k + 1, last);
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char priority(char c)
|
||||||
|
{
|
||||||
|
if ((c - '0' >= 0) && (c - '0' <= 9))
|
||||||
|
{
|
||||||
|
return MPRT;
|
||||||
|
}
|
||||||
|
switch (c)
|
||||||
|
{
|
||||||
|
case '+':
|
||||||
|
case '-':
|
||||||
|
return 1;
|
||||||
|
case '*':
|
||||||
|
case '/':
|
||||||
|
return 2;
|
||||||
|
case '^':
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
int lastop(char expr[], int first, int last)
|
||||||
|
{
|
||||||
|
unsigned char min, prt;
|
||||||
|
int i, k;
|
||||||
|
min = MPRT;
|
||||||
|
for (i = first; i <= last; i++)
|
||||||
|
{
|
||||||
|
prt = priority(expr[i]);
|
||||||
|
if (prt <= min)
|
||||||
|
{
|
||||||
|
min = prt;
|
||||||
|
k = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return k;
|
||||||
|
}
|
||||||
|
|
||||||
|
int calculate(Tree *root)
|
||||||
|
{
|
||||||
|
int num1, num2;
|
||||||
|
if (!(root->left))
|
||||||
|
{
|
||||||
|
return root->key - 0x30;
|
||||||
|
}
|
||||||
|
num1 = calculate(root->left);
|
||||||
|
num2 = calculate(root->right);
|
||||||
|
switch (root->key)
|
||||||
|
{
|
||||||
|
case '+':
|
||||||
|
return num1 + num2;
|
||||||
|
case '-':
|
||||||
|
return num1 - num2;
|
||||||
|
case '*':
|
||||||
|
return num1 * num2;
|
||||||
|
case '/':
|
||||||
|
return num1 / num2;
|
||||||
|
case '^':
|
||||||
|
return pow(num1, num2);
|
||||||
|
}
|
||||||
|
return 0xffffffff;
|
||||||
|
}
|
||||||
|
|
||||||
|
void showtree(Tree *root, int level)
|
||||||
|
{
|
||||||
|
if ((root->key > 31) && (root->key < 127))
|
||||||
|
{
|
||||||
|
printf("%c ", root->key);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("%X ", root->key);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (root->right)
|
||||||
|
{
|
||||||
|
printf("------> ");
|
||||||
|
level++;
|
||||||
|
showtree(root->right, level);
|
||||||
|
level--;
|
||||||
|
}
|
||||||
|
if (root->left)
|
||||||
|
{
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
for (i = 0; i < level; i++)
|
||||||
|
{
|
||||||
|
printf(" ");
|
||||||
|
}
|
||||||
|
printf("| \n");
|
||||||
|
|
||||||
|
for (i = 0; i < level; i++)
|
||||||
|
{
|
||||||
|
printf(" ");
|
||||||
|
}
|
||||||
|
showtree(root->left, level);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Tree *create(unsigned char key)
|
||||||
|
{
|
||||||
|
Tree *tmp;
|
||||||
|
if ((tmp = malloc(sizeof(Tree))) == NULL)
|
||||||
|
{
|
||||||
|
perror("malloc:");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
tmp->key = key;
|
||||||
|
tmp->parent = NULL;
|
||||||
|
tmp->left = NULL;
|
||||||
|
tmp->right = NULL;
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
Tree *add(Tree *root, unsigned char key)
|
||||||
|
{
|
||||||
|
Tree *rtmp = root, *rsave = NULL, *tmp;
|
||||||
|
if ((tmp = malloc(sizeof(Tree))) == NULL)
|
||||||
|
{
|
||||||
|
perror("malloc:");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
while (rtmp)
|
||||||
|
{
|
||||||
|
rsave = rtmp;
|
||||||
|
if (key < rtmp->key)
|
||||||
|
{
|
||||||
|
rtmp = rtmp->left;
|
||||||
|
}
|
||||||
|
else if (key > rtmp->key)
|
||||||
|
{
|
||||||
|
rtmp = rtmp->right;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("\n There is element %c \n", key);
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tmp->parent = rsave;
|
||||||
|
tmp->left = NULL;
|
||||||
|
tmp->right = NULL;
|
||||||
|
tmp->key = key;
|
||||||
|
if (key < rsave->key)
|
||||||
|
{
|
||||||
|
rsave->left = tmp;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rsave->right = tmp;
|
||||||
|
}
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
Tree *search(Tree *root, unsigned char key)
|
||||||
|
{
|
||||||
|
if ((root == NULL) || (root->key == key))
|
||||||
|
{
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
if (key < root->key)
|
||||||
|
{
|
||||||
|
return search(root->left, key);
|
||||||
|
}
|
||||||
|
else if (key > root->key)
|
||||||
|
{
|
||||||
|
return search(root->right, key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Tree *min(Tree *root)
|
||||||
|
{
|
||||||
|
while (root->left != NULL)
|
||||||
|
{
|
||||||
|
root = root->left;
|
||||||
|
}
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
Tree *max(Tree *root)
|
||||||
|
{
|
||||||
|
while (root->right != NULL)
|
||||||
|
{
|
||||||
|
root = root->right;
|
||||||
|
}
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
Tree *nextdel(Tree *root)
|
||||||
|
{
|
||||||
|
Tree *l = NULL;
|
||||||
|
if (root->right != NULL)
|
||||||
|
{
|
||||||
|
return min(root->right);
|
||||||
|
}
|
||||||
|
l = root->parent;
|
||||||
|
while ((l != NULL) && (root == l->right))
|
||||||
|
{
|
||||||
|
root = l;
|
||||||
|
l = l->parent;
|
||||||
|
}
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
|
||||||
|
void delete(Tree *del)
|
||||||
|
{
|
||||||
|
Tree *tmp;
|
||||||
|
if (del == NULL)
|
||||||
|
{
|
||||||
|
printf("\n no such elem \n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if ((del->left == NULL) && (del->right == NULL))
|
||||||
|
{
|
||||||
|
if (del == del->parent->right)
|
||||||
|
{
|
||||||
|
del->parent->right = NULL;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
del->parent->right = NULL;
|
||||||
|
}
|
||||||
|
free(del);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((del->left == NULL) && (del->right != NULL))
|
||||||
|
{
|
||||||
|
if (del == del->parent->right)
|
||||||
|
{
|
||||||
|
del->parent->right = del->right;
|
||||||
|
del->parent->right->parent = del->parent;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
del->parent->left = del->right;
|
||||||
|
del->parent->left->parent = del->parent;
|
||||||
|
}
|
||||||
|
free(del);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((del->left != NULL) && (del->right == NULL))
|
||||||
|
{
|
||||||
|
if (del == del->parent->right)
|
||||||
|
{
|
||||||
|
del->parent->right = del->left;
|
||||||
|
del->parent->right->parent = del->parent;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
del->parent->left = del->left;
|
||||||
|
del->parent->left->parent = del->parent;
|
||||||
|
}
|
||||||
|
free(del);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((del->left != NULL) && (del->right != NULL))
|
||||||
|
{
|
||||||
|
tmp = min(del->right);
|
||||||
|
del->key = tmp->key;
|
||||||
|
delete (tmp);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
#define MPRT 255
|
||||||
|
#define SIZE 80
|
||||||
|
|
||||||
|
typedef struct tree
|
||||||
|
{
|
||||||
|
unsigned char key;
|
||||||
|
struct tree *parent;
|
||||||
|
struct tree *left;
|
||||||
|
struct tree *right;
|
||||||
|
} Tree;
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <error.h>
|
||||||
|
#include "head.h"
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
char s[SIZE];
|
||||||
|
Tree *tree = NULL;
|
||||||
|
if (argc < 2)
|
||||||
|
{
|
||||||
|
printf("no expression\n");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
strcpy(s, argv[1]);
|
||||||
|
printf("processing %s\n", s);
|
||||||
|
tree = maketree(s, 0, strlen(s) - 1);
|
||||||
|
|
||||||
|
printf("\n tree: \n");
|
||||||
|
showtree(tree, 0);
|
||||||
|
|
||||||
|
printf("= %d \n", calculate(tree));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user