(Java学习笔记13)二叉树---删除二叉树节点

来源:互联网 发布:centos修改locale 编辑:程序博客网 时间:2024/05/21 19:37

删除二叉树节点

删除节点是二叉树操作中最复杂的。在删除之前首先要查找要删的节点,找到节点后,这个要删除的节点可能会有三种情况需要考虑。

1.该节点是叶子节点,没有子节点。

要删除叶子节点,只需要改变该节点的父节点的引用值,将指向该节点的引用设置为null就可以了。

2.该节点有一个子节点。

改变父子节点的引用,将其直接指向要删除节点的子节点。

3.该节点有两个子节点。

要删除有两个子节点的节点,就需要使用它的中序后继来替代该节点。

代码如下:

public class Node {//数据项public long data;//数据项public String sData;//左子节点public Node leftChild;//右子节点public Node rightChild;/* * 构造方法 *  * */public Node(long value,String sData){this.data=value;this.sData=sData;}}public class Tree {//根节点public Node root;/* * 插入节点 * */public void insert(long value,String sValue){//封装节点Node newNode=new Node(value,sValue);//引用当前节点Node current=root;//引用父节点Node parent;//如果root为null,也就是第一次插入的时候if(root==null){root=newNode;return;}else{while(true){//父节点指向当前节点parent = current;//如果当前指向的节点数据比插入的要大,则向左走if(current.data > value){current=current.leftChild;if(current==null){parent.leftChild=newNode;return;}}else{current=current.rightChild;if(current==null){parent.rightChild=newNode;return;}}}}}/* * 查找节点 * */public Node find(long value){//引用当前节点,从根节点开始Node current=root;//循环,只要查找值不等于当前节点的数据项while(current.data!=value){//进行比较,比较查找和当前节点的大小if(current.data>value){current=current.leftChild;}else{current=current.rightChild;}//如果查找不到if(current==null){return null;}}return current;}/* * 删除节点 * */public boolean delete(long value){//引用当前节点,从根节点开始Node current=root;//应用当前节点的父节点Node parent=root;//是否为左节点boolean isLeftChild = true;while(current.data != value){parent=current;if(current.data > value){current=current.leftChild;isLeftChild=true;}else{current=current.rightChild;isLeftChild=false;}//如果找不到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;}}else if(current.rightChild==null){if(current==root){root=current.leftChild;}else if(isLeftChild){parent.leftChild=current.leftChild;}else{parent.rightChild=current.leftChild;}}else if(current.leftChild==null){if(current==root){root=current.rightChild;}else if(isLeftChild){parent.leftChild=current.rightChild;}else{parent.rightChild=current.rightChild;}} else {Node successor=getSuccessor(current);if(current == root){root = successor;} else if(isLeftChild){parent.leftChild = successor;} else{parent.rightChild=successor;}successor.leftChild=current.leftChild;}return true;}/* * 删除有两个子节点的节点 * */public Node getSuccessor(Node delNode){Node successor=delNode;Node successorParent=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;}/* * 前序遍历 * */public void frontOrder(Node localNode){if(localNode != null){//先访问根节点System.out.println(localNode.data+","+localNode.sData);//前序遍历左子树frontOrder(localNode.leftChild);//前序遍历右子树frontOrder(localNode.rightChild);}}/* * 中序遍历 * */public void inOrder(Node localNode){if(localNode != null){//先访问左子树inOrder(localNode.leftChild);//中序遍历根节点System.out.println(localNode.data+","+localNode.sData);//中序遍历右子树inOrder(localNode.rightChild);}}/* * 后序遍历 * */public void afterOrder(Node localNode){if(localNode != null){//后序遍历左子树afterOrder(localNode.leftChild);//后序遍历右子树afterOrder(localNode.rightChild);//遍历根节点System.out.println(localNode.data+","+localNode.sData);}}}public class TestTree {public static void main(String args[]){Tree tree=new Tree();tree.insert(10,"James");tree.insert(20,"Yao");tree.insert(15,"Kobe");tree.insert(3,"Mac");tree.insert(4, "Zhangsan");tree.insert(90, "Lisi");tree.delete(4);tree.inOrder(tree.root);}}


0 0
原创粉丝点击