zoukankan      html  css  js  c++  java
  • 算法导论-----------二叉搜索树【转】

    转自:https://blog.csdn.net/chenxun_2010/article/details/41709801

    先上二叉树查找树的删除的代码,因为删除是二叉查找树最复杂的操作:

     

    1. int BinarySearchTree<T>::tree_remove(const T& elem)
    2. {
    3. BinarySearchTreeNode<T> *z = tree_search(elem);//根据元素查找到要删除的节点
    4. BinarySearchTreeNode<T> *x, *y;
    5.  
    6. if (z != NULL)
    7. {
    8. //用y来表示实际要删除的节点
    9. if (z->left == NULL || z->right == NULL)//最多只有一个儿子节,要么没有儿子节点
    10. y = z;
    11. else
    12. y = tree_search(tree_successor(elem));//有两个儿子的时候实际删除的是后继节点
    13.  
    14. //因为有上面的if语句,所以y要么只有一个儿子,要么没有儿子。后继节点只有右儿子或者没有儿子
    15. //所以x要么是儿子节点,要么是空节点
    16. if (y->left != NULL)
    17. x = y->left;
    18. else
    19. x = y->right;
    20.  
    21. if (x != NULL)//判断y节点有没有儿子节点,有的花就把y节点的父节点变成x的父节点。
    22. x->parent = y->parent;
    23.  
    24. //y是根节点或者不是根节点的情况
    25. if (y->parent == NULL)
    26. root = x;
    27. else if (y == y->parent->left)//如果y节点不是根节点的情况该怎么处理呢?
    28. y->parent->left = x;
    29. else
    30. y->parent->right = x;
    31.  
    32. //处理后继节点的情况,因为y表示后继的时候y!=z;
    33. if (y != z)
    34. z->elem = y->elem;
    35. delete y;
    36. }
    37.  
    38. return -1;
    39. }


    二叉查找树的概念及操作。主要内容包括二叉查找树的性质,如何在二叉查找树中查找最大值、最小值和给定的值,如何找出某一个元素的前驱和后继,如何在二叉查找树中进行插入和删除操作。在二叉查找树上执行这些基本操作的时间与树的高度成正比,一棵随机构造的二叉查找树的期望高度为O(lgn),从而基本动态集合的操作平均时间为θ(lgn)。

    1、二叉查找树

      二叉查找树是按照二叉树结构来组织的,因此可以用二叉链表结构表示。二叉查找树中的关键字的存储方式满足的特征是:设x为二叉查找树中的一个结点。如果y是x的左子树中的一个结点,则key[y]≤key[x]。如果y是x的右子树中的一个结点,则key[x]≤key[y]。根据二叉查找树的特征可知,采用中根遍历一棵二叉查找树,可以得到树中关键字有小到大的序列。

    一棵二叉树查找及其中根遍历结果如下图所示:

    书中给出了一个定理:如果x是一棵包含n个结点的子树的根,则其中根遍历运行时间为θ(n)。

    问题:二叉查找树性质与最小堆之间有什么区别?能否利用最小堆的性质在O(n)时间内,按序输出含有n个结点的树中的所有关键字?

    2、查询二叉查找树

      二叉查找树中最常见的操作是查找树中的某个关键字,除了基本的查询,还支持最大值、最小值、前驱和后继查询操作,书中就每种查询进行了详细的讲解。

    (1)查找SEARCH

      在二叉查找树中查找一个给定的关键字k的过程与二分查找很类似,根据二叉查找树在的关键字存放的特征,很容易得出查找过程:首先是关键字k与树根的关键字进行比较,如果k大比根的关键字大,则在根的右子树中查找,否则在根的左子树中查找,重复此过程,直到找到与遇到空结点为止。例如下图所示的查找关键字13的过程:(查找过程每次在左右子树中做出选择,减少一半的工作量)

    书中给出了查找过程的递归和非递归形式的伪代码:

     

    1. TREE_SEARCH(x,k)
    2. if x=NULL or k=key[x]
    3. then return x
    4. if(k<key[x])
    5. then return TREE_SEARCH(left[x],k)
    6. else
    7. then return TREE_SEARCH(right[x],k)

    1. ITERATIVE_TREE_SEARCH(x,k)
    2. while x!=NULL and k!=key[x]
    3. do if k<key[x]
    4. then x=left[x]
    5. else
    6. then x=right[x]
    7. return x


    (2)查找最大关键字和最小关键字

      根据二叉查找树的特征,很容易查找出最大和最小关键字。查找二叉树中的最小关键字:从根结点开始,沿着各个节点的left指针查找下去,直到遇到NULL时结束。如果一个结点x无左子树,则以x为根的子树中,最小关键字就是key[x]。查找二叉树中的最大关键字:从根结点开始,沿着各个结点的right指针查找下去,直到遇到NULL时结束。书中给出了查找最大最小关键字的伪代码:

     

    1. TREE_MINMUM(x)
    2.  
    3. while left[x] != NULL
    4.  
    5. do x=left[x]
    6.  
    7. return x

     

     

    1. TREE_MAXMUM(x)
    2.  
    3. while right[x] != NULL
    4.  
    5. do x= right[x]
    6.  
    7. return x


    (3)前驱和后继

      给定一个二叉查找树中的结点,找出在中序遍历顺序下某个节点的前驱和后继。如果树中所有关键字都不相同,则某一结点x的前驱就是小于key[x]的所有关键字中最大的那个结点,后继即是大于key[x]中的所有关键字中最小的那个结点。根据二叉查找树的结构和性质,不用对关键字做任何比较,就可以找到某个结点的前驱和后继。

      查找前驱步骤:先判断x是否有左子树,如果有则在left[x]中查找关键字最大的结点,即是x的前驱。如果没有左子树,则从x继续向上执行此操作,直到遇到某个结点是其父节点的右孩子结点。例如下图查找结点7的前驱结点6过程:

      查找后继步骤:先判断x是否有右子树,如果有则在right[x]中查找关键字最小的结点,即使x的后继。如果没有右子树,则从x的父节点开始向上查找,直到遇到某个结点是其父结点的左儿子的结点时为止。例如下图查找结点13的后继结点15的过程:

    书中给出了求x结点后继结点的伪代码:

     

    1. TREE_PROCESSOR(x)
    2. if right[x] != NULL
    3. then return TREE_MINMUM(right(x))
    4. y=parent[x]
    5. while y!= NULL and x ==right[y]
    6. do x = y
    7. y=parent[y]
    8. return y


    定理:对一棵高度为h的二叉查找,动态集合操作SEARCH、MINMUM、MAXMUM、SUCCESSOR、PROCESSOR等的运行时间均为O(h)。

    3、插入和删除

      插入和删除会引起二叉查找表示的动态集合的变化,难点在在插入和删除的过程中要保持二叉查找树的性质。插入过程相当来说要简单一些,删除结点比较复杂。

    (1)插入

      插入结点的位置对应着查找过程中查找不成功时候的结点位置,因此需要从根结点开始查找带插入结点位置,找到位置后插入即可。下图所示插入结点过程:

    书中给出了插入过程的伪代码:

     

    1. TREE_INSERT(T,z)
    2. y = NULL;
    3. x =root[T]
    4. while x != NULL
    5. do y =x
    6. if key[z] < key[x]
    7. then x=left[x]
    8. else x=right[x]
    9. parent[z] =y
    10. if y=NULL
    11. then root[T] =z
    12. else if key[z]>key[y]
    13. then keft[y] = z
    14. else right[y] =z


    插入过程运行时间为O(h),h为树的高度。

    (2)删除

      从二叉查找树中删除给定的结点z,分三种情况讨论:

    <1>结点z没有左右子树,则修改其父节点p[z],使其为NULL。删除过程如下图所示:

    <2>如果结点z只有一个子树(左子树或者右子树),通过在其子结点与父节点建立一条链来删除z。删除过程如下图所示:

    <3>如果z有两个子女,则先删除z的后继y(y没有左孩子),在用y的内容来替代z的内容。

    书中给出了删除过程的伪代码:

     

    1. TREE_DELETE(T,z)
    2. if left[z] ==NULL or right[z] == NULL
    3. then y=z
    4. else y=TREE_SUCCESSOR(z)
    5. if left[y] != NULL
    6. then x=left[y]
    7. else x=right[y]
    8. if x!= NULL
    9. then parent[x] = parent[y]
    10. if p[y] ==NULL
    11. then root[T] =x
    12. else if y = left[[prarnt[y]]
    13. then left[parent[y]] = x
    14. else right[parent[y]] =x
    15. if y!=z
    16. then key[z] = key[y]
    17. copy y's data into z
    18. return y

    定理:对高度为h的二叉查找树,动态集合操作INSERT和DELETE的运行时间为O(h)。

    4、实现测试

      采用C++语言实现一个简单的二叉查找树,支持动态集合的基本操作:search、minmum、maxmum、predecessor、successor、insert和delete。设计的二叉查找树结构如下所示:

     

    1. template<class T>
    2. class BinarySearchTreeNode
    3. {
    4. public:
    5. T elem;
    6. BinarySearchTreeNode<T> *parent;
    7. BinarySearchTreeNode<T> *left;
    8. BinarySearchTreeNode<T> *right;
    9. };
    10.  
    11. template<class T>
    12. class BinarySearchTree
    13. {
    14. public:
    15. BinarySearchTree();
    16. void tree_insert(const T& elem);
    17. int tree_remove(const T& elem);
    18. BinarySearchTreeNode<T> *tree_search(const T& elem) const;
    19. T tree_minmum(BinarySearchTreeNode<T>* root) const;
    20. T tree_maxmum(BinarySearchTreeNode<T>* root) const;
    21. T tree_successor(const T& elem) const;
    22. T tree_predecessor(const T& elem) const;
    23. int empty() const;
    24. void inorder_tree_walk() const;
    25. BinarySearchTreeNode<T>* get_root() { return root; }
    26.  
    27. private:
    28. BinarySearchTreeNode<T>* root;
    29. };


     完整程序如下所示:

     

    1. #include<iostream>
    2. #include<stack>
    3.  
    4. using namespace std;
    5. /*-----------------------------------------------------------------------------------------------*/
    6. /*采用C++语言实现一个简单的二叉查找树,支持动态集合的基本操作: */
    7. /*search、minmum、maxmum、predecessor、successor、insert和delete。设计的二叉查找树结构如下所示: */
    8. /*------------------------------------------------------------------------------------------------*/
    9.  
    10. template<class T>
    11. class BinarySearchTreeNode
    12. {
    13. public:
    14. T elem;
    15. BinarySearchTreeNode<T> *parent;
    16. BinarySearchTreeNode<T> *left;
    17. BinarySearchTreeNode<T> *right;
    18. };
    19.  
    20. template<class T>
    21. class BinarySearchTree
    22. {
    23. public:
    24. BinarySearchTree();
    25. void tree_insert(const T& elem);
    26. int tree_remove(const T& elem);
    27. BinarySearchTreeNode<T> *tree_search(const T& elem) const;
    28. T tree_minmum(BinarySearchTreeNode<T>* root) const;
    29. T tree_maxmum(BinarySearchTreeNode<T>* root) const;
    30. T tree_successor(const T& elem) const;
    31. T tree_predecessor(const T& elem) const;
    32. int empty() const;
    33. void inorder_tree_walk() const;
    34. BinarySearchTreeNode<T>* get_root() { return root; }
    35.  
    36. private:
    37. BinarySearchTreeNode<T>* root;
    38. };
    39.  
    40. //构造函数,初始化二叉查找树。
    41. template <class T>
    42. BinarySearchTree<T>::BinarySearchTree()
    43. {
    44. root = NULL;
    45. }
    46.  
    47.  
    48. template <class T>
    49. void BinarySearchTree<T>::tree_insert(const T& elem)
    50. {
    51. if (!empty())
    52. {
    53. BinarySearchTreeNode<T> *p_node = root;
    54. BinarySearchTreeNode<T> *q_node = NULL;
    55. BinarySearchTreeNode<T> *new_node = new BinarySearchTreeNode<T>;
    56.  
    57. new_node->elem = elem;
    58. new_node->left = NULL;
    59. new_node->right = NULL;
    60. new_node->parent = NULL;
    61.  
    62. while (p_node)
    63. {
    64. q_node = p_node;
    65. if (p_node->elem > elem)
    66. p_node = p_node->left;
    67. else
    68. p_node = p_node->right;
    69. }//当p_node为空的时候,q_node正好是正确的插入位置的父节点,且q_node是叶节点.
    70.  
    71. if (q_node->elem > elem)
    72. q_node->left = new_node;
    73. else
    74. q_node->right = new_node;
    75. new_node->parent = q_node;
    76. }
    77. else
    78. {
    79. root = new BinarySearchTreeNode<T>;
    80. root->elem = elem;
    81. root->parent = NULL;
    82. root->left = NULL;
    83. root->right = NULL;
    84. }
    85. }
    86.  
    87. //二叉查找树节点的删除
    88. template <class T>
    89. int BinarySearchTree<T>::tree_remove(const T& elem)
    90. {
    91. BinarySearchTreeNode<T> *z = tree_search(elem);
    92. BinarySearchTreeNode<T> *x, *y;
    93.  
    94. if (z != NULL)
    95. {
    96. //用y来表示实际要删除的节点
    97. if (z->left == NULL || z->right == NULL)//最多只有一个儿子节,要么没有儿子节点
    98. y = z;
    99. else
    100. y = tree_search(tree_successor(elem));//有两个儿子的时候实际删除的是后继节点
    101.  
    102. //因为有上面的if语句,所以y要么只有一个儿子,要么没有儿子。后继节点只有右儿子或者没有儿子
    103. //所以x要么是儿子节点,要么是空节点
    104. if (y->left != NULL)
    105. x = y->left;
    106. else
    107. x = y->right;
    108.  
    109. if (x != NULL)
    110. x->parent = y->parent;
    111.  
    112. if (y->parent == NULL)
    113. root = x;
    114. else if (y == y->parent->left)
    115. y->parent->left = x;
    116. else
    117. y->parent->right = x;
    118.  
    119. //处理后继节点的情况,因为y表示后继的时候y!=z;
    120. if (y != z)
    121. z->elem = y->elem;
    122. delete y;
    123. }
    124.  
    125. return -1;
    126. }
    127.  
    128.  
    129. // BinarySearchTreeNode<T>* 返回类型,返回查找元素elem的节点
    130. template <class T>
    131. BinarySearchTreeNode<T>* BinarySearchTree<T>::tree_search(const T& elem) const
    132. {
    133. BinarySearchTreeNode<T> *pnode = root;
    134. while (pnode)
    135. {
    136. if (pnode->elem == elem)
    137. break;
    138. else if (pnode->elem > elem)
    139. pnode = pnode->left;
    140. else
    141. pnode = pnode->right;
    142. }
    143.  
    144. return pnode;
    145. }
    146.  
    147. //返回最小关键字的元素,可以参考书上用递归方法的写
    148. template <class T>
    149. T BinarySearchTree<T>::tree_minmum(BinarySearchTreeNode<T>* root) const
    150. {
    151. BinarySearchTreeNode<T> *pnode = root;
    152.  
    153. while (pnode->left)
    154. pnode = pnode->left;
    155. return pnode->elem;
    156. }
    157.  
    158. //返回最大关键字的元素,可以改用递归,不过效率降低
    159. template <class T>
    160. T BinarySearchTree<T>::tree_maxmum(BinarySearchTreeNode<T>* root) const
    161. {
    162. BinarySearchTreeNode<T> *pnode = root;
    163.  
    164. while (pnode->right != NULL)
    165. pnode = pnode->right;
    166.  
    167. return pnode->elem;
    168. }
    169.  
    170.  
    171. //后继节点
    172. template <class T>
    173. T BinarySearchTree<T>::tree_successor(const T& elem) const
    174. {
    175. BinarySearchTreeNode<T>* pnode = tree_search(elem);
    176. BinarySearchTreeNode<T>* parentnode;
    177. if (pnode != NULL)
    178. {
    179. if (pnode->right)
    180. return tree_minmum(pnode->right);
    181. parentnode = pnode->parent;
    182. while (parentnode && pnode == parentnode->right)
    183. {
    184. pnode = parentnode;
    185. parentnode = parentnode->parent;
    186. }
    187. if (parentnode)
    188. return parentnode->elem;
    189. else
    190. return T();
    191. }
    192. return T();
    193. }
    194.  
    195. //前继节点
    196. template <class T>
    197. T BinarySearchTree<T>::tree_predecessor(const T& elem)const
    198. {
    199. BinarySearchTreeNode<T>* pnode = tree_search(elem);
    200. BinarySearchTreeNode<T>* parentnode;
    201. if (pnode != NULL)
    202. {
    203. if (pnode->right)
    204. return tree_maxmum(pnode->right);
    205. parentnode = pnode->parent;
    206. while (parentnode && pnode == parentnode->left)
    207. {
    208. pnode = parentnode;
    209. parentnode = pnode->parent;
    210. }
    211. if (parentnode)
    212. return parentnode->elem;
    213. else
    214. return T();
    215. }
    216. return T();
    217. }
    218.  
    219. template <class T>
    220. int BinarySearchTree<T>::empty() const
    221. {
    222. return (NULL == root);
    223. }
    224.  
    225.  
    226. //按照大小顺序输出二叉查找树,即中根遍历的方法输出二叉查找树.使用stack功能的实现。
    227. template <class T>
    228. void BinarySearchTree<T>::inorder_tree_walk() const
    229. {
    230. if (NULL != root)
    231. {
    232. stack<BinarySearchTreeNode<T>*> s;
    233. BinarySearchTreeNode<T> *P_temp;
    234. P_temp = root;
    235. while (NULL != P_temp || !s.empty())
    236. {
    237. if (NULL != P_temp)
    238. {
    239. s.push(P_temp);
    240. P_temp = P_temp->left;
    241. }
    242. else
    243. {
    244. P_temp = s.top();
    245. s.pop();
    246. cout << P_temp->elem << " ";
    247. P_temp = P_temp->right;
    248. }
    249. }
    250. }
    251. }
    252.  
    253. int main()
    254. {
    255. BinarySearchTree<int> bstree;
    256. BinarySearchTreeNode<int>* ptnode, *proot;
    257. bstree.tree_insert(32);
    258. bstree.tree_insert(21);
    259. bstree.tree_insert(46);
    260. bstree.tree_insert(54);
    261. bstree.tree_insert(16);
    262. bstree.tree_insert(38);
    263. bstree.tree_insert(70);
    264. cout << "inorder tree walk is: ";
    265. bstree.inorder_tree_walk();
    266. proot = bstree.get_root();
    267. cout << " max value is: " << bstree.tree_maxmum(proot) << endl;
    268. cout << "min value is: " << bstree.tree_minmum(proot) << endl;
    269. ptnode = bstree.tree_search(38);
    270. if (ptnode)
    271. cout << "the element 38 is exist in the binary tree. ";
    272. else
    273. cout << "the element 38 is not exist in the binary tree. ";
    274. cout << "the successor of 38 is: " << bstree.tree_successor(38) << endl;
    275. cout << "the predecessor of 38 is:" << bstree.tree_predecessor(38) << endl;
    276. if (bstree.tree_remove(46) == 0)
    277. cout << "delete 46 successfully" << endl;
    278. else
    279. cout << "delete 46 failed" << endl;
    280. cout << "inorder tree walk is: ";
    281. bstree.inorder_tree_walk();
    282. exit(0);
    283. }

    程序测试结果如下所示:


     

    二叉查找上各种基本操作的运行时间都是O(h),h为树的高度。但是在元素插入和删除过程中,树的高度会发生改变。如果n个元素按照严格增长的顺序插入,那个构造出的二叉查找树的高度为n-1。例如按照先后顺序插入7、15、18、20、34、46、59元素构造二叉查找树,二叉查找树结构如下所示:

    版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chenxun2009/article/details/41709801
  • 相关阅读:
    A1023 Have Fun with Numbers (20分)(大整数四则运算)
    A1096 Consecutive Factors (20分)(质数分解)
    A1078 Hashing (25分)(哈希表、平方探测法)
    A1015 Reversible Primes (20分)(素数判断,进制转换)
    A1081 Rational Sum (20分)
    A1088 Rational Arithmetic (20分)
    A1049 Counting Ones (30分)
    A1008 Elevator (20分)
    A1059 Prime Factors (25分)
    A1155 Heap Paths (30分)
  • 原文地址:https://www.cnblogs.com/sky-heaven/p/9358506.html
Copyright © 2011-2022 走看看