BST Binary search(sort) tree 二叉排序树

BST Binary search(sort) tree 二叉排序树


提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

一、property

Binary Search Tree (BST) is a tree with the following properties:
1. All items in the left subtree are less than the root.
2. All items in the right subtree are greater than or equal to the root.
3. Each subtree is itself a binary search tree.

A BST is a binary tree in which the left subtree contains key values less than the root and the right subtree contains key values greater than or equal to the root.

二、code

1.construct ,add and traversal(infixorder)

class Node {
	int value;
	Node left;
	Node right;

	public Node(int value) {

		this.value = value;
	}

	@Override
	public String toString() {
		return "Node [value=" + value + "]";
	}
	public void add(Node node) {
		if (node == null) {
			return;
		}
		if (node.value < this.value) {
			if (this.left == null) {
				this.left = node;
			} else {
				this.left.add(node);
			}
		} else { 
			if (this.right == null) {
				this.right = node;
			} else {
				this.right.add(node);
			}

		}
	}
	public void infixOrder() {
		if (this.left != null) {
			this.left.infixOrder();
		}
		System.out.println(this);
		if (this.right != null) {
			this.right.infixOrder();
		}
	}
}

2:Delete A Node From Binary Search Tree

Case 1:
The node to be deleted has no children – leaves node
All we need to do is set the delete node’s parent to null
Case 2:
The node to be deleted has only a right subtree .
If there is only a right subtree, then we can simply attach the right subtree to the delete node’s parent.
Case 3 :
The node to be deleted has only a left subtree .
If there is only a left subtree, then we attach the left subtree to the delete node’s parent.

Case 4 :
The node to be deleted has two subtrees.
1: find the largest node in the deleted node’s left subtree and move its data to replace the deleted node’s data, or
2: find the smallest node on the deleted node’s right subtree and move its data to replace the deleted nodes data.

3:search node and search node parent

Search:
Search algorithm is used to find a specific node in the tree.
If the target value, newItem is greater than the root tree then the search is carry on the right subtrees, whereas if the target value, newItem is smaller than the root tree, search is carry on the left subtrees.

public Node search(int value) {
		if (value == this.value) {
			return this;
		} else if (value < this.value) {
			if (this.left == null) {
				return null;
			}
			return this.left.search(value);
		} else { 
			if (this.right == null) {
				return null;
			}
			return this.right.search(value);
		}

	}

public Node searchParent(int value) {
		if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)) {
			return this;
		} else {
			if (value < this.value && this.left != null) {
				return this.left.searchParent(value); 
			} else if (value >= this.value && this.right != null) {
				return this.right.searchParent(value); 
			} else {
				return null;
			}
		}

	}

2:construct BST class

(for apply)

class BinarySortTree {
	private Node root;

	public Node getRoot() {
		return root;
	}

	public void add(Node node) {
		if (root == null) {
			root = node;
		} else {
			root.add(node);
		}
	}

	public void infixOrder() {
		if (root != null) {
			root.infixOrder();
		} else {
			System.out.println("the BST is null");
		}
	}

	public Node search(int value) {
		if (root == null) {
			return null;
		} else {
			return root.search(value);
		}
	}

	public Node searchParent(int value) {
		if (root == null) {
			return null;
		} else {
			return root.searchParent(value);
		}
	}

	public void delNode(int value) {
		if (root == null) {
			return;
		} else {
			Node targetNode = search(value);
			if (targetNode == null) {
				return;
			}
			if (root.left == null && root.right == null) {
				root = null;
				return;
			}
			Node parent = searchParent(value);
			if (targetNode.left == null && targetNode.right == null) {
				if (parent.left != null && parent.left.value == value) {
					parent.left = null;
				} else if (parent.right != null && parent.right.value == value) {
					parent.right = null;
				}
			} else if (targetNode.left != null && targetNode.right != null) { 
				int minVal = delRightTreeMin(targetNode.right);
				targetNode.value = minVal;

			} else { 
				if (targetNode.left != null) {
					if (parent != null) {
						if (parent.left.value == value) {
							parent.left = targetNode.left;
						} else { 
							parent.right = targetNode.left;
						}
					} else {
						root = targetNode.left;
					}
				} else { 
					if (parent != null) {
						if (parent.left.value == value) {
							parent.left = targetNode.right;
						} else { 
							parent.right = targetNode.right;
						}
					} else {
						root = targetNode.right;
					}
				}

			}

		}
	}

	
	public int delRightTreeMin(Node node) {
		Node target = node;
		while (target.left != null) {
			target = target.left;
		}
		delNode(target.value);
		return target.value;
	}

}

三: conclusion:

Basic operations:
1:Construction – build a null tree.
2:Destroy - delete all items in the tree.
3:Insert(add) – insert a new node into the tree.
4:Delete – delete a node from a tree.
5:Traversal – traverse, access, and process an item in the tree.
6:Search– search an item in the tree.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值