标签:img this 基于 tor 最短路径 文件 tps sea 删除
树结构本身是一种天然的组织结构
将数据使用树结构存储后,出奇的高效
class Node {
E e;
Node left; //左孩子
Node right; //右孩子
}
// 从二分搜索树中添加新的元素e
public void add(T t){
root = add(root, t);
}
private Node add(Node node, T t){
if(node == null){
size++;
return new Node(t);
}
if(t.compareTo(node.t) < 0){
node.left = add(node.left, t);
}
else if(t.compareTo(node.t) > 0){
node.right = add(node.right, t);
}
return node;
}
public boolean contains(T t){
return contains(root, t);
}
private boolean contains(Node node, T t){
if(node == null){
return false;
}
if(t.compareTo(node.t) == 0){
return true;
}
else if(t.compareTo(node.t) > 0){
return contains(node.right, t);
}
else {
return contains(node.left, t);
}
}
//前序遍历
public void preOrder(){
preOrder(root);
}
private void preOrder(Node node){
if(node == null){
return;
}
System.out.println(node.t);
preOrder(node.left);
preOrder(node.right);
}
中序遍历可实现二分搜索树的从小到大排序。
// 中序遍历
public void inOrder(){
inOrder(root);
}
private void inOrder(Node node){
if(node == null){
return;
}
inOrder(node.left);
System.out.println(node.t);
inOrder(node.right);
}
public void postOrder(){
postOrder(root);
}
private void postOrder(Node node){
if(node == null){
return;
}
postOrder(node.left);
System.out.println(node.t);
postOrder(node.right);
}
基于栈的实现:
//基于栈的前序遍历
public void preOrderNR(){
Stack<Node> nodeStack = new ArrayStack<>();
nodeStack.push(root);
while (!nodeStack.isEmpty()){
Node pop = nodeStack.pop();
System.out.println(pop.t);
if(pop.right != null){
nodeStack.push(pop.right);
}
if(pop.left != null){
nodeStack.push(pop.left);
}
}
}
之前实现的的遍历方式都是深度优先优先遍历。而广度优先遍历的实现基于队列。
广度优先遍历的意义:
// 层序遍历
public void levelOrder(){
Queue<Node> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()){
Node remove = queue.remove();
System.out.println(remove.t);
if(remove.left != null){
queue.add(remove.left);
}
if(remove.right != null){
queue.add(remove.right);
}
}
}
// 找到二分搜素树的最小元素
public T minimum(){
if(size == 0){
throw new IllegalArgumentException("树为空,删除最小值失败");
}
return minimum(root).t;
}
// 返回以node为根的二分搜索树的最小值所在的节点
private Node minimum(Node node){
if(node.left == null){
return node;
}
return minimum(node.left);
}
// 返回删除最小节点后的二分搜索树的根节点
// 删除掉以node为根的二分搜索树的最小节点
private Node removeMin(Node node) {
if(node.left == null){
Node rightNode = node.right;
node.right = null;
size--;
return rightNode;
}
node.left = removeMin(node.left);
return node;
}
// 查询二分搜素树的最大元素
public T maxmum(){
if(size == 0){
throw new IllegalArgumentException("树为空,删除最大值失败");
}
return maxmum(root).t;
}
// 返回以node为根的二分搜索树的最大值所在的节点
private Node maxmum(Node node){
if(node.right == null){
return node;
}
return maxmum(node.right);
}
public T removeMax(){
T ret = maxmum();
root = removeMax(root);
return ret;
}
// 返回删除最大节点后的二分搜索树的根节点
// 删除掉以node为根的二分搜索树的最大节点
private Node removeMax(Node node) {
if(node.right == null){
Node leftNode = node.left;
node.left = null;
size--;
return leftNode;
}
node.right = removeMax(node.right);
return node;
}
package binarySearchTree;
import java.util.LinkedList;
import java.util.Stack;
import java.util.Queue;
//二分搜索树的泛型必须具有可比较性
public class BST<T extends Comparable<T>> {
private class Node{
public T t;
public Node left, right;
public Node(T t) {
this.t = t;
this.left = null;
this.right = null;
}
}
private Node root;
private int size;
public BST() {
root = null;
size = 0;
}
public int getSize() {
return size;
}
public boolean isEmpty(){
return size == 0;
}
// 从二分搜索树中添加新的元素e
public void add(T t){
root = add(root, t);
}
private Node add(Node node, T t){
if(node == null){
size++;
return new Node(t);
}
if(t.compareTo(node.t) < 0){
node.left = add(node.left, t);
}
else if(t.compareTo(node.t) > 0){
node.right = add(node.right, t);
}
return node;
}
public boolean contains(T t){
return contains(root, t);
}
private boolean contains(Node node, T t){
if(node == null){
return false;
}
if(t.compareTo(node.t) == 0){
return true;
}
else if(t.compareTo(node.t) > 0){
return contains(node.right, t);
}
else {
return contains(node.left, t);
}
}
//前序遍历
public void preOrder(){
preOrder(root);
}
private void preOrder(Node node){
if(node == null){
return;
}
System.out.println(node.t);
preOrder(node.left);
preOrder(node.right);
}
//基于栈的前序遍历的非递归实现
public void preOrderNR(){
Stack<Node> nodeStack = new Stack<>();
nodeStack.push(root);
while (!nodeStack.isEmpty()){
Node pop = nodeStack.pop();
System.out.println(pop.t);
if(pop.right != null){
nodeStack.push(pop.right);
}
if(pop.left != null){
nodeStack.push(pop.left);
}
}
}
// 中序遍历
public void inOrder(){
inOrder(root);
}
private void inOrder(Node node){
if(node == null){
return;
}
inOrder(node.left);
System.out.println(node.t);
inOrder(node.right);
}
// 后序遍历
public void postOrder(){
postOrder(root);
}
private void postOrder(Node node){
if(node == null){
return;
}
postOrder(node.left);
System.out.println(node.t);
postOrder(node.right);
}
// 层序遍历
public void levelOrder(){
Queue<Node> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()){
Node remove = queue.remove();
System.out.println(remove.t);
if(remove.left != null){
queue.add(remove.left);
}
if(remove.right != null){
queue.add(remove.right);
}
}
}
// 查询而二分搜素树的最小元素
public T minimum(){
if(size == 0){
throw new IllegalArgumentException("树为空,删除最小值失败");
}
return minimum(root).t;
}
// 返回以node为根的二分搜索树的最小值所在的节点
private Node minimum(Node node){
if(node.left == null){
return node;
}
return minimum(node.left);
}
// 查询而二分搜素树的最大元素
public T maxmum(){
if(size == 0){
throw new IllegalArgumentException("树为空,删除最大值失败");
}
return maxmum(root).t;
}
// 返回以node为根的二分搜索树的最大值所在的节点
private Node maxmum(Node node){
if(node.right == null){
return node;
}
return maxmum(node.right);
}
public T removeMin(){
T ret = minimum();
root = removeMin(root);
return ret;
}
// 返回删除最小节点后的二分搜索树的根节点
// 删除掉以node为根的二分搜索树的最小节点
private Node removeMin(Node node) {
if(node.left == null){
Node rightNode = node.right;
node.right = null;
size--;
return rightNode;
}
node.left = removeMin(node.left);
return node;
}
public T removeMax(){
T ret = maxmum();
root = removeMax(root);
return ret;
}
// 返回删除最大节点后的二分搜索树的根节点
// 删除掉以node为根的二分搜索树的最大节点
private Node removeMax(Node node) {
if(node.right == null){
Node leftNode = node.left;
node.left = null;
size--;
return leftNode;
}
node.right = removeMax(node.right);
return node;
}
public void remove(T t){
root = remove(root, t);
}
private Node remove(Node node, T t) {
if(node == null){
return null;
}
if(t.compareTo(node.t) > 0){
node.right = remove(node.right, t);
return node;
}
else if(t.compareTo(node.t) < 0){
node.left = remove(node.left, t);
return node;
}
else {
// 当前待删除节点只有右孩子的情况
if(node.left == null){
Node rightNode = node.right;
node.right = null;
size--;
return rightNode;
}
// 当前待删除节点只有左孩子的情况
if(node.right == null){
Node leftNode = node.left;
node.left = null;
size--;
return leftNode;
}
// 当前待删除节点左右孩子均不为空的情况
Node successor = minimum(node.right);
successor.right = removeMin(node.right);
successor.left = node.left;
node.left = node.right = null;
return successor;
}
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
generateBSTString(root, 0, builder);
return builder.toString();
}
// 生成二分搜索树的字符串
private void generateBSTString(Node node, int depth, StringBuilder builder){
if(node == null){
builder.append(generateDepthString(depth) + "null\n");
return;
}
builder.append(generateDepthString(depth) + node.t + "\n");
generateBSTString(node.left, depth+1, builder);
generateBSTString(node.right, depth+1, builder);
}
// 生成深度信息的字符串
private String generateDepthString(int depth) {
StringBuilder builder = new StringBuilder();
for (int i=0; i<depth; i++){
builder.append("--");
}
return builder.toString();
}
}
标签:img this 基于 tor 最短路径 文件 tps sea 删除
原文地址:https://www.cnblogs.com/sout-ch233/p/13095191.html