Ремонт принтеров, сканнеров, факсов и остальной офисной техники


назад Оглавление вперед




[12]

return(x);

void deleteNode(Node *z) { Node *x, *y;

/*****************************

* delete node z from tree * *****************************/

/* y will be removed from the parent chain */

if (!z z == NULL) return;

/* find tree successor */

if (z->left == NULL z->right == NULL) y = z;

y = z->right;

while (y->left != NULL) y = y->left;

/* x is ys only child */

if (y->left != NULL)

x = y->left;

x = y->right;

/* remove y from the parent chain */ if (x) x->parent = y->parent; if (y->parent)

if (y == y->parent->left) y->parent->left = x;

y->parent->right = x;

root = x;

/* y is the node were removing */ /* z is the data were removing */

/* if z and y are not the same, replace z with y. */

if (y != z) {

y->left = z->left;

if (y->left) y->left->parent = y; y->right = z->right;

if (y->right) y->right->parent = y; y->parent = z->parent; if (z->parent)

if (z == z->parent->left) z->parent->left = y;

z->parent->right = y;

root = y; free (z); } else {

free (y);


Node *findNode(T data) {

/*******************************

* find node containing data * *******************************/

Node *current = root; while(current != NULL)

if(compEQ(data, current->data)) return (current);

current = compLT(data, current->data) ? current->left : current->right;

return(0);

4.7 Коды для красно-черных деревьев

typedef int T;/* type of item to be sorted */

#define compLT(a,b) (a < b) #define compEQ(a,b) (a == b)

/* Red-Black tree description */ typedef enum { BLACK, RED } nodeColor;

typedef struct Node {

struct Node *left;/* left child */

struct Node *right;/* right child */

struct Node *parent;/* parent */

nodeColor color;/* node color (BLACK, RED) */

T data;/* data stoRED in node */ } Node;

#define NIL &sentinel/* all leafs are sentinels */

Node sentinel = { NIL, NIL, 0, BLACK, 0};

Node *root = NIL;/* root of Red-Black tree */

void rotateLeft(Node *x) {

/**************************

* rotate node x to left * **************************/

Node *y = x->right;

/* establish x->right link */ x->right = y->left;

if (y->left != NIL) y->left->parent = x;

/* establish y->parent link */ if (y != NIL) y->parent = x->parent;


} (аЯН == лохоо<-Л) jt . q.t[6Tj<-q.uajBd<-q.uajBd<-x = эрод } (q.jaT<-q.ua:rBd<-q.ua:rBd<-x == q.ua:cBd<-x) jt I+ иотвхотл в элвц эм } (аЯН == ioxoo<-q.u3JBd<-x 55 ooi = j x) эттцм I+ saxq.jadojd эвта-рэн з[оэцо

*x эрой 6uti3sut oraqje *

} (х* эрод) dnxT,jq.jasuT ртол

.Л = q.uaBd<-x (TIN =i x) jt . x = .ц6тл<-Л I* Л рив x 3[UTT */

} ssxa {

= q.jaT<-q.ua:cBd<-x

= q.t[6Tj<-q.uajBd<-x (q.t[6Tj<-q.uajBd<-x == x) jt

} (q.u33B<J<-x) jt ; q.uajB<J<-x = ц.иэхте<3.<-Л (TIN =i ) JT 4UTT q-ua:cBd<-A u;sTTC[Bq.sa */

.x = q.uajBd<-q.t[6Tj<-A (TIN =i Ч.Ч-БТЛ<-) JT

.:щ6т;1<-Л = 4-JST<-x /* 4"TT qjs-[<-x qsTTqqss */

.q.jax<-x = Л* эрод

* q.t[6TJ oq. x эрой aq.Bq.oor *

} (x* эрод) qqbTgsqBqoj РТол-

.Л = quaBd<-x (TIN =i x) jt .x = qjaT<-A Л рив x 3[UTT */

.Л = ц.оох

} ssxa {

= qt[6Tj<-quajBd<-x

= qjax<-quajBd<-x (qjax<-quajBd<-x == x) jt

} (q.u33B<J<-x) jt



[стр.Начало] [стр.1] [стр.2] [стр.3] [стр.4] [стр.5] [стр.6] [стр.7] [стр.8] [стр.9] [стр.10] [стр.11] [стр.12] [стр.13] [стр.14] [стр.15]