• 二叉树的增删改查


    package erTree;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;
    import java.util.Stack;
    public class erTree {
     public static void main(String[] args) {
      BiTree bt = new BiTree();
      bt.insert(50, 1.5);
      bt.insert(25, 1.7);
      bt.insert(75, 1.9);
      bt.insert(55, 1.0);
      bt.insert(20, 1.1);
      bt.insert(30, 2.0);
      bt.insert(5, 0.2);
      bt.insert(27, 0.1);
      bt.insert(40, 0.3);
      bt.insert(28, 0.7);
      HuffmanTreeBuilder hb =new HuffmanTreeBuilder();
      hb.ss();
     }
    }
    class Node {
     Person p1 = new Person();
     public Node left;
     public Node right;
     public int value;
    }
    class Person {
     int iDate;
     double fDate;
    }
    class BiTree {
     Node root;
     public BiTree() {
      this.root = root;
     }
     public Node find(int key) {
      Node currect = root;
      while (currect.p1.iDate != key) {
       if (key < currect.p1.iDate) {
        currect = currect.left;
       } else {
        currect = currect.right;
       }
       if (currect == null) {
        return null;
       }
      }
      return currect;
     }
     public void insert(int id, double dd) {
      Node newNode = new Node();
      newNode.p1.iDate = id;
      newNode.p1.fDate = dd;
      if (root == null) {
       root = newNode;
      } else {
       Node currect = root;
       Node parent;
       while (true) {
        parent = currect;
        if (id < currect.p1.iDate) {
         currect = currect.left;
         if (currect == null) {
          parent.left = newNode;
          return;
         }
        } else {
         currect = currect.right;
         if (currect == null) {
          parent.right = newNode;
          return;
         }
        }
       }
      }
     }
     public void delete(int key) {
      Node currect = root;
      Node parent = root;
      boolean isLeft = true;
      while (currect.p1.iDate != key && currect != null) {
       parent = currect;
       if (key > currect.p1.iDate) {
        isLeft = true;
        currect = currect.right;
       } else {
        isLeft = false;
        currect = currect.left;
       }
      }
      if (currect.left == null && currect.right == null) {
       if (currect == root) {
        root = null;
       } else if (isLeft) {
        parent.right = null;
       } else {
        parent.left = null;
       }
      } else if (currect.right == null) {
       if (currect == root) {
        root = root.left;
       } else if (isLeft) {
        parent.right = currect.left;
       } else {
        parent.left = currect.left;
       }
      } else if (currect.left == null) {
       if (currect == root) {
        root = root.right;
       } else if (isLeft) {
        parent.left = currect.right;
       } else {
        parent.right = currect.right;
       }
      } else if (currect.left != null && currect.right != null) {
       Node houji = currect.right;
       Node houjiparent = currect.right;
       while (houji.left != null) {
        houjiparent = houji;
        houji = houji.left;
       }
       if (currect == root) {
        root = houji;
       } else {
        currect.p1.iDate = houji.p1.iDate;
        if (houji.right == null) {
         houjiparent.left = null;
        } else {
         houjiparent.left = houji.right;
        }
       }
      }
     }
     // 先序遍历非递归
     public void inOrder(Node localRoot) {
      Stack<Node> stack = new Stack<Node>();
      if (localRoot != null) {
       stack.push(localRoot);
       while (!stack.empty()) {
        localRoot = stack.pop();
        System.out.print(localRoot.p1.iDate + ",");
        if (localRoot.right != null) {
         stack.push(localRoot.right);
        }
        if (localRoot.left != null) {
         stack.push(localRoot.left);
        }
       }
      }
     }
     // 先序遍历递归
     public void inOrder1(Node localRoot) {
      if (localRoot != null) {
       System.out.print(localRoot.p1.iDate + ",");
       inOrder(localRoot.left);
       inOrder(localRoot.right);
      }
     }
     // 中序遍历非递归
     public void iterativeInorder(Node localRoot) {
      Stack<Node> stack = new Stack<Node>();
      while (localRoot != null || !stack.empty()) {
       while (localRoot != null) {
        stack.push(localRoot);
        localRoot = localRoot.left;
       }
       if (!stack.empty()) {
        localRoot = stack.pop();
        System.out.print(localRoot.p1.iDate + ",");
        localRoot = localRoot.right;
       }
      }
     }
     // 中序遍历递归
     public void iterativeInorder1(Node localRoot) {
      if (localRoot != null) {
       iterativeInorder1(localRoot.left);
       System.out.print(localRoot.p1.iDate + ",");
       iterativeInorder1(localRoot.right);
      }
     }
     // 后序遍历
     public void iterativePostorder(Node node) {
      if (node == null)
       return;
      Stack<Node> s = new Stack<Node>();
      Node curNode; // 当前访问的结点
      Node lastVisitNode; // 上次访问的结点
      curNode = node;
      lastVisitNode = null;
      // 把currentNode移到左子树的最下边
      while (curNode != null) {
       s.push(curNode);
       curNode = curNode.left;
      }
      while (!s.empty()) {
       curNode = s.pop(); // 弹出栈顶元素
       // 一个根节点被访问的前提是:无右子树或右子树已被访问过
       if (curNode.right != null && curNode.right != lastVisitNode) {
        // 根节点再次入栈
        s.push(curNode);
        // 进入右子树,且可肯定右子树一定不为空
        curNode = curNode.right;
        while (curNode != null) {
         // 再走到右子树的最左边
         s.push(curNode);
         curNode = curNode.left;
        }
       } else {
        // 访问
        System.out.print(curNode.p1.iDate + ",");
        // 修改最近被访问的节点
        lastVisitNode = curNode;
       }
      } // while
     }
     // 求最小值
     public Node min() {
      Node current, last = null;
      current = root;
      while (current != null) {
       last = current;
       current = current.left;
      }
      return last;
     }
     // 求最大值
     public Node max() {
      Node current, last = null;
      current = root;
      while (current != null) {
       last = current;
       current = current.right;
      }
      return last;
     }
     // 求最大深度
     public int maxDepth(Node node) {
      if (node == null) {
       return 0;
      }
      int left = maxDepth(node.left);
      int right = maxDepth(node.right);
      return Math.max(left, right) + 1;
     }
     // 求最小深度
     public int minDepth(Node node) {
      if (node == null) {
       return 0;
      }
      int left = maxDepth(node.left);
      int right = maxDepth(node.right);
      return Math.min(left, right) + 1;
     }
     // 求二叉树中节点个数
     public int number(Node node) {
      if (node == null) {
       return 0;
      }
      int left = number(root.left);
      int right = number(root.right);
      return left + right + 1;
     }
    }
  • 相关阅读:
    es字符串字段类型的私有属性 建立索引的分析器 用于查询的分析器 短语查询优化
    限制索引、字段的自动创建 无模式与自定义 索引结构映射 切面搜索
    Lucene索引数计算
    API网关性能比较:NGINX vs. ZUUL vs. Spring Cloud Gateway vs. Linkerd API 网关出现的原因
    git 中 A C D M R T U X 分别解释
    通过 ulimit 改善系统性能
    机器重启 查看crontab执行历史记录crontab没有执行
    烂代码 git blame
    es 中 for in for of
    发散阶段 收集阶段 标识符 得分 副本角色
  • 原文地址:https://www.cnblogs.com/xhlwjy/p/9745222.html
Copyright © 2020-2023  润新知