zoukankan      html  css  js  c++  java
  • Java实现二叉搜索树及相关操作

    package com.tree;
    
    import com.tree.BitNode;
    
    /**
     *
     * 二叉搜索树:一个节点的左子节点的关键字小于这个节点。右子节点的关键字大于或等于这个父节点
     *
     * 注意:不适合插入同样关键字的节点
    与平衡二叉树比較:不适合插入有序序列
     */
    public class SearchBinTree {
    
        public static void main(String[] args) {
    
            BitNode root = new BitNode();
            root.data = 6;
            int[] arr = { 1, 3, 9, 7, 0, 4 };
            for (int i = 0; i < arr.length; i++) {
                insert(root, arr[i]);
            }
            BinTree.levelTraverse(root);
            BitNode successor = getSuccessor(root);
            System.out.println("successor:" + successor.data);
    
    
            System.out.println("delete : " + delete(root, 1));
            BinTree.levelTraverse(root);
    
            // BinTree.levelTraverse(root);
            //
            // BitNode min = getMinNode(root);
            // System.out.println("min = " + min.data);
            //
            // BitNode max = getMaxNode(root);
            // System.out.println("max = " + max.data);
    
        }
    
        // 构造二叉搜索树
        public static void insert(BitNode root, int data) {
            BitNode node = new BitNode();
            node.data = data;
            if (root == null) {
                root = node;
            } else {
                if (data < root.data) {
                    if (root.lchild == null) {
                        root.lchild = node;
                    } else {
                        insert(root.lchild, data);
                    }
                } else {
                    if (root.rchild == null) {
                        root.rchild = node;
                    } else {
                        insert(root.rchild, data);
                    }
                }
    
            }
        }
    
        // 查找最小节点
        public static BitNode getMinNode(BitNode root) {
            BitNode current, min = null;
            current = root;
            while (current != null) {
                min = current;
                current = current.lchild;
            }
            return min;
        }
    
        // 查找最大节点
        public static BitNode getMaxNode(BitNode root) {
            BitNode current, max = null;
            current = root;
            while (current != null) {
                max = current;
                current = current.rchild;
            }
            return max;
        }
    
        // 查找
        public static BitNode find(BitNode root, int data) {
            BitNode current = root;
            while (current.data != data) {
                if (data < current.data)
                    current = current.lchild;
                else
                    current = current.rchild;
                if (current == null)
                    return null;
            }
            return current;
        }
    
        // 得到简要被删除的节点的后继(中序遍历)
        public static BitNode getSuccessor(BitNode delNode) {
            BitNode successorParent = delNode;
            BitNode successor = delNode;
            BitNode current = delNode.rchild;
            while (current != null) {
                successorParent = successor;
                successor = current;
                current = current.lchild;
            }
    
            if (successor != delNode.rchild) {
                successorParent.lchild = successor.rchild;
                successor.rchild = delNode.rchild;
            }
            return successor;
        }
    
        // 删除一个节点----?删除根节点时会出现错误
        public static boolean delete(BitNode root, int data) {
            BitNode current = root;
            BitNode parent = root;
            boolean isLeftChild = true;
    
            while (current.data != data) {// search for node
                parent = current;
                if (data < current.data) { // go left?

    isLeftChild = true; current = current.lchild; } else { // go right?

    isLeftChild = false; current = current.rchild; } if (current == null) { // end of th return false; // didn't find it } } // found node to delete // if no children, simply delete it if (current.lchild == null && current.rchild == null) { if (current == root) {// if root root = null; // tree is empty } else if (isLeftChild) { parent.lchild = null;// disconnect } else { // from parent parent.rchild = null; } } else if (current.rchild == null) {// if no right child replace with // left subtree if (current == root) { root = current.lchild; } else if (isLeftChild) { parent.rchild = current.lchild; } else { parent.rchild = current.lchild; } } else if (current.lchild == null) {// if no left child replace with // right subtree if (current == root) { root = current.rchild; } else if (isLeftChild) { parent.lchild = current.rchild; } else { parent.rchild = current.rchild; } } else {// two chiledren ,so replace with inorde successor //get successor of node to delete(current) BitNode successor = getSuccessor(current); //connect parent of current to successor instead if(current == root){ root = successor; } else if(isLeftChild){ parent.lchild = successor; } else { parent.rchild = successor; } //connect successor to current's left child successor.lchild = current.lchild; } return true; } }

  • 相关阅读:
    DPDK 多进程
    dpdk helloword
    dpdk-ring-ping
    【基于python实现UI自动化】3.0 selenium
    算法题:实现 strStr()函数
    python程序设计:某体操比赛共有10名运动员参加,12名评委将根据运动员表现进行评分(满分10分),请编写Python程序,解决下列问题:
    Scrapy框架实战(五):通用爬虫 CrawlSpider
    python爬虫爬取_高德地图_主要城市迁徙意愿排行榜_19年至今数据
    年轻就该多尝试,教你20小时Get一项新技能
    LeetCode:283.移动零——简单
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/7140605.html
Copyright © 2011-2022 走看看