• 二叉排序树 常用函数小结


    所实现的功能为
    insert 插入
    del 删除
    find 查找
    getMin 查找最小值
    getMax 查找最大值
    size 结点总个数
    getHeight 树的高度
    inorder 中序遍历
    [前序遍历和后序遍历请看我之前文章] 144. 二叉树的前序迭代遍历推出后序,中序以及层序的实现
    printleaves 打印叶子结点
    onelevelnum 打印树的每一层结点
    kthlargest 树的第k最大值
    suojin 缩进打印树

    所有的功能对应大部分至少两种方法

    BitreeNode 类 代码如下

    package BST;
    
    class BiTreeNode {
    	public BiTreeNode lchild, rchild;
    	public int data;
    
    
    	public BiTreeNode(int data) {
    		this.data=data;	
    	}
    
    	public BiTreeNode() {
    		lchild=rchild=null;
    		data=0;
    	}
    
    	@Override
    	public String toString() {
    		return "BiTreeNode [data=" + data + "]";
    	}
    
    	public BiTreeNode(BiTreeNode lchild, BiTreeNode rchild, int data) {
    		this.lchild = lchild;
    		this.rchild = rchild;
    		this.data = data;
    	}
    	
    }
    

    BiSearchTree 类 代码如下

    package BST;
    
    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.Stack;
    
    public class BiSearchTree {
    	public BiTreeNode root;
    	int m = 0;
    	int n;
    
    	public BiSearchTree() {
    		root = null;
    	}
    
    	public void insert(int x) {
    
    		if (root == null) {
    			root = new BiTreeNode(x);
    			return;
    		}
    		BiTreeNode p = root;
    		while (p != null) {
    			if (x > p.data) {
    
    				if (p.rchild == null) {
    					p.rchild = new BiTreeNode(x);
    					return;
    				}
    				p = p.rchild;
    
    			} else {
    				if (p.lchild == null) {
    					p.lchild = new BiTreeNode(x);
    					return;
    				}
    				p = p.lchild;
    
    			}
    
    		}
    	}
    
    	public BiTreeNode insert2(BiTreeNode p, int x) {
    		if (root == null) {
    			root = new BiTreeNode(x);
    		}
    		if (p == null) {
    			p = new BiTreeNode(x);
    			return p;
    		} else {
    			if (x > p.data) {
    				p.rchild = insert2(p.rchild, x);
    
    			} else if (x < p.data) {
    				p.lchild = insert2(p.lchild, x);
    
    			}
    			return p;
    		}
    	}
    
    	public void del1(int x) {
    		BiTreeNode p = root;
    		BiTreeNode parent = p;
    		while (p != null && p.data != x) {
    			parent = p;
    			if (x > p.data) {
    				p = p.rchild;
    			} else {
    				p = p.lchild;
    			}
    		}
    		if (p == null) {
    			System.out.println("error");
    			return;
    		} else if (p.data == x) {
    			if (p.lchild != null && p.rchild != null) {
    				BiTreeNode temp = p;
    				BiTreeNode parent2 = temp;
    				temp = p.lchild;
    				p.data = getMax2(temp).data;
    				while (temp.rchild != null) {
    					parent2 = temp;
    					temp = temp.rchild;
    				}
    				if (temp.lchild == null) {
    					if (parent2.lchild == temp) {
    						parent2.lchild = null;
    					} else {
    						parent2.rchild = null;
    					}
    
    				} else {
    					if (parent2.lchild == temp) {
    						parent2.lchild = temp.lchild;
    					} else {
    						parent2.rchild = temp.lchild;
    					}
    
    				}
    
    			} else {
    				BiTreeNode child;
    				if (p.lchild != null) {
    					child = p.lchild;
    				} else if (p.rchild != null) {
    					child = p.rchild;
    				} else {
    					child = null;
    				}
    
    				if (parent.lchild == p) {
    					parent.lchild = child;
    				} else if (parent.rchild == p) {
    					parent.rchild = child;
    				} else {
    					root = child;
    				}
    
    			}
    
    		}
    	}
    
    	public BiTreeNode del2(BiTreeNode p, int x) {
    		if (p == null) {
    			System.out.println("empty error");
    			return p;
    		}
    		if (x < p.data) {
    			p.lchild = del2(p.lchild, x);
    		} else if (x > p.data) {
    			p.rchild = del2(p.rchild, x);
    		} else {
    			if (p.lchild != null && p.rchild != null) {
    				BiTreeNode temp = p.lchild;
    				p.data = getMax2(temp).data;
    				p.lchild = del2(p.lchild, p.data);
    			} else if (p.lchild == null) {
    				p = p.rchild;
    			} else if (p.rchild == null) {
    				p = p.lchild;
    			}
    		}
    		root = p;
    		return p;
    	}
    
    	public void del3(int x) {
    		BiTreeNode p = root;
    		BiTreeNode parent = p;
    		while (p != null && p.data != x) {
    			parent = p;
    			if (x < p.data) {
    				p = p.lchild;
    			} else if (x > p.data) {
    				p = p.rchild;
    			}
    
    		}
    		if (p == null) {
    			return;
    		} else {
    			if (p.lchild != null && p.rchild != null) {
    				BiTreeNode temp = p.lchild;
    				if (parent.lchild == p) {
    					parent.lchild = temp;
    				} else if (parent.rchild == p) {
    					parent.rchild = temp;
    				} else {
    					if (p.lchild != null) {
    						root = p.lchild;
    					} else if (p.rchild != null) {
    						root = p.rchild;
    					}
    				}
    				getMax2(temp).rchild = p.rchild;
    
    			} else {
    				BiTreeNode child = null;
    				if (p.lchild != null) {
    					child = p.lchild;
    				} else if (p.rchild != null) {
    					child = p.rchild;
    				}
    				if (parent.lchild == p) {
    					parent.lchild = child;
    				} else if (parent.rchild == p) {
    					parent.rchild = child;
    				} else {
    					root = child;
    				}
    			}
    		}
    
    	}
    
    	public BiTreeNode del4(BiTreeNode p, int x) {
    		if (p == null) {
    			return p;
    		}
    
    		if (x < p.data) {
    			p.lchild = del4(p.lchild, x);
    		} else if (x > p.data) {
    			p.rchild = del4(p.rchild, x);
    		} else {
    			if (p.lchild != null && p.rchild != null) {
    				BiTreeNode temp = getMax2(p.lchild);
    				temp.rchild = p.rchild;
    				p = p.lchild;
    
    			} else if (p.lchild == null) {
    				p = p.rchild;
    			} else if (p.rchild == null) {
    				p = p.lchild;
    			}
    		}
    		root = p;
    		return p;
    	}
    
    	public BiTreeNode find(int x) {
    		BiTreeNode p = root;
    		if (p == null) {
    			System.out.println("empty error");
    			return p;
    		}
    		while (p != null) {
    			if (x > p.data) {
    				p = p.rchild;
    			} else if (x < p.data) {
    				p = p.lchild;
    			} else {
    				return p;
    			}
    		}
    		return null;
    
    	}
    
    	public BiTreeNode find2(BiTreeNode p, int x) {
    		if (p == null) {
    			System.out.println("empty error");
    			return p;
    		}
    		if (x > p.data) {
    			return find2(p.rchild, x);
    		} else if (x < p.data) {
    			return find2(p.lchild, x);
    		} else {
    			if (x == p.data) {
    				return p;
    			} else {
    				return null;
    			}
    
    		}
    
    	}
    
    	public BiTreeNode getMin() {
    		if (root == null) {
    			return null;
    		}
    		BiTreeNode p = root;
    		while (p.lchild != null) {
    			p = p.lchild;
    		}
    		return p;
    	}
    
    	public BiTreeNode getMin2(BiTreeNode p) {
    		if (p == null) {
    			System.out.println("error p结点data为空");
    			return null;
    		}
    		if (p.lchild == null) {
    			return p;
    		}
    
    		return getMin2(p.lchild);
    	}
    
    	public BiTreeNode getMax() {
    		if (root == null) {
    			return null;
    		}
    		BiTreeNode p = root;
    		while (p.rchild != null) {
    			p = p.rchild;
    		}
    		return p;
    	}
    
    	public BiTreeNode getMax2(BiTreeNode p) {
    		if (p == null) {
    			System.out.println("error p结点data为空");
    			return null;
    		}
    		if (p.rchild == null) {
    			return p;
    		}
    		return getMax2(p.rchild);
    	}
    
    	public int size() {
    		return size(root);
    	}
    
    	public int size(BiTreeNode p) {
    		if (p == null) {
    			return 0;
    		}
    		return 1 + size(p.lchild) + size(p.rchild);
    
    	}
    	public void size2(BiTreeNode p) {
    		int num=1;
    		Queue<BiTreeNode> queue = new LinkedList<BiTreeNode>();
    		queue.add(p);
    		while(!queue.isEmpty()) {
    			BiTreeNode temp=queue.poll();
    			if(temp.lchild!=null) {
    				queue.add(temp.lchild);
    				num++;
    			}
    			if(temp.rchild!=null) {
    				queue.add(temp.rchild);
    				num++;
    			}
    		}
    		System.out.println(num);
    	}
    	public int getHeight() {
    		return getHeight(root);
    	}
    
    	public int getHeight(BiTreeNode p) {
    		if (p == null) {
    			return 0;
    		}
    		return getHeight(p.lchild) > getHeight(p.rchild) ? getHeight(p.lchild) + 1 : getHeight(p.rchild) + 1;
    
    	}
    	public int getHeight2() {
    		return getHeight2(root);
    	}
    	public int getHeight2(BiTreeNode p) {
    		int height=0;
    		Queue<BiTreeNode> queue = new LinkedList<BiTreeNode>();
    		queue.add(p);
    		while(!queue.isEmpty()) {
    			int n=queue.size();
    			for(int i=0;i<n;i++) {
    				BiTreeNode temp=queue.poll();
    				if(temp.lchild!=null) {
    					queue.add(temp.lchild);
    				}
    				if(temp.rchild!=null) {
    					queue.add(temp.rchild);
    				}	
    			}
    			height++;
    			
    		}
    		return height;
    	}
    	public int inorder(BiTreeNode p) {
    		if (p == null) {
    			return -1;
    		}
    		inorder(p.lchild);
    		System.out.print(p.data + " ");
    		inorder(p.rchild);
    		return 1;
    	}
    
    	public int inorder() {
    		return inorder(root);
    	}
    
    	public LinkedList<Integer> inorder2() {
    		if (root == null) {
    			return null;
    		}
    		BiTreeNode p = root;
    		LinkedList<Integer> list = new LinkedList<Integer>();
    		Stack<BiTreeNode> stack = new Stack<BiTreeNode>();
    		while (p != null || !stack.isEmpty()) {
    			if (p != null) {
    				stack.push(p);
    				p = p.lchild;
    			} else {
    				p = stack.pop();
    				list.addLast(p.data);
    				p = p.rchild;
    
    			}
    		}
    		System.out.println(list.toString());
    		return list;
    	}
    	
    	public void OneLevelnum(int xlevel) {
    		BiTreeNode p=root;
    		int height=0;
    		Queue<BiTreeNode> queue = new LinkedList<BiTreeNode>();
    		queue.add(p);
    		while(!queue.isEmpty()) {
    			int n=queue.size();
    			if(++height==xlevel) {
    				for(int i=0;i<n;i++) {		
    					System.out.print(queue.poll()+" ");
    				}
    				break;
    			}
    			for(int i=0;i<n;i++) {
    				BiTreeNode temp=queue.poll();
    				if(temp.lchild!=null) {
    					queue.add(temp.lchild);
    				}
    				if(temp.rchild!=null) {
    					queue.add(temp.rchild);
    				}	
    			}
    			
    			
    		}
    	
    	}
    	public void printleaves(BiTreeNode p) {
    		if (p == null)
    			return;
    		if (p.lchild == null && p.rchild == null) {
    			System.out.print(p + " ");
    		} else {
    			printleaves(p.lchild);
    			printleaves(p.rchild);
    		}
    	}
    	public void printleaves2(BiTreeNode p) {
    		int height=getHeight();
    		OneLevelnum(height);
    	}
    	public void suojin() {
    		suojin(root, 1);
    	}
    
    	public void suojin(BiTreeNode p, int i) {
    		if (p == null)
    			return;
    		else {
    			for (int j = 0; j < i; j++) {
    				System.out.print("-");
    			}
    			System.out.println(p.data);
    			suojin(p.lchild, ++i);
    			i--;
    			suojin(p.rchild, ++i);
    			i--;
    		}
    
    	}
    
    	public int kthLargest(BiTreeNode p, int k) {
    		if (p == null) {
    			return -1;
    		}
    		kthLargest(p.rchild, k);
    		m++;
    		if (k == m) {
    			n = p.data;
    			return p.data;
    		}
    
    		kthLargest(p.lchild, k);
    		return n;
    	}
    
    }
    
    
  • 相关阅读:
    【题解】小Z的袜子
    浅谈最小生成树
    【题解】钻石收藏家
    浅谈线段树
    浅谈拓扑排序
    浅谈树的直径和树的重心
    浅谈求逆序对
    浅谈欧拉回路
    浅谈离散化
    浅谈高斯消元
  • 原文地址:https://www.cnblogs.com/nmydt/p/14195321.html
Copyright © 2020-2023  润新知