您的位置:首页 > 其它

二叉搜索树

2015-08-28 21:57 204 查看
用链表来表示, 其中每个节点为一个对象,除了key外,每个节点还包含了属性left, right, p,它们分别指向了结点的左孩子,右孩子和双亲。对任何结点x, 其左子树的所有结点关键字最大不超过x.key, 其右子树的所有结点关键字最小不低于x.key.

代码如下
http://www.cnblogs.com/sld666666/archive/2010/10/23/1859512.html http://www.cnblogs.com/sysu-blackbear/archive/2013/02/12/2910190.html
35 template<class T>

36 void BindaryTree<T>::preOder(BindaryTree<T>* root)

37 {

38 if (root) {

39 cout<<root->data<<" ";

40 preOder(root->left);

41 preOder(root->right);

42 } else {

43 return;

44 }

45 }

46

47 template<class T>

48 void BindaryTree<T>::inOrder(BindaryTree<T>* root)

49 {

50 if (root) {

51 inOder(root->left);

52 cout<<root->data<<" ";

53 inOder(root->right);

54 }

55 }

56

57 template<class T>

58 void BindaryTree<T>::postOder(BindaryTree<T>* root)

59 {

60 if (root) {

61 postOder(root->left);

62 postOder(root->right);

63 cout<<root->data<<" ";

64 }

65 }

66

67 template<class T>

68 void BinaryTree<T>::levelOrder(BindaryTree<T>* root)

69 {

70 deque<BinaryTreeNode<T>* > q;

71 q.push_back(root);

72 BinrayTreeNode<T>* t = root;

73 while (t) {

74 cout<<<t->data<<" ";

75 if (t->left) .push_back(t->left);

76 if (t->right) q.push_back(t->right);

77 q.pop_front();

78 t = q.front();

79 }

80 }

82 template<class T>

83 BinarySearchTree<T>::BinarySearchTree()

84 {

85 root = NULL;

86 }

87

88 template<class T>

89 BinarySearchTree<T>::BinarySearchTree(const BinarySearchTree& rhs)

90 {

91 root = NULL:

92 *this = rhs;

93 }

94

95 template<class T>

96 BinarySearchTree<T>::~BinarySearchTree()

97 {

98 makeEmpty();

99 }

100

101

102 template<class T>

103 const T& BinarySearchTree<T>::findMin() const

104 {

105 if (!isEmpty()) {

106 BinaryTreeNode<T>* r = root;

107 while (r->left)

108 r = r->left;

109 return r->data;

110 }

111 return 0;

112 }

113

114 template<class T>

115 const T& BinarySearchTree<T>::findMax() const

116 {

117 if (!isEmpty()) {

118 BinaryTreeNode<T>* r= root;

119 while (r->right)

120 r = r->right;

121 return r->data;

122 }

123 return 0;

124 }

126 template<class T>

127 void BinarySearchTree<T>::insert(const T& x)

128 {

129 BinaryTreeNode<T>* node = root;

130 while (node) {

131 if (x < node->data)

132 node = node->left;

133 else if (x > node->right)

134 node = node->right;

135 }

136 node = new BinaryTreeNode<T>(x);

137 }

138 template<class T>

139 void BinarySearchTree<T>::remove(const T& x)

140 {

141 if (root == NULL)

142 return;

143 BinaryTreeNode<T>* node = root;

144 while (node) {

145 if (x < node->left)

146 node = node->left;

147 else if (x > node->right)

148 node = node->right;

149 else if ( node->left && node->right) {

150 node->data = findMin(node->right);

151 //then remove that node in tree

152 } else {

153 BinaryTreeNode<T>* n1 = node;

154 node = (node->left) ? t->left:(node->right) ? t->right : NULL;

155 delete n1;

156 }

157 }

158 }

159

160 template<class T>

161 bool BinarySearchTree<T>::contain(const T& x) const

162 {

163 BinaryTreeNode<T>* node = root;

164 while (node) {

165 if (x < node->data)

166 node = node->left;

167 else if (x > node->right)

168 node = node->rigth;

169 else

170 return true;

171 }

172 return false;

173 }

175 template<typename T>

176 static void makeEmpty(BinaryTreeNode<T>* root)

177 {

178 if (root == NULL)

179 return;

180 if (root->left)

181 makeEmpty(root->left);

182 if (root->right)

183 makeEmpty(root->right);

184 delete root;

185 root = NULL;

186 }

187 template<class T>

188 void BinarySearchTree<T>::makeEmpty()

189 {

190

191 makeEmpty(root);

192 }

193

194 template<typename T>

195 void BFS(int v, int reach [], int lable)

196 {

197 deque<int> q;

198 q.push_back(v);

199 reach[v] = label;

200 while (q.size()) {

201 int w = q.front();

202 for (int i = 1; i <= n; i++) {

203 if (a[w][i] != NoEdge && reach[i] !=label) {

204 q.push_back(i);

205 reach[i] = label;

206 }

207 }

208 q.pop_front();

209

210 }

211 }

212

213 template<typename T>

214 void BFS1(int v, int reach[], int label)

215 {

216 deque<int> q;

217 q.push_back(v);

218 reach[v] = label;

219 while (q.size()) {

220 int w = q.front();

221 ChainNode<int>* p = H[v].first;

222 while (p != NULL) {

223 if (reach[p->data] != label) {

224 q.push_back(p->data);

225 reach[p->data] = label;

226 }

227 p = p->link;

228 }

229 q.pop_front();

230 }

233 template<typename T>

234 void DFS(int v, int reach[], int label)

235 {

236 reach[v]= label;

237 for (int i = 1; i <= n; i++)

238 {

239 if (a[v][i] != NoEdge && reach[i] != label)

240 {

241 DFS(i, reach, label);

242 }

243 }

244 }

245

246

247

248 template<typename T>

249 bool findPath(int v, int &length, int w, int path[], int reach[])

250 {

251 reach[v] = 1;

252 for (int i =1; i <= n; i++) {

253

254 if (!reach[i] && a[v][i] != NoEdge) {

255 length++;

256 path[length] = i;

257 if (i == w)

258 return true;

259 if (findPath(a[v][i], length, w, path, reach))

260 return true;

261 length--;

262 }

263 }

264 return false;

265

266 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: