搜索二叉树之一种特殊的二叉树,其右子树值大于根节点,根节点值大于左子树。因此,BST的插入、删除都要维持二叉树的这一特性。利用这一特性也使BST的元素查找效率提高,同时,本代码也实现了BST的递归遍历和非递归遍历。代码如下:
import java.util.ArrayDeque;
public class StdBST
{
BSTNode root;
public StdBST()
{
root=null;
}
void visit(BSTNode p)
{
System.out.print(p.key+" ");
}
void insert(Object el)//插入元素
{
BSTNode p=root,pre=null;
while(p!=null)
{
pre=p;
if((int)el>(int)p.key)
p=p.right;
else p=p.left;
}//跳出循环时,pre指向叶子节点,p指向null
if(root==null)
root=new BSTNode(el);
else if((int)el>(int)pre.key)
pre.right=new BSTNode(el);
else
pre.left=new BSTNode(el);
}
void breadthFirst()//深度优先遍历
{
BSTNode p=root;
ArrayDeque<bstnode> que=new ArrayDeque<>();
if(p!=null)
{
que.offer(p);
while(!que.isEmpty())
{
p=(BSTNode) que.poll();
visit(p);
if(p.left!=null)
que.offer(p.left);
if(p.right!=null)
que.offer(p.right);
}
}
}
void preOrder(BSTNode p)//递归先序遍历
{
if(p!=null)
{
visit(p);
preOrder(p.left);
preOrder(p.right);
}
}
void inOrder(BSTNode p)//递归中序遍历
{
if(p!=null)
{
inOrder(p.left);
visit(p);
inOrder(p.right);
}
}
void postOrder(BSTNode p)//递归后序遍历
{
if(p!=null)
{
postOrder(p.left);
postOrder(p.right);
visit(p);
}
}
void iterativePreorder()//非递归先序遍历
{
BSTNode p=root;
if(p!=null)
{
ArrayDeque<bstnode> stack=new ArrayDeque<>();
stack.push(p);
while(!stack.isEmpty())
{
p=(BSTNode)stack.pop();
visit(p);
if(p.right!=null)
stack.push(p.right);
if(p.left!=null)
stack.push(p.left);
}
}
}
void iterativePostorder()//非递归后序遍历
{
BSTNode p=root,q=root;
ArrayDeque<bstnode> stack=new ArrayDeque<>();
while(p!=null)
{
for(;p.left!=null;p=p.left)
stack.push(p);
while(p!=null&&(p.right==null||p.right==q))
{
visit(p);
q=p;
if(stack.isEmpty())
return;
p=(BSTNode)stack.pop();
}
stack.push(p);
p=p.right;
}
}
void iterativeInorder()//非递归中序遍历
{
BSTNode p=root;
ArrayDeque<bstnode> stack=new ArrayDeque<>();
while(p!=null)
{
while(p!=null)
{
if(p.right!=null)
stack.push(p.right);
stack.push(p);
p=p.left;
}
p=(BSTNode)stack.pop();
while(!stack.isEmpty()&&p.right==null)
{
visit(p);
p=(BSTNode)stack.pop();
}
visit(p);
if(!stack.isEmpty())
p=(BSTNode)stack.pop();
else p=null;
}
}
BSTNode search(BSTNode p,Object el)//查找元素
{
while(p!=null)
{
if(p.key==el)
return p;
else if((int)el<(int)p.key)
p=p.left;
else p=p.right;
}
return null;
}
void deleteByMerging(Object el)//归并删除
{
BSTNode tmp,node,pre=null,p=root;
while(p!=null&&p.key!=el)
{
pre=p;
if((int)el>(int)p.key)
p=p.right;
else
p=p.left;
}
node=p;
if(p!=null&&p.key==el)
{
if(node.left==null)
node=node.right;
else if(node.right==null)
node=node.left;
else
{
tmp=node.left;
while(tmp.right!=null)
tmp=tmp.right;
tmp.right=node.right;
node=node.left;
}
if(p==root)
root=node;
else if(pre.left==p)
pre.left=node;
else
pre.right=node;
}
else if(root!=null)
System.out.println("key"+el+"在此树中不存在");
else
System.out.println("树为空");
}
public class BSTNode //数的节点内部类
{
Object key;
BSTNode left,right;
public BSTNode(Object key,BSTNode left,BSTNode right)
{
this.key=key;
this.left=left;
this.right=right;
}
public BSTNode(Object key)
{
this(key,null,null);
}
public BSTNode()
{
left=right=null;
}
}
public static void main(String[] args)
{
StdBST st=new StdBST();
st.insert(13);
st.insert(25);
st.insert(10);
st.insert(20);
st.insert(31);
st.insert(12);
st.insert(2);
st.insert(29);
st.insert(15);
st.insert(21);
st.insert(32);
st.insert(28);
st.insert(36);
st.deleteByMerging(12);
st.breadthFirst();
}
}
参考来源:数据结构与算法 Adam Drozdek