Chinaunix首页 | 论坛 | 博客
  • 博客访问: 138301
  • 博文数量: 25
  • 博客积分: 588
  • 博客等级: 中士
  • 技术积分: 292
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-10 18:18
文章分类

全部博文(25)

文章存档

2012年(7)

2011年(18)

我的朋友

分类: C/C++

2012-02-13 17:16:51

  1. #include <iostream>

  2. using namespace std;

  3. typedef struct Node_ {
  4.     int key;
  5.     struct Node_* left, *right, *parent;
  6. }Node, *BST;

  7. Node *BSTsearch(BST T, int key) {
  8.     if (T == NULL) {
  9.         return NULL;
  10.     }
  11.     else if(key == T->key) {
  12.         return T;
  13.     }
  14.     else {
  15.         if (key < T->key) {
  16.             return BSTsearch(T->left, key);
  17.         }
  18.         else {
  19.             return BSTsearch(T->right, key);
  20.         }
  21.     }
  22. }

  23. Node *BSTsuccessor(BST T) {
  24.     if (T->right == NULL ) {
  25.         return T->parent;
  26.     }
  27.     else {
  28.         Node *p = T->right;
  29.         while (p->left != NULL) {
  30.             p = p->left;
  31.         }
  32.         return p;
  33.     }
  34. }

  35. Node *BSTinsert(BST &T, int key) {
  36.     Node *p = T;
  37.     Node *q = T;
  38.     while (q != NULL) {
  39.         p = q;
  40.         if (key <= p->key) {
  41.             q = p->left;
  42.         }
  43.         else {
  44.             q = p->right;
  45.         }
  46.     }
  47.     Node *tmp = new Node;
  48.     tmp->left = tmp->right = tmp->parent = NULL;
  49.     tmp->key = key;
  50.     if (p == NULL) {
  51.         T = tmp;
  52.     }
  53.     else {
  54.         tmp->parent = p;
  55.         if (key <= p->key) {
  56.             p->left = tmp;
  57.         }
  58.         else {
  59.             p->right = tmp;
  60.         }
  61.     }
  62.     return T;
  63. }

  64. int BSTdelete(BST &T, int key) {
  65.     Node *q = BSTsearch(T, key);
  66.     if (q == NULL) {
  67.         cout<<key<<" is not found"<<endl;
  68.         return false;
  69.     }
  70.     Node *y = NULL;
  71.     Node *x = NULL;
  72.     if (q->left == NULL || q->right == NULL) {
  73.         y = q;
  74.     }
  75.     else {
  76.         y = BSTsuccessor(q);
  77.     }
  78.     if (y->left != NULL) {
  79.         x = y->left;
  80.     }
  81.     else {
  82.         x = y->right;
  83.     }
  84.     if (x != NULL) {
  85.         x->parent = y->parent;
  86.     }
  87.     if (y->parent == NULL) {
  88.         T = x;
  89.     }
  90.     else if (y == y->parent->left) {
  91.         y->parent->left = x;
  92.     }
  93.     else {
  94.         y->parent->right = x;
  95.     }
  96.     if (y != q) {
  97.         q->key = y->key;
  98.     }
  99.     delete y;
  100.     return true;
  101. }

  102. void inorder(BST T) {
  103.     if (T != NULL) {
  104.         inorder(T->left);
  105.         cout<<T->key<<" ";
  106.         inorder(T->right);
  107.     }
  108. }

  109. int main(int argc, char **argv) {

  110.     BST T = NULL;
  111.     int n = 2;
  112.     while (n != 0) {
  113.         BSTinsert(T, n);
  114.         cout<<“中序遍历: ”;
  115.         inorder(T);
  116.         cout<<endl;
  117.         cin>>n;
  118.     }
  119.     n = 2;
  120.     cout<<endl;
  121.     inorder(T);
  122.     cout<<endl;
  123.     cout<<endl;
  124.     while (n != 0) {
  125.         BSTdelete(T, n);
  126.         cout<<"删除节点 "<;
  127.         inorder(T);
  128.         cout<<endl;
  129.         cin>>n;
  130.     }

  131.     return 0;
  132. }
阅读(1340) | 评论(0) | 转发(0) |
0

上一篇:flash 性能相关

下一篇:libevent中的小根堆

给主人留下些什么吧!~~