• 树——二叉查找树 - 有删除动作


     代码:

    1. #include
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. const int N=1e6+10;
    7. int l[N],r[N];
    8. int d[N],idx;
    9. char a[7];
    10. int n;
    11. int h;
    12. void intree(int x,int father)
    13. {
    14. if(x>d[father])
    15. {
    16. if(r[father]==-1)
    17. {
    18. r[father]=++idx;
    19. d[idx]=x;
    20. }else
    21. {
    22. intree(x,r[father]);
    23. }
    24. }else if(x
    25. {
    26. if(l[father]==-1)
    27. {
    28. l[father]=++idx;
    29. d[idx]=x;
    30. }else
    31. {
    32. intree(x,l[father]);
    33. }
    34. }else
    35. return;
    36. }
    37. bool get(int x)
    38. {
    39. int p=h;
    40. while(1)
    41. {
    42. if(x==d[p])return true;
    43. else if(x>d[p])
    44. {
    45. if(r[p]==-1)
    46. {
    47. return false;
    48. }else
    49. {
    50. p=r[p];
    51. }
    52. }else
    53. {
    54. if(l[p]==-1)
    55. {
    56. return false;
    57. }else
    58. {
    59. p=l[p];
    60. }
    61. }
    62. }
    63. return false;
    64. }
    65. int get_lmin(int p,int father)
    66. {
    67. if(r[p]==-1)
    68. {
    69. if(l[father]==p)
    70. {
    71. l[father]=l[p];
    72. }else
    73. {
    74. r[father]=l[p];
    75. }
    76. return p;
    77. }else
    78. {
    79. return get_lmin(r[p], p);
    80. }
    81. }
    82. int get_rmin(int p,int father)
    83. {
    84. if(l[p]==-1)
    85. {
    86. if(l[father]==p)
    87. {
    88. l[father]=r[p];
    89. }else
    90. {
    91. r[father]=r[p];
    92. }
    93. return p;
    94. }else
    95. {
    96. return get_rmin(l[p],p);
    97. }
    98. }
    99. void dele(int x,int p,int father)
    100. {
    101. if(d[p]==x)
    102. {
    103. if(l[p]!=-1)
    104. {
    105. d[p]=d[get_lmin(l[p],p)];
    106. }else if(r[p]!=-1)
    107. {
    108. d[p]=d[get_rmin(r[p],p)];
    109. }else if(r[p]==-1&&l[p]==-1)
    110. {
    111. if(d[l[father]]==x)
    112. {
    113. l[father]=-1;
    114. }else
    115. {
    116. r[father]=-1;
    117. }
    118. }
    119. }else if(x>d[p])
    120. {
    121. dele(x,r[p],p);
    122. }else
    123. {
    124. dele(x,l[p],p);
    125. }
    126. }
    127. void output()
    128. {
    129. queue<int>que;
    130. que.push(1);
    131. while(que.size())
    132. {
    133. int t=que.front();
    134. cout<' ';
    135. que.pop();
    136. if(l[t]!=-1)que.push(l[t]);
    137. if(r[t]!=-1)que.push(r[t]);
    138. }
    139. }
    140. int main()
    141. {
    142. int b;
    143. h=0;
    144. d[h]=1e9;
    145. memset(d,0x3f,sizeof d);
    146. memset(l,-1,sizeof l);
    147. memset(r,-1,sizeof r);
    148. scanf("%d",&n);
    149. while(n--)
    150. {
    151. scanf("%s%d",a+1,&b);
    152. if(a[1]=='i')
    153. {
    154. intree(b,h);
    155. }else if(a[1]=='f')
    156. {
    157. if(get(b))
    158. {
    159. puts("yes");
    160. }else
    161. puts("no");
    162. }else
    163. {
    164. // output();
    165. // cout<
    166. dele(b,l[h],h);
    167. //output();
    168. }
    169. }
    170. return 0;
    171. }

  • 相关阅读:
    应用使用Druid连接池经常性断链问题分析
    Log4j2
    【专题复习】树状数组
    「Redis」07 持久化操作(RDB、AOF)
    二叉树进阶——手撕二叉搜索树
    idea 设置文件忽略git版本控制
    Node.js的POST请求与响应编程
    arduino 天下第一(暴论) -- 智能猫眼与 SDDC 连接器移植到 arduino 上
    密码(6)
    快读《ASP.NET Core技术内幕与项目实战》WebApi3.1:WebApi最佳实践
  • 原文地址:https://blog.csdn.net/m0_62327332/article/details/126625754