自己写的学习案例代码,还是要给记录下来的,方便以后自己去查找。
package eight11one;
/**
* 二叉树节点
* @author 180719-2
*
*/
public class Node {
public long index;
public String data;
public Node leftChild;
public Node rightChild;
public Node(long index,String data){
this.index = index;
this.data = data;
}
}
package eight11one;
import java.util.HashMap;
import java.util.Map;
/**
* 二叉树
* @author 180719-2
*
*/
public class BinaryTree {
public Node root;//二叉树根节点
/**
* 向二叉树中插入数据
* 郭胜
* @param index
* @param data
*/
public void insertBinaryTree(long index,String data){
Node newNode = new Node(index,data);//新建节点
if(root == null){//如果根节点为null
root = newNode;
}else{//如果root不为空要向下循环
Node current = root;
while(true){
if(current.index > index){
if(current.leftChild == null){
current.leftChild = newNode;
return;
}else{
current = current.leftChild;
}
}else {
if(current.rightChild == null){
current.rightChild = newNode;
return;
}else{
current = current.rightChild;
}
}
}
}
}
/**
* 从二叉树中查找数据
* 郭胜
* @return
*/
public Node searchFromBinaryTree(long index){
Node current = this.root;
while(current.index != index){
if(current.index > index){
current = current.leftChild;
}else{
current = current.rightChild;
}
if(current == null){
return null;
}
}
return current;
}
/**
* 根据值查询出该节点和该节点的父节点
* 郭胜
* @param index
* @return
*/
public Map<String,Object> searchPCNode(long index){
Map<String,Object> map = new HashMap<String,Object>();
Node current = this.root;
Node parent = this.root;
boolean isLeft = true;
while(current.index != index){
parent = current;
if(current.index > index){
current = current.leftChild;
isLeft = true;
}else{
current = current.rightChild;
isLeft = false;
}
if(current == null){
return null;
}
}
map.put("childNode", current);
map.put("parentNode", parent);
map.put("isLeft", isLeft);
return map;
}
/**
* 删除节点
* 分三种情况,1删除叶节点,2删除只有一个子节点的节点,3删除有两个节点的节点
* @param val
* @return
*/
public boolean delete(long val){
Map<String,Object> map = searchPCNode(val);
if(null == map ){
return false;
}
boolean isLeft = (boolean)map.get("isLeft");
Node parent = (Node) map.get("parentNode");
Node child = (Node) map.get("childNode");
//删除的是叶节点
if(child.leftChild == null && child.rightChild == null){
if(child == root){//如果删除的是根节点则令根节点为null就可以了
root = null;
}else{
if(isLeft){//如果要删除的节点是左节点,则删除左节点
parent.leftChild = null;
}else{//如果要删除的节点是右节点,则删除右节点
parent.rightChild = null;
}
}
return true;
}
//删除的是该节点只有一个节点的节点
if(child.leftChild == null || child.rightChild == null){
if(child == root){
root = null;
}else{
if(isLeft){//如果删除该节点是左节点
if(child.leftChild != null){//这边再加判断,即如果要删除的节点的左子节点不为null,则将要删除节点的父节点的左子节点指向要删除节点的左子节点
parent.leftChild = child.leftChild;
}else{//反之,则将要删除节点的父节点的左子节点指向要删除节点的右子节点
parent.leftChild = child.rightChild;
}
}else {//如果删除该节点是右节点
if(child.leftChild != null){//同上
parent.rightChild = child.leftChild;
}else{
parent.rightChild = child.rightChild;
}
}
}
return true;
}
//删除的节点有两个子节点,思路是:从该节点的左子节点查,如果左子节点有右节点,一直继续向下找。
//流程是左子节点——右子节点——右子节点——右子节点……一直到最后一个右子节点、
//满足的条件是,要么第一个左子节点没有右子节点,要么是循环向下找右子节点,直到最后右子节点没有右子节点为止
//找到该节点后,将要删除的节点替换成该节点就可以了。
map.clear();
map = searchNode(child);
boolean isLeftChange = (boolean)map.get("isLeft");
Node changeNode = (Node)map.get("changeNode");
if(root == child){//如果删除的是根节点
if(isLeftChange){//即要替换的节点是删除节点的左子节点
changeNode.rightChild = root.rightChild;
root = changeNode;
}else{//即要替换的节点是删除节点的左子节点的所有右子节点中的最后一个没有右子节点的右子节点
Node changeParent = (Node)map.get("parent");
changeParent.rightChild = changeNode.leftChild;
changeNode.rightChild = root.rightChild;
changeNode.leftChild = root.leftChild;
root = changeNode;
}
return true;
}
if(isLeftChange){//即要替换的节点是删除节点的左子节点
if(isLeft){//如果删除的是左子节点
/*
* 删除逻辑是将要删除的节点的父节点的左子节点指向为替换节点
* 然后将替换节点的右子节点指向为删除节点的右子节点
*/
parent.leftChild = changeNode;
}else{//如果删除的是右子节点
parent.rightChild = changeNode;
}
changeNode.rightChild = child.rightChild;
}else{//要替换的是删除节点的左子节点的所有右子节点中的某个节点
Node changeParent = (Node)map.get("parent");
changeParent.rightChild = changeNode.leftChild;
/*
* 替换节点的父节点的右子节点指向替换节点的左子节点
*删除的逻辑是将要删除的父节点的子节点指向替换节点
*替换节点的右子节点指向删除节点的右子节点
*替换节点的左子节点指向删除节点的左子节点
*/
if(isLeft){
parent.leftChild = changeNode;
}else{
parent.rightChild = changeNode;
}
changeNode.rightChild = child.rightChild;
changeNode.leftChild = child.leftChild;
}
return true;
}
/**
* 删除节点有两个子节点时,查询出可以替换该节点的子节点(中序后继)
* 查询的方式是:先查询该删除节点的左子节点,如果左子节点没有右子节点,则要替换删除节点的就是这个左子节点
* 如果左子节点有右子节点,则继续看该右子节点是否有右子节点,直到找到右子节点不再有右子节点的节点,则该节点就是要替换删除节点的右子节点
* @param node
* @return
*/
public Map<String,Object> searchNode(Node node){
Map<String,Object> map = new HashMap<String,Object>();
Node parent = node;
Node changeNode = node.leftChild;
if(changeNode.rightChild == null){//这里判断,如果要替换的正好就是删除节点的左子节点(即该左子节点没有右子节点),则直接将该左子节点返回
map.put("changeNode", changeNode);
map.put("isLeft", true);
return map;
}
while(true){
if(changeNode.rightChild == null){
break;
}
parent = changeNode;
changeNode = changeNode.rightChild;
}
map.put("parent", parent);
map.put("changeNode", changeNode);
map.put("isLeft", false);
return map;
}
/**
* 前序遍历
* 郭胜
*/
public void frontOrder(Node node){
//判断node 为null则return
if(node != null){
//1.访问根节点
System.out.println(node.index + "\t" + node.data);
//2.前序遍历左子树
frontOrder(node.leftChild);
//3.前序遍历右子树
frontOrder(node.rightChild);
}
}
/**
* 中序遍历
* 郭胜
* @param node
*/
public void centerOrder(Node node){
if(node != null){
//1中序遍历左子树
centerOrder(node.leftChild);
//2.访问根节点
System.out.println(node.index +"\t" + node.data);
//3.中序遍历右节点
centerOrder(node.rightChild);
}
}
/**
* 后序遍历
* 郭胜
* @param node
*/
public void afterOrder(Node node){
if(node != null){
//1.后序遍历左子树
afterOrder(node.leftChild);
//2.后序遍历右子树
afterOrder(node.rightChild);
//3.访问根节点
System.out.println(node.index + "\t" + node.data);
}
}
}
package eight11one;
import org.junit.Test;
/**
* 二叉树测试类
* 郭胜
* @author 180719-2
*
*/
public class BinaryTreeTest {
@Test
public void test1(){
BinaryTree bt = new BinaryTree();
bt.insertBinaryTree(34, "guosheng34");
bt.insertBinaryTree(10, "郭胜10");
bt.insertBinaryTree(99, "guosheng99");
bt.insertBinaryTree(23,"guosheng23");
bt.insertBinaryTree(8,"guosheng8");
bt.insertBinaryTree(50,"guosheng50");
bt.insertBinaryTree(98,"guosheng98");
bt.insertBinaryTree(41,"guosheng41");
bt.insertBinaryTree(120,"guosheng120");
bt.insertBinaryTree(72,"guosheng72");
bt.insertBinaryTree(88,"guosheng88");
bt.insertBinaryTree(130,"guosheng130");
System.out.println("*******************************");
bt.centerOrder(bt.root);
System.out.println("*******************************");
bt.delete(34);
System.out.println("*******************************");
bt.centerOrder(bt.root);
System.out.println("*******************************");
/*System.out.println(bt.root.data +"\t" + bt.root.index);
System.out.println(bt.root.leftChild.data +"\t" + bt.root.leftChild.index);
System.out.println(bt.root.rightChild.data + "\t" + bt.root.rightChild.index);
System.out.println(bt.root.leftChild.rightChild.data + "\t" + bt.root.leftChild.rightChild.index);
System.out.println("***********************************************");
Node searchNode = bt.searchFromBinaryTree(0);
System.out.println(searchNode.index + "\t" + searchNode.data);*/
}
}