二叉查找树的很多相关操作 包括递归和非递归,看代码。
- package com.meran.tree;
- public class TNode {
- int key;
- TNode left;
- TNode right;
- TNode parent;
- public TNode(int key,TNode left,TNode right,TNode parent){
- this.key=key;
- this.left=left;
- this.right=right;
- this.parent=parent;
- }
- public TNode(int key,TNode parent){
- this.key=key;
- this.left=null;
- this.right=null;
- this.parent=parent;
- }
- }
- package com.meran.tree;
- import java.util.LinkedList;
- public class BinarySearchTree {
- TNode root ;
- //非递归方法插入节点
- public void iterativeInsertNode(int i){
- TNode p =root,prev=null;
- while(p!=null){
- prev=p;
- if(i<p.key)
- p=p.left;
- else
- p=p.right;
- }
- if(root==null)
- root=new TNode(i,null);
- else
- if(prev.key<i){
- TNode temp=new TNode(i,prev);
- prev.right=temp;
- temp.parent=prev;
- }
- else{
- TNode temp=new TNode(i,prev);
- prev.left=temp;
- temp.parent=prev;
- }
- };
- //递归方式插入
- public void insertNode(int i){
- TNode prev=getPosition(i,root,root);
- if(prev==null)
- root=new TNode(i,null);
- else if(i<prev.key)
- prev.left=new TNode(i,prev);
- else
- prev.right=new TNode(i,prev);
- }
- private TNode getPosition(int i,TNode p,TNode prev){
- if(p!=null){
- prev=p;
- if(i<p.key)
- return getPosition(i,p.left,prev);
- else
- return getPosition(i,p.right,prev);
- }
- return prev;
- }
- public void preorder(TNode p){
- if(p!=null){
- visit(p);
- preorder(p.left);
- preorder(p.right);
- }
- }
- public void inorder(TNode p){
- if(p!=null){
- inorder(p.left);
- visit(p);
- inorder(p.right);
- }
- }
- public void postorder(TNode p){
- if(p!=null){
- postorder(p.left);
- postorder(p.right);
- visit(p);
- }
- }
- private void visit(TNode p){
- System.out.println(p.key);
- }
- //非递归查找
- public TNode iterativeSearch(int i){
- TNode p=root;
- while(p!=null){
- if(p.key==i)
- return p;
- else if(i<p.key)
- p=p.left;
- else
- p=p.right;
- }
- return null;
- }
- //递归查找
- public TNode search(TNode p,int i){
- if(p!=null){
- if(p.key==i)
- return p;
- else if(i<p.key)
- search(p.left,i);
- else
- search(p.right,i);
- }
- return null;
- }
- //非递归前序遍历
- public void iterativePreorder(){
- TNode p=root;
- LinkedList<TNode> list=new LinkedList<TNode>();
- if(p!=null){
- list.addFirst(p);
- while(!list.isEmpty()){
- p=list.poll();
- visit(p);
- if(p.right!=null)
- list.addFirst(p.right);
- if(p.left!=null)
- list.addFirst(p.left);
- }
- }
- }
- //非递归后序遍历
- public void iterativePostorder(){
- TNode p=root;
- TNode q=p;
- LinkedList<TNode> list=new LinkedList<TNode>();
- while(p!=null){
- for(;p.left!=null;p=p.left)
- list.addFirst(p);
- //无右子或者右子已打印
- while(p!=null&&(p.right==null||p.right==q)){
- visit(p);
- q=p;
- if(list.isEmpty())
- return;
- p=list.poll();
- }
- //处理右子
- list.addFirst(p);
- p=p.right;
- }
- }
- // 广度优先遍历
- public void breadthFirst(){
- TNode p=root;
- LinkedList<TNode> list=new LinkedList<TNode>();
- if(p!=null){
- list.addLast(p);
- while(!list.isEmpty()){
- p=list.poll();
- visit(p);
- if(p.left!=null)
- list.addLast(p.left);
- if(p.right!=null)
- list.addLast(p.right);
- }
- }
- }
- //返回最小值非递归
- public TNode iterativeminMum(){
- TNode p=root;
- while(p.left!=null){
- p=p.left;
- }
- return p;
- }
- //返回最大值非递归
- public TNode iterativemimMum(){
- TNode p=root;
- while(p.right!=null)
- p=p.right;
- return p;
- }
- //返回最大值递归
- public TNode maxMum(TNode p){
- if(p.right!=null)
- return minMum(p.right);
- return p;
- }
- //返回最小值递归
- public TNode minMum(TNode p){
- if(p.left!=null)
- return minMum(p.left);
- return p;
- }
- //返回节点的后继
- public TNode successor(TNode p){
- if(p.right!=null)
- return minMum(p.right);
- TNode y=p.parent;
- while(y!=null&&y==y.parent.right)
- {
- p=y;
- y=y.parent;
- }
- return y;
- }
- //返回节点的前驱
- public TNode predecessor(TNode p){
- if(p.left!=null)
- return maxMum(p.left);
- TNode y=p.parent;
- while(y!=null&&y==y.parent.left){
- p=y;
- y=y.parent;
- }
- return y;
- }
- public void delete(TNode p){
- TNode y=new TNode(0,null);
- TNode x=new TNode(0,null);
- if(p.left==null||p.right==null)
- y=p;
- else
- y=successor(p); //选择需要被删除的元素
- if(y.left!=null)
- x=y.left;
- else
- x=y.right;
- if(x!=null)
- x.parent=y.parent; //删除y
- if(y.parent==null)
- root=y;//如果y是根
- else if(y==y.parent.left)
- y.parent.left=x;
- else
- y.parent.right=x;
- if(y!=p)
- p.key=y.key;
- }
- public static void main(String [] args){
- BinarySearchTree tree=new BinarySearchTree();
- TNode node3=new TNode(4,null,null,null);
- TNode node1=new TNode(3,null,node3,null);
- TNode node2=new TNode(6,null,null,null);
- TNode node7=new TNode(9,null,null,null);
- TNode node6=new TNode(8,null,node7,null);
- TNode node5=new TNode(10,node6,null,null);
- TNode node4=new TNode(7,node2,node5,null);
- TNode root=new TNode(5,node1,node4,null);
- node1.parent=root;
- node2.parent=node4;
- node4.parent=root;
- node5.parent=node4;
- node3.parent=node1;
- node6.parent=node5;
- node7.parent=node6;
- tree.root=root;
- // tree.inorder(tree.root);
- // System.out.println( tree.successor(root).key);
- // System.out.print( tree.predecessor(root).key);
- tree.delete(root);
- tree.iterativePostorder();
- //tree.delete(node5);
- // tree.iterativePreorder();
- //tree.preorder(tree.root);
- // tree.postorder(tree.root);
- }
- }