Cay Avl - Chuong Trinh - Call By Ref

  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Cay Avl - Chuong Trinh - Call By Ref as PDF for free.

More details

  • Words: 682
  • Pages: 7
1

typedef struct Node * ref; struct Node { int key; int count; int bal; ref left; ref right; }; /* –––––––––––––––––––––––––––––– */ void searchAdd(int x, ref&p, int &h) { ref p1, p2; if (p == NULL) { h = 1; p = new Node; p->key = x; p->count = 1; p->bal = 0; p->left = p->right = NULL; } else

if (x < p->key) { searchAdd(x, p->left, h); if (h) switch (p->bal) { case 1: p->bal = 0; h = 0; break; case 0: p->bal = -1; break; case -1: p1 = p->left; if (p1->bal == -1) {// LL p->left = p1->right; p1->right = p; p->bal = 0; p = p1; }

2

else { // LR p2 = p1->right; p1->right p2->left p->left p2->right

= = = =

p2->left; p1; p2->right; p;

if (p2->bal == -1) p->bal = 1; else p->bal = 0; if (p2->bal == 1) p1->bal = -1; else p1->bal = 0; p = p2;

} p->bal = 0; h = 0;

if (x > p->key) { searchAdd(x, p->right, h); if (h) switch (p->bal) { case -1: p->bal = 0; h = 0; break; case 0: p->bal = 1; break; case 1: p1 = p->right; if (p1->bal == 1) {//RR p->right = p1->left; p1->left = p;

}

} else

p->bal = 0; p = p1; }

3

else { // RL p2 = p1->left;

else { p->count ++;

p1->left = p2->right; p2->right = p1; p->right = p2->left; p2->left = p; if (p2->bal == 1) p->bal = -1; else p->bal = 0; if (p2->bal == -1) p1->bal = 1; else p1->bal = 0; p = p2;

} }

} p->bal = 0; h = 0;

} }

h = 0;

4

void balance1(ref &p, int &h) { ref p1, p2; switch (p->bal) { case -1: p->bal = 0;

break;

case 0: p->bal = 1; h = 0;

break;

else { p->bal = 0; p1->bal = 0; } p = p1; } else { // RL p2 = p1->left; b2 = p2->bal; p1->left = p2->right = p->right = p2->left =

case 1: p1 = p->right; b1 = p1->bal; if (b1 >= 0) { // RR p->right = p1->left; p1->left = p; if (b1 == 0) { p->bal = 1; p1->bal = -1; h = 0; }

p2->right; p1; p2->left; p;

if (b2 == 1) else if (b2 == -1) else p = p2; p2->bal = 0; } } // switch }

p->bal = -1; p->bal = 0; p1->bal = 1; p1->bal = 0;

5

void balance2(ref &p, int &h) { ref p1, p2; switch (p->bal) { case 1: p->bal = 0; case 0: p->bal = -1; h = 0;

else { p->bal = 0; p1->bal = 0; } p = p1;

break;

} else { // LR p2 = p1->right; b2 = p2->bal;

break;

p1->right p2->left p->left p2->right

case -1: p1 = p->left; b1 = p1->bal; if (b1 <= 0) { // LL p->left = p1->right; p1->right = p; if (b1 == 0) { p->bal = -1; p1->bal = 1; h = 0; }

= = = =

p2->left; p1; p2->right; p;

if (b2 == -1) else if (b2 == 1) else p = p2; p2->bal = 0; } }

} // switch

p->bal = 1; p->bal = 0; p1->bal = -1; p1->bal = 0;

6

void del(ref &q, ref &r, int &h) { if (r->right) { del(q, r->right, h); if (h) balance2(r, h); } else { q->key q->count q r h = 1; }

}

= = = =

r->key; r->count; r; r->left;

7

void searchDelete(int int &h) { ref q;

x,

ref

&p,

if (p == NULL) { puts("Khoa x khong co "); h = 0; } else if (x < p->key) { searchDelete(x, p->left, h); if (h) balance1(p, h); } else if (x > p->key) { searchDelete(x, p->right, h); if (h) balance2(p, h); }

}

else { q = p; if (q->right == NULL) { p = q->left; h = 1; } else if (q->left == NULL) { p = q->right; h = 1; } else { del(q, p->left, h); if (h) balance1(p, h); } delete q; }

Related Documents

Chuong Trinh
November 2019 27
Giao Trinh Cay Mo
October 2019 13
Avl
November 2019 4
Chuong Trinh Chi Tiet
November 2019 20