有序二叉树作为二叉树的一种,特点是左子树存储的数据<父节点存储的数据<右子树存储的数据,对这种二叉树进行中序遍历就能得到数据的升序序列,相反,如果想得到数据的降序序列,则子树存储的数据>父节点存储的数据>右子树存储的数据.
有序二叉树的优点是:不仅添加删除元素效率较高,查找效率也较高,类似于二分查找法。
public class BinarySortTreeDemo {
public static void main(String[] args) {
int arr[]={7,3,10,12,5,1,9,2};
BinarySortTree binarySortTree = new BinarySortTree();
//遍历数组添加元素到树中
for(int data:arr){
binarySortTree.add(new Node(data)); //添加元素
}
//先序遍历
binarySortTree.preOrder();
System.out.println("---------------");
//中序遍历
binarySortTree.midOrder(); //有序二叉树中序遍历才是输出有序数组
System.out.println("---------------");
//后序遍历
binarySortTree.lastOrder();
System.out.println("----------------");
//查找节点
System.out.println(binarySortTree.search(12));
System.out.println("---------------");
//删除节点
binarySortTree.delNode(7);
//删除后中序遍历
binarySortTree.midOrder();
}
}
class BinarySortTree{
private Node root;
public void add(Node node){
if(root==null){
root=node;
}else{
root.add(node);
}
}
//中序遍历
public void midOrder(){
if(root!=null){
root.midOrder();
}else {
System.out.println("树为空");
}
}
//先序遍历
public void preOrder(){
if(root!=null){
root.preOrder();
}else {
System.out.println("树为空");
}
}
//后序遍历
public void lastOrder(){
if(root!=null){
root.lastOrder();
}else {
System.out.println("树为空");
}
}
//查找节点
public Node search(int value){
if(root==null){
System.out.println("树为空");
return null;
}
return root.search(value);
}
//删除节点
public void delNode(int value){
if(root==null){
System.out.println("树为空");
return;
}
//找到要删除的节点
Node targetNode=search(value);
if(targetNode==null){
System.out.println("节点不存在");
return;
}
if(root.left==null&&root.right==null){
//当前树只有一个根节点,且就是要被删除的
root=null;
return;
}
//找父节点
Node parent=searchParent(value);
//如果要删除的节点是叶子节点
if(targetNode.left==null&&targetNode.right==null){
//判断targetNode是parent的哪个节点
if(parent.left!=null&&parent.left==targetNode){
parent.left=null;
}else if(parent.right!=null&&parent.right==targetNode){
parent.right=null;
}
}else if(targetNode.left!=null&&targetNode.right!=null){ //如果要删除的节点有两个子树
//在右子树中找到最小的一个元素替换掉要删除的那个节点
targetNode.value=delTreeMin(targetNode.right);
}else {//删除只有一颗子树的节点
if(targetNode.left!=null){
if(parent!=null) {
//判断targetNode 是parent的哪个节点
if (parent.left == targetNode) {
parent.left = targetNode.left;
} else {
parent.right = targetNode.left;
}
}else{
root=targetNode.left;
}
}else{
if(parent!=null) {
//判断targetNode 是parent的哪个节点
if (parent.left == targetNode) {
parent.left = targetNode.right;
} else {
parent.right = targetNode.right;
}
}else{
root=targetNode.right;
}
}
}
}
//查找节点的父节点
public Node searchParent(int value){
if(root==null){
System.out.println("树为空");
return null;
}
if(root.value==value){
return null;
}
return root.searchParent(value);
}
//删除以node为根的排序二叉树的最小值并返回
public int delTreeMin(Node node){
Node target=node;
if(target==null){
throw new NullPointerException();
}
while(target.left!=null){
target=target.left; //向左遍历
}
//删除最小节点
delNode(target.value);
return target.value;
}
}
class Node{
int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
}
//查找节点并返回
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;
}
}
}
//添加节点
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);
}
}
}
@Override
public String toString() {
return "Node{" +
"value=" + value +
'}';
}
//中序遍历
public void midOrder(){
if(this.left!=null){
this.left.midOrder();
}
System.out.println(this);
if(this.right!=null){
this.right.midOrder();
}
}
//先序遍历
public void preOrder(){
System.out.println(this);
if(this.left!=null){
this.left.preOrder();
}
if(this.right!=null){
this.right.preOrder();
}
}
//后序遍历
public void lastOrder(){
if(this.left!=null){
this.left.lastOrder();
}
if(this.right!=null){
this.right.lastOrder();
}
System.out.println(this);
}
}