zoukankan      html  css  js  c++  java
  • java实现B树(二叉树)插入,删除

    B树(二叉搜索树)定义:
    1)、每个非叶子节点至多有两个子节点。
    2)、每个节点都存储关键字值。
    3)、其左子节点的关键字值小于该节点,且右子节点的关键字值大于或等于该节点。

    1. /**
    2. * 节点类
    3. */   
    4. class Node{   
    5. public int key;   
    6. public int data;   
    7. public Node leftChild;   
    8. public Node rightChild;   
    9.   
    10. public Node(int key, int data){   
    11. this.key = key;   
    12. this.data = data;   
    13. this.leftChild = null;   
    14. this.rightChild = null;   
    15. }   
    16.   
    17. public void display(){   
    18. System.out.println("key: " + key + ", data: " + data);   
    19. }   
    20. }   
    21.   
    22. /**
    23. * B树类
    24. */   
    25. class Tree{   
    26. public Node root;   
    27.   
    28. public void insert(int key, int data){   
    29. Node newNode = new Node(key, data);   
    30.   
    31. if (root == null){   
    32. root = newNode;   
    33. }else{   
    34. Node current = root;   
    35. Node parent = null;   
    36. while (true){   
    37. parent = current;   
    38. if (key < current.key){   
    39. current = current.leftChild;   
    40. if (current == null){   
    41. parent.leftChild = newNode;   
    42. return;   
    43. }   
    44. }else{   
    45. current = current.rightChild;   
    46. if (current == null){   
    47. parent.rightChild = newNode;   
    48. return;   
    49. }   
    50. }   
    51. }   
    52. }   
    53. }   
    54.   
    55. /** 只实现有一个节点的删除 */   
    56. public boolean delete(int key){   
    57. Node current = root;   
    58. Node parent = null;   
    59. boolean isLeftChild = false;   
    60.   
    61. while (current.key != key){   
    62. parent = current;   
    63. if (key < current.key){   
    64. current = current.leftChild;   
    65. isLeftChild = true;   
    66. }else{   
    67. current = current.rightChild;   
    68. isLeftChild = false;   
    69. }   
    70. }   
    71.   
    72. if (current == null){   
    73. return false;   
    74. }   
    75.   
    76. /** 无子节点 */   
    77. if (current.leftChild == null && current.rightChild == null){   
    78. if (current == root){   
    79. root = null;   
    80. }else if (isLeftChild){   
    81. parent.leftChild = null;   
    82. }else{   
    83. parent.rightChild = null;   
    84. }   
    85. }   
    86. /** 仅有右节点 */   
    87. else if ((current.leftChild == null && current.rightChild != null)){   
    88. if (current == root){   
    89. root = current.rightChild;   
    90. }else if (isLeftChild){   
    91. parent.leftChild = current.rightChild;   
    92. }else{   
    93. parent.rightChild = current.rightChild;   
    94. }   
    95. }else if ((current.leftChild != null && current.rightChild == null)){   
    96. if (current == root){   
    97. root = null;   
    98. }else if (isLeftChild){   
    99. parent.leftChild = current.leftChild;   
    100. }else{   
    101. parent.rightChild = current.leftChild;   
    102. }   
    103. }   
    104. return true;   
    105. }   
    106.   
    107. public Node find(int key){   
    108. Node current = root;   
    109. while (current != null){   
    110. if (current.key == key){   
    111. break;   
    112. }else if (key < current.key){   
    113. current = current.leftChild;   
    114. }else{   
    115. current = current.rightChild;   
    116. }   
    117. }   
    118.   
    119. return current;   
    120. }   
    121.   
    122. /** 中序 */   
    123. public void inOrder(Node localNode){   
    124. if (localNode   != null){   
    125. inOrder(localNode.leftChild);   
    126. System.out.println("key: " + localNode.key + ", data: " + localNode.data);   
    127. inOrder(localNode.rightChild);   
    128. }   
    129.   
    130. }   
    131.   
    132. }   
    133.   
    134. public class BTree {   
    135.   
    136. /**
    137. * @param args
    138. */   
    139. public static void main(String[] args) {   
    140. // TODO Auto-generated method stub   
    141. Tree newTree = new Tree();   
    142. newTree.insert(5, 5);   
    143. newTree.insert(1, 1);   
    144. newTree.insert(2, 2);   
    145. newTree.insert(8, 8);   
    146. newTree.insert(9, 9);   
    147. newTree.insert(7, 7);   
    148.   
    149. newTree.delete(1);   
    150. newTree.inOrder(newTree.root);   
    151. }   
    152.   
    153. }   
    154. 转自:http://zfsn.javaeye.com/blog/409692
  • 相关阅读:
    使用Docfx生成项目文档
    代码性能优化-----减少数据库读取次数
    代码性能优化-----前端页面异步实现
    代码性能优化——task
    SVN批处理
    性能优化
    provider 设计模式
    【IObit】五大软件激活码( Advanced Systemcare....)
    关于 facebook
    关于 Google 与 Chrome
  • 原文地址:https://www.cnblogs.com/xinzhuangzi/p/4100619.html
Copyright © 2011-2022 走看看