二叉树的节点数+增删改

该代码实现了一个二叉搜索树,包括获取节点数量、插入节点、查找节点以及删除节点的功能。获取节点数通过递归遍历计算;插入节点根据值大小决定插入位置;查找节点返回匹配值及其父节点;删除节点处理不同情况,如无子节点、单子节点和双子节点。
摘要由CSDN通过智能技术生成

二叉树的节点

1.获得二叉树的节点个数

public int getAns(TreeNode root) {
       // write your code here
        HashMap map = new HashMap();
        map.put("max",1);
        if(root == null) {
            return 0;
        }
        findMax(root,map);
        return (int)map.get("max");
    }
    public void findMax(TreeNode root,HashMap map){
        //代表已到叶子节点
        if(root.left == null&& root.right == null) {
            /*int target = (int)map.get("max");
                target++;
                map.put("max",target);*/
                return;
        }
        if(root.left != null){
            map.put("max",(int)map.get("max")+1);
            findMax(root.left,map);
        }
        if(root.right != null){
            map.put("max",(int)map.get("max")+1);
            findMax(root.right,map);
        }
    }

总结:

1.左边节点不为空
	节点数+1;
	递归;
	
2.右边节点不为空
	节点数+1;
	递归;

3.左右节点都为空,代表到达代表已到叶子节点

2.增删查

package search.binaryTreeSearch;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 二叉树类
 * 
 * @author jeker-chen
 *
 */
public class BinaryTree {
	private static Node orignNode = new Node();

	/**
	 * 插入节点
	 * 
	 * @param 要插入的节点
	 * @param orign
	 *            目标节点
	 */
	public void InsertTree(Node node, Node orign) {
		if (node.data < orign.data) {
			if (orign.leftNode != null) {
				InsertTree(node, orign.leftNode);
			} else {
				orign.leftNode = new Node(node.data);
			}
		} else if (node.data > orign.data) {
			if (orign.rightNode != null) {
				InsertTree(node, orign.rightNode);
			} else {
				orign.rightNode = new Node(node.data);
			}
		} else {
			// do nothing
		}
	}

	/**
	 * 二叉树查找
	 * 
	 * @param mubiao
	 *            所要查找的数据
	 * @param orign
	 *            二叉树的根节点
	 */
	public Map<Node, Node> searchNode(int mubiao, Node orign, Node father) {
		if (orign == null) {
			System.out.println("查找节点不存在!");
			return null;
		}
		if (orign.data == mubiao) {
			Map<Node, Node> map = new HashMap<>();
			map.put(orign, father);
			return map;
		} else if (orign.data < mubiao) {
			return searchNode(mubiao, orign.rightNode, orign);
		} else {
			return searchNode(mubiao, orign.leftNode, orign);
		}
	}

	/**
	 * 删除二叉树中指定的元素
	 * 
	 * @param orign
	 *            二叉树根节点
	 * @param mubiao
	 *            索要删除节点的数据
	 */
	public void deleteNode(Map<Node, Node> nodeMap) {
		Node mubiao = nodeMap.keySet().iterator().next();
		Node father = nodeMap.get(mubiao);
		// 若不存在左子树
		if (mubiao.leftNode == null) {
			// 将右子节点的数据赋值给目标节点
			if (father.leftNode == mubiao) {
				father.leftNode = mubiao.rightNode;
			} else {
				father.rightNode = mubiao.rightNode;
			}
		} else if (mubiao.rightNode == null) {
			if (father.leftNode == mubiao) {
				father.leftNode = mubiao.leftNode;
			} else {
				father.rightNode = mubiao.leftNode;
			}
		} else {
			Node left = mubiao.leftNode;
			if (left.rightNode == null) {
				father.leftNode = mubiao.leftNode;
				mubiao = null;
			} else {
				father = left;
				// 获得目标节点相邻节点
				while (left.rightNode != null) {
					father = left;
					left = left.rightNode;
				}
				mubiao.data = left.data;
				father.rightNode = null;
			}
		}
	}

	public Node Create(List<Integer> integers) {
		for (int i = 0; i < integers.size(); i++) {
			if (i == 0) {
				orignNode.data = integers.get(i);
			} else {
				Node node = new Node(integers.get(i));
				InsertTree(node, orignNode);
			}
		}
		return orignNode;
	}

	/*
	 * 前序遍历二叉树的节点
	 */
	public void preorderTraversal(Node T, int level) {
		if (T != null) {
			System.out.println(T.data + "在第" + level + "层");
			preorderTraversal(T.leftNode, level + 1);
			preorderTraversal(T.rightNode, level + 1);
		}
	}

	public static void main(String[] args) {
		BinaryTree binaryTree = new BinaryTree();
		List<Integer> integers = new LinkedList<>();
		integers.add(5);
		integers.add(7);
		integers.add(3);
		integers.add(1);
		integers.add(2);
		integers.add(6);
		integers.add(4);
		
		binaryTree.Create(integers);
		binaryTree.preorderTraversal(orignNode, 0);
		System.out.println("=====================");
		Map<Node, Node> nodeMap = binaryTree.searchNode(1, orignNode, orignNode);
		// System.out.println(node.data);
		System.out.println(nodeMap.keySet().iterator().next().data);
		System.out.println(nodeMap.get(nodeMap.keySet().iterator().next()).data);
		binaryTree.deleteNode(nodeMap);
		binaryTree.preorderTraversal(orignNode, 0);

	}

}

总结:

	public void InsertTree(Node node, Node orign) {
		if (node.data < orign.data) {
			if (orign.leftNode != null) {
				InsertTree(node, orign.leftNode);
			} else {
				orign.leftNode = new Node(node.data);
			}
		} else if (node.data > orign.data) {
			if (orign.rightNode != null) {
				InsertTree(node, orign.rightNode);
			} else {
				orign.rightNode = new Node(node.data);
			}
		} else {
			// do nothing
		}
	}


1.插入: 
	插入的数据要小于根节点,否则什么都不做
	如果根节点的左节点不为空,递归插入
	如果根节点的左节点为空,如图所示
	

在这里插入图片描述

/**
	 * 二叉树查找
	 * 
	 * @param mubiao
	 *            所要查找的数据
	 * @param orign
	 *            二叉树的根节点
	 */
	public Map<Node, Node> searchNode(int mubiao, Node orign, Node father) {
		if (orign == null) {
			System.out.println("查找节点不存在!");
			return null;
		}
		if (orign.data == mubiao) {
			Map<Node, Node> map = new HashMap<>();
			map.put(orign, father);
			return map;
		} else if (orign.data < mubiao) {
			return searchNode(mubiao, orign.rightNode, orign);
		} else {
			return searchNode(mubiao, orign.leftNode, orign);
		}
	}

2.查询
	如果根节点为空,返回当前节点不存在
	如果刚好相等,返回一个map

在这里插入图片描述

3.删除






/**
	 * 删除二叉树中指定的元素
	 * 
	 * @param orign
	 *            二叉树根节点
	 * @param mubiao
	 *            索要删除节点的数据
	 */
	public void deleteNode(Map<Node, Node> nodeMap) {
		Node mubiao = nodeMap.keySet().iterator().next();
		Node father = nodeMap.get(mubiao);
		// 若不存在左子树
		if (mubiao.leftNode == null) {
			// 将右子节点的数据赋值给目标节点
			if (father.leftNode == mubiao) {
				father.leftNode = mubiao.rightNode;
			} else {
				father.rightNode = mubiao.rightNode;
			}
		} else if (mubiao.rightNode == null) {
			if (father.leftNode == mubiao) {
				father.leftNode = mubiao.leftNode;
			} else {
				father.rightNode = mubiao.leftNode;
			}
		} else {
			Node left = mubiao.leftNode;
			if (left.rightNode == null) {
				father.leftNode = mubiao.leftNode;
				mubiao = null;
			} else {
				father = left;
				// 获得目标节点相邻节点
				while (left.rightNode != null) {
					father = left;
					left = left.rightNode;
				}
				mubiao.data = left.data;
				father.rightNode = null;
			}
		}
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值