标签:
1 #include <cmath> 2 #include <iostream> 3 4 using namespace std; 5 6 class Node{ 7 public: 8 int data; 9 Node *L; 10 Node *R; 11 Node():data(0), L(NULL), R(NULL){} 12 }; 13 14 class SearchTree{ 15 public: 16 Node *root; 17 SearchTree():root(NULL){}; 18 int depth(Node *S); 19 Node *farther(int data); 20 Node *check(int pos); 21 bool add(Node *S, int data); 22 bool del(Node *S, int data); 23 }; 24 25 int SearchTree::depth(Node *S) 26 { 27 if(NULL == S) 28 return 0; 29 return depth(S->L) > depth(S->R) ? depth(S->L) + 1 : depth(S->R) + 1; 30 } 31 32 Node *SearchTree::check(int pos) 33 { 34 if(pos == 1) 35 return root; 36 Node *parent = check(pos/2); 37 if(NULL != parent) 38 { 39 return pos%2 ? parent->R : parent->L; 40 } 41 else 42 return NULL; 43 } 44 45 bool SearchTree::add(Node *S, int data) 46 { 47 if(root == NULL) 48 { 49 Node *temp = new Node(); 50 temp->data = data; 51 root = temp; 52 return true; 53 } 54 if(S->data == data) 55 return false; 56 else if(S->data < data) 57 { 58 if(S->R == NULL) 59 { 60 Node *temp = new Node(); 61 temp->data = data; 62 S->R = temp; 63 return true; 64 } 65 else 66 return add(S->R, data); 67 } 68 else 69 { 70 if(S->L == NULL) 71 { 72 Node *temp = new Node(); 73 temp->data = data; 74 S->L = temp; 75 return true; 76 } 77 else 78 return add(S->L, data); 79 } 80 } 81 82 Node * SearchTree::farther(int data) 83 { 84 Node *temp = root, *far = NULL; 85 while(temp != NULL) 86 { 87 if(temp->data == data) 88 return far; 89 else if(temp->data >= data) 90 { 91 far = temp; 92 temp = temp->L; 93 } 94 else 95 { 96 far = temp; 97 temp = temp->R; 98 } 99 if(temp == NULL) 100 return NULL; 101 } 102 return NULL; 103 } 104 105 bool SearchTree::del(Node *S, int data) 106 { 107 if(S == NULL) 108 return false; 109 if(S->data < data) 110 return del(S->R, data); 111 else if(S->data > data) 112 return del(S->L, data); 113 else 114 { 115 if(S->L == NULL) 116 farther(data)->L == S ? farther(data)->L = S->R : farther(data)->R = S->R; 117 else if(S->R == NULL) 118 farther(data)->L == S ? farther(data)->L = S->L : farther(data)->R = S->L; 119 else 120 { 121 Node *p = S->L; 122 while(p->R != NULL) 123 p = p->R; 124 farther(p->data)->L == p ? farther(p->data)->L = NULL : farther(p->data)->R = NULL; 125 p->R = S->R; 126 Node *q = p; 127 while(q->L != NULL) 128 q = q->L; 129 q->L = S->L; 130 farther(data)->L == S ? farther(data)->L = p : farther(data)->R = p; 131 } 132 delete S; 133 return true; 134 } 135 } 136 137 ostream &operator<<(ostream &out,SearchTree &R) 138 { 139 for(int i = 1; i < pow(2, R.depth(R.root)); i++) 140 { 141 if(R.check(i) == NULL) 142 cout<<i<<"\t"<<"NULL"<<endl; 143 else 144 cout<<i<<"\t"<<R.check(i)->data<<endl; 145 } 146 return out; 147 } 148 149 int main() 150 { 151 SearchTree t; 152 t.add(t.root, 62); 153 t.add(t.root, 88); 154 t.add(t.root, 58); 155 t.add(t.root, 47); 156 t.add(t.root, 35); 157 t.add(t.root, 73); 158 t.add(t.root, 51); 159 t.add(t.root, 99); 160 t.add(t.root, 37); 161 t.add(t.root, 93); 162 t.del(t.root, 47); 163 cout<<t<<endl; 164 }
标签:
原文地址:http://www.cnblogs.com/yangzhouyyz/p/5065334.html