[关闭]
@iwktd981220 2017-11-24T12:24:18.000000Z 字数 3144 阅读 324

学习Binary search tree

笔记


  1. 貌似我是不需要传入一个结点的,因为那个结点直接就引用了这个函数。
  2. 每一次调用该对象的node,应该是不需要用this->来指向的,因为默认是对象的node
  3. 对于transplate 声明为bst,但是我传入的是两个地址....应该是有问题吧
  4. 我感觉对于问题3,我没有好好的理清楚里面每个结点,究竟是一个对象还是....
  5. 对于类里面的一个函数,默认是调用里面的元素
  6. 对于每一个使用了模板的类型,在声明使用的时候,都要加上

脑子有洞!!!什么都变成了类里面的函数???!!!

继续填坑
1. 面向对象,说的是面向一个类中同一样的不同对象使用相同的方法去实现一个目的。[好像这样理解并没有什么问题。
2. 从这个角度出发的话,对于一个二叉查找树,

初步构想的功能:

函数 功能(方法) 类型
void inOrder(); 把所有的结点的数按照从小到大输出 树类
list_t* search(type k); 查找出key为k的结点 结点类
list_t* searchIteration(type k); 使用迭代完成上面的那个问题 结点类
list_t* minimun(); 找出树中最小的结点 树类
list_t* maximun(); 找出树中最大的结点 树类
list_t* successor(); 找出此结点的继承人 结点类
void insert(type num); 插入一个数据在树中 结点类
void transplant(list_t <\type> *y,list_t<\type> *z); 把子树y,移植到z的位置 结点类
void Delete(type num); 删除树中某一个结点 结点类
Bst(); 初始化一个树 树类
Bst(list_t *n); 以n为根生成子树 树类
~Bst(); 释放内存什么的 结点类
void print(list_t *p); 把整个树的函数打印???[怎么又来了

貌似这个link在分清结点类和二叉树类这方面比较清晰。
因为主要是分好两个类各自的职能,结点类负责什么,二叉树类负责什么。
可能这就是所谓的代码设计的重要性啊

失败文件:见link

成功:BSTreeDone.cpp

  1. #include <iostream>
  2. using namespace std;
  3. template <typename T>
  4. class Node{
  5. public:
  6. Node();
  7. void DeleteNodesRecursion();
  8. T key;
  9. Node *left;
  10. Node *right;
  11. Node *parent;
  12. Node<T>* search(T num);
  13. Node<T>* minimun();
  14. Node<T>* maximun();
  15. void print();
  16. private:
  17. };
  18. template <typename T>
  19. class BSTree {
  20. public:
  21. BSTree():root(nullptr){};
  22. ~BSTree();
  23. void insert(T num);
  24. void deleteNode(T num);
  25. void inOrder() ;
  26. private:
  27. Node<T> *root;
  28. void transplant(Node<T> *x,Node<T> *y);
  29. };
  30. template <typename T>
  31. Node<T>::Node() {
  32. key = 0;
  33. left = nullptr;
  34. right = nullptr;
  35. }
  36. template <typename T>
  37. void Node<T>::DeleteNodesRecursion() {
  38. if (this) {
  39. Node<T> *l = this->left;
  40. Node<T> *r = this->right;
  41. delete this;
  42. l->DeleteNodesRecursion();
  43. r->DeleteNodesRecursion();
  44. }
  45. }
  46. template <typename T>
  47. Node<T>* Node<T>::search(T num) {
  48. if (this == NULL || this->key == num ) {
  49. return this;
  50. }
  51. if (this->key > num) {
  52. return left->search(num);
  53. }
  54. else {
  55. return right->search(num);
  56. }
  57. }
  58. template <typename T>
  59. Node<T>* Node<T>::minimun() {
  60. Node<T> *temp;
  61. temp = this;
  62. while (temp->left != NULL) {
  63. temp = (temp->left);
  64. }
  65. return temp;
  66. }
  67. template <typename T>
  68. Node<T>* Node<T>::maximun() {
  69. while (this->right) {
  70. this = this->right;
  71. }
  72. return this;
  73. }
  74. template <typename T>
  75. void Node<T>::print() {
  76. if(left != NULL)left->print();
  77. cout<<key;
  78. if(right != NULL)right->print();
  79. }
  80. template <typename T>
  81. BSTree<T>::~BSTree() {
  82. root->DeleteNodesRecursion();
  83. }
  84. template <typename T>
  85. void BSTree<T>::insert(T num) {
  86. Node<T> *z = new Node<T>;
  87. z->key = num;
  88. Node<T> *y = new Node<T>;
  89. y = nullptr;
  90. Node<T> *x = root;
  91. while (x != NULL) {
  92. y = x;
  93. if (x->key > z->key) {
  94. x= x->left;
  95. }
  96. else x = x->right;
  97. }
  98. z->parent = y;
  99. if (y == NULL) {
  100. root = z;
  101. }
  102. else if (y->key > z->key) {
  103. y->left = z;
  104. }
  105. else y->right = z;
  106. }
  107. template <typename T>
  108. void BSTree<T>::deleteNode(T num) {
  109. Node<T> *n = root->search(num);
  110. if (n->left == NULL) {
  111. transplant(n,n->right);
  112. }
  113. else if (n->right ==NULL ) {
  114. transplant(n,n->left);
  115. }
  116. else {
  117. Node<T> *min;
  118. min = n->right->minimun();
  119. if (min->parent != n) {
  120. transplant(min,min->right);
  121. min->right = n->right;
  122. min->right->parent = min;
  123. }
  124. transplant(n,min);
  125. min->left = n->left;
  126. min->left->parent = min;
  127. }
  128. }
  129. template <typename T>
  130. void BSTree<T>::inOrder() {
  131. root->print();
  132. }
  133. template <typename T>
  134. void BSTree<T>::transplant(Node<T> *x ,Node<T> *y) {
  135. if (x->parent == NULL) {
  136. x = y;
  137. }
  138. else if (x->parent->left == x) {
  139. x->parent->left = y;
  140. }
  141. else x->parent->right = y;
  142. if (y != NULL) {
  143. y->parent = x->parent;
  144. }
  145. }
  146. int main() {
  147. BSTree<int> tree;
  148. tree.insert(2);
  149. tree.insert(4);
  150. tree.insert(1);
  151. tree.insert(3);
  152. tree.inOrder();
  153. tree.deleteNode(4);
  154. tree.insert(5);
  155. tree.inOrder();
  156. return 0;
  157. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注