public class BST_Iterative {
private Node m_root;
///Binary Search Tree Find Iterative Solution
public Node findNode(int value){
Node cur = m_root;
while( cur != null){
int currval = cur.getValue();
if( currval == value) return cur;
if ( currval < value ) {
cur = cur.getRight();
}
else
cur = cur.getLeft();
}
return null;
}
///Binary Search Tree Insertion Iterative Solution
public void insertNode(int value){
if( m_root == null){
m_root = new Node(value, null, null);
return;
}
Node cur = m_root;
while( cur != null){
int currval = cur.getValue();
if( currval == value){
System.out.println("Node is exist already! ");
return;
}
else if ( currval < value){
if( cur.getRight() == null){
cur.setRight(new Node(value, null,null));
}
else
cur = cur.getRight();
}
else{
if( cur.getLeft() == null){
if( cur.getLeft() == null){
cur.setLeft(new Node(value,null,null));
}
else
cur = cur.getLeft();
}
}
}
}
///Binary Search Tree In-Order Traversal Iterative Solution
/*public void showInorder(){
Node root = m_root;
Node cur, pre;
if( root == null) return;
while( root != null){
if( root.getLeft() != null){
root = root.getLeft();
}
}
}
*/
}
BST_Iterative class in Java
最新推荐文章于 2020-07-10 00:47:16 发布