Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. public class BST
  2. {
  3. // This is the root node, which starts off as null
  4. // when the BST is empty.
  5. BSTNode m_objRootNode;
  6.  
  7. // Class constructor.
  8. public BST()
  9. {
  10. // Not really necessary, provided for clarity.
  11. m_objRootNode = null;
  12. }
  13.  
  14. // Method to see if the tree is empty.
  15. public boolean IsEmpty()
  16. {
  17. // Return a boolean indicating whether the
  18. // three is empty or not.
  19. return( m_objRootNode == null );
  20. }
  21.  
  22. /* Functions to search for an element */
  23. public BSTNode Search( int nKeyValue )
  24. {
  25. return( Search( m_objRootNode, nKeyValue ) );
  26. }
  27.  
  28. // Method to search for an element recursively.
  29. private BSTNode Search( BSTNode objNode, int nKeyValue )
  30. {
  31.  
  32. if( objNode == null )
  33. {
  34. return( null );
  35. }
  36.  
  37. // First, we get the key value for this node.
  38. int nThisKeyValue = objNode.GetKeyValue();
  39.  
  40. // See if the passed in key value is less. If so,
  41. // this indicates that we need to go left.
  42. if( nKeyValue < nThisKeyValue )
  43. {
  44. // Get the left node object reference so we
  45. // can walk down it.
  46. objNode = objNode.GetLeftNode();
  47. }
  48.  
  49. // See if the passed in key value is greater. If so,
  50. // this indicates that we need to go right.
  51. else if( nKeyValue > nThisKeyValue )
  52. {
  53. // Get the right node object reference so we
  54. // can walk down it.
  55. objNode = objNode.GetRightNode();
  56. }
  57.  
  58. // Here we have found the node with the key
  59. // value that was passed in.
  60. else
  61. {
  62. return( objNode );
  63. }
  64.  
  65. // Now call Search recursively.
  66. return( Search( objNode, nKeyValue ) );
  67. }
  68.  
  69. // This method inserts a node based on the key value.
  70. public void Insert( int nKeyValue )
  71. {
  72. // The root node is returned to m_objRootNode from Insert()
  73. m_objRootNode = Insert( nKeyValue, m_objRootNode );
  74. }
  75.  
  76. // Class protected (internal) method to insert nodes. This method
  77. // will be called recursively.
  78. protected BSTNode Insert( int nKeyValue, BSTNode objNode )
  79. {
  80.  
  81. // This node is null and simply needs to be allocated.
  82. if( objNode == null )
  83. {
  84. objNode = new BSTNode( nKeyValue );
  85. }
  86.  
  87. // Here we need to walk left.
  88. else if( nKeyValue < objNode.GetKeyValue() )
  89. {
  90. // Set the left node of this object by recursively walking left.
  91. objNode.SetLeftNode( Insert( nKeyValue, objNode.GetLeftNode() ) );
  92. }
  93.  
  94. // Here we need to talk right.
  95. else if( nKeyValue > objNode.GetKeyValue() )
  96. {
  97. // Set the right node of this object by recursively walking right.
  98. objNode.SetRightNode( Insert( nKeyValue, objNode.GetRightNode() ) );
  99. }
  100. //System.out.print(objNode.GetKeyValue()+ "\n");
  101. return( objNode );
  102. }
  103. public boolean delete( int x )
  104. {
  105. boolean deleted = delete( x, m_objRootNode );
  106. System.out.print(deleted);
  107. return deleted;
  108. }
  109. private boolean delete( int x, BSTNode t )
  110. {
  111. boolean deleted = false;
  112. if( t == null )
  113. return false; // Item not found; do nothing
  114.  
  115. //finds node with key, removes node
  116.  
  117. if( x > t.GetKeyValue() )
  118. {
  119. delete( x, t.GetRightNode() );
  120. }
  121. else if( x < t.GetKeyValue() )
  122. {
  123. delete( x, t.GetLeftNode() );
  124. }
  125. else
  126. {
  127. System.out.print("hear\n");
  128.  
  129. deleted = t.remove(x, t);
  130. System.out.print("hear\n");
  131. return deleted;
  132. }
  133. //return false;
  134. return deleted;
  135. }
  136. public BSTNode getRootNode()
  137. {
  138. return m_objRootNode;
  139. }
  140.  
  141. public int getSubTreeSize()
  142. {
  143. int i = m_objRootNode.getSubtreeSize(m_objRootNode);
  144. return i;
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement