编程语言
首页 > 编程语言> > 二叉树的Java实现以及运行图例

二叉树的Java实现以及运行图例

作者:互联网

二叉树的Java实现以及运行图例

二叉树的基本操作有以下几个:

在基本操作的基础上又增加了两个操作:

首先我们需要定义一个结点类:

代码如下:

class Node {
	int  value;//结点的数据
	Node leftChild;//左子节点
	Node rightChild;//右子节点
	boolean isdelate;//判断是否被删除 true为未删除,false为已删除
	Node(int value) {
		this.value = value;
	}
	public void display() {
		System.out.print(this.value + "\t");
	}
	@Override
	public String toString() {
		return String.valueOf(value);
	}
}

结点类定义好之后就可以定义树的类了

class BinaryTree {
	private Node root = null;
	
	BinaryTree(int value) {
		root = new Node(value);
		root.leftChild  = null;
		root.rightChild = null;
	}
	public Node findKey(int value) {}   //查找
        public String insert(int value) {}  //插入
        public void inOrderTraverse() {}    //中序遍历递归操作
        public void preOrderTraverse() {}  //前序遍历
        public void postOrderTraverse() {} //后序遍历
        public int getMinValue() {} //得到最小(大)值
        public boolean delete(int value) {} //删除
}

这里包含了二叉树的基本操作和两个扩展的操作
以下是操作的实现代码:

查找

//查找
public Node findKey(int value){
    Node current = root;
    while(current != null){
        if(current.data > key){//当前值比查找值大,搜索左子树
            current = current.leftChild;
        }else if(current.data < key){//当前值比查找值小,搜索右子树
            current = current.rightChild;
        }else{
            return current;
        }
    }
    return null;//遍历完整个树没找到,返回null
}

插入

//插入
public boolean insert(int data) {
    Node newNode = new Node(data);
    if(root == null){//当前树为空树,没有任何节点
        root = newNode;
        return true;
    }else{
        Node current = root;
        Node parentNode = null;
        while(current != null){
            parentNode = current;
            if(current.data > data){//当前值比插入值大,搜索左子节点
                current = current.leftChild;
                if(current == null){//左子节点为空,直接将新值插入到该节点
                    parentNode.leftChild = newNode;
                    return true;
                }
            }else{
                current = current.rightChild;
                if(current == null){//右子节点为空,直接将新值插入到该节点
                    parentNode.rightChild = newNode;
                    return true;
                }
            }
        }
    }
    return false;
}

递归遍历

//中序遍历
public void infixOrder(Node current){
    if(current != null){
        infixOrder(current.leftChild);
        System.out.print(current.data+" ");
        infixOrder(current.rightChild);
    }
}
 
//前序遍历
public void preOrder(Node current){
    if(current != null){
        System.out.print(current.data+" ");
        preOrder(current.leftChild);
        preOrder(current.rightChild);
    }
}
 
//后序遍历
public void postOrder(Node current){
    if(current != null){
        postOrder(current.leftChild);
        postOrder(current.rightChild);
        System.out.print(current.data+" ");
    }
}

查找最大值和最小值

//找到最大值
public Node findMax(){
    Node current = root;
    Node maxNode = current;
    while(current != null){
        maxNode = current;
        current = current.rightChild;
    }
    return maxNode;
}
//找到最小值
public Node findMin(){
    Node current = root;
    Node minNode = current;
    while(current != null){
        minNode = current;
        current = current.leftChild;
    }
    return minNode;
}

删除

public boolean delete(int key) {
    Node current = root;
    Node parent = root;
    boolean isLeftChild = false;
    //查找删除值,找不到直接返回false
    while(current.data != key){
        parent = current;
        if(current.data > key){
            isLeftChild = true;
            current = current.leftChild;
        }else{
            isLeftChild = false;
            current = current.rightChild;
        }
        if(current == null){
            return false;
        }
    }
    //如果当前节点没有子节点
    if(current.leftChild == null && current.rightChild == null){
        if(current == root){
            root = null;
        }else if(isLeftChild){
            parent.leftChild = null;
        }else{
            parent.rightChild = null;
        }
        return true;
    }
    return false;
}
public boolean delete(int key) {
    Node current = root;
    Node parent = root;
    boolean isLeftChild = false;
    //查找删除值,找不到直接返回false
    while(current.data != key){
        parent = current;
        if(current.data > key){
            isLeftChild = true;
            current = current.leftChild;
        }else{
            isLeftChild = false;
            current = current.rightChild;
        }
        if(current == null){
            return false;
        }
        public boolean delete(int key) {
    Node current = root;
    Node parent = root;
    boolean isLeftChild = false;
    //查找删除值,找不到直接返回false
    while(current.data != key){
        parent = current;
        if(current.data > key){
            isLeftChild = true;
            current = current.leftChild;
        }else{
            isLeftChild = false;
            current = current.rightChild;
        }
        if(current == null){
            return false;
        }
public Node getSuccessor(Node delNode){
    Node successorParent = delNode;
    Node successor = delNode;
    Node current = delNode.rightChild;
    while(current != null){
        successorParent = successor;
        successor = current;
        current = current.leftChild;
    }
    //将后继节点替换删除节点
    if(successor != delNode.rightChild){
        successorParent.leftChild = successor.rightChild;
        successor.rightChild = delNode.rightChild;
    }
     
    return successor;
}

标签:Node,rightChild,Java,运行图,current,二叉树,null,root,public
来源: https://blog.csdn.net/qq_29817901/article/details/88886012