`

数据结构 二叉树(B树)

 
阅读更多

BTree

特征:

1.一个节点最多只有两个子节点,其中左子节点的关键字小于这个节点,右子节点的关键字大于等于该节点。

2.执行查找,插入,删除的时间复杂度都是:O(logN)。

3.遍历有中序,前序,后序。前,中和后只是在递归的时候先输出左子,自己或右子的顺序。可以通过中排序,按左子,自己,右子的顺序就是升序,反之则是降序。

4.最大值是树的右边底层叶子;最小值是左边底层叶子。

 

JAVA代码实现:

 

 

Java代码  收藏代码
  1. package org.acooly.datastructure.btree;  
  2.   
  3. import java.util.Random;  
  4.   
  5. /** 
  6.  * 特征:一个节点的左子节点的关键字小于这个节点,右子节点的关键字大于等于该节点。 
  7.  *  
  8.  * @author zhangpu 
  9.  *  
  10.  */  
  11. public class BinaryTree {  
  12.   
  13.     /** 根节点 */  
  14.     private Node root;  
  15.   
  16.     /** 
  17.      * 查找 
  18.      *  
  19.      * @param key 
  20.      * @return 
  21.      */  
  22.     public Node find(int key) {  
  23.         if (root == null) {  
  24.             return null;  
  25.         }  
  26.         Node current = root;  
  27.         while (current.getKey() != key) {  
  28.             if (key < current.getKey()) {  
  29.                 // 小于本节点在左边  
  30.                 current = current.getLeftNode();  
  31.             } else {  
  32.                 // 大于等于本节点在右边  
  33.                 current = current.getRightNode();  
  34.             }  
  35.             if (current == null) {  
  36.                 // 搜索到最后叶子为空,表示没有找到  
  37.                 return null;  
  38.             }  
  39.         }  
  40.         return current;  
  41.     }  
  42.   
  43.     public Node getParent(int key) {  
  44.         if (root == null) {  
  45.             return null;  
  46.         }  
  47.         Node current = root;  
  48.         Node parent = root;  
  49.         while (current.getKey() != key) {  
  50.             if (key < current.getKey()) {  
  51.                 // 小于本节点在左边  
  52.                 parent = current;  
  53.                 current = current.getLeftNode();  
  54.             } else {  
  55.                 // 大于等于本节点在右边  
  56.                 parent = current;  
  57.                 current = current.getRightNode();  
  58.             }  
  59.             if (current == null) {  
  60.                 // 搜索到最后叶子为空,表示没有找到  
  61.                 return null;  
  62.             }  
  63.         }  
  64.         return parent;  
  65.     }  
  66.   
  67.     /** 
  68.      * 插入 
  69.      *  
  70.      * @param key 
  71.      * @param value 
  72.      */  
  73.     public void insert(int key, Object value) {  
  74.         Node node = new Node(key, value);  
  75.         if (root == null) {  
  76.             root = node;  
  77.             return;  
  78.         }  
  79.         Node current = root;  
  80.         while (true) {  
  81.             if (key < current.getKey()) {  
  82.                 if (current.getLeftNode() == null) {  
  83.                     current.setLeftNode(node);  
  84.                     return;  
  85.                 } else {  
  86.                     current = current.getLeftNode();  
  87.                 }  
  88.             } else {  
  89.                 if (current.getRightNode() == null) {  
  90.                     current.setRightNode(node);  
  91.                     return;  
  92.                 } else {  
  93.                     current = current.getRightNode();  
  94.                 }  
  95.             }  
  96.         }  
  97.   
  98.     }  
  99.   
  100.     /** 
  101.      * 中遍历(升序) 
  102.      *  
  103.      * @param startNode 
  104.      */  
  105.     public void inOrderAsc(Node startNode) {  
  106.   
  107.         if (startNode != null) {  
  108.             inOrderAsc(startNode.getLeftNode());  
  109.             System.out.println(startNode);  
  110.             inOrderAsc(startNode.getRightNode());  
  111.         }  
  112.   
  113.     }  
  114.   
  115.     /** 
  116.      * 中遍历(降序) 
  117.      *  
  118.      * @param startNode 
  119.      */  
  120.     public void inOrderDesc(Node startNode) {  
  121.         if (startNode != null) {  
  122.             inOrderDesc(startNode.getRightNode());  
  123.             System.out.println(startNode);  
  124.             inOrderDesc(startNode.getLeftNode());  
  125.         }  
  126.   
  127.     }  
  128.   
  129.     /** 
  130.      * 最大值 算法:树中最底层的右子叶 
  131.      *  
  132.      * @return 
  133.      */  
  134.     public Node getMax() {  
  135.         Node current = root;  
  136.         while (current.getRightNode() != null) {  
  137.             current = current.getRightNode();  
  138.         }  
  139.         return current;  
  140.     }  
  141.   
  142.     /** 
  143.      * 算法:树中最底层的左子叶 
  144.      *  
  145.      * @return 
  146.      */  
  147.     public Node getMin() {  
  148.         return getMin(root);  
  149.     }  
  150.   
  151.     /** 
  152.      * 指定节点的最小节点,如果指定节点为root,则是树的最小节点 
  153.      *  
  154.      * @param localRoot 
  155.      * @return 
  156.      */  
  157.     private Node getMin(Node localRoot) {  
  158.         Node current = localRoot;  
  159.         while (current.getLeftNode() != null) {  
  160.             current = current.getLeftNode();  
  161.         }  
  162.         return current;  
  163.     }  
  164.   
  165.     /** 
  166.      * 删除节点存在3中情况 <li>目标节点是叶子:直接删除,置为null <li> 
  167.      * 目标节点只有一个子节点:如果目标节点是在父节点的左边,直接使用子节点作为父节点的左子,反正则为右子。 <li> 
  168.      * 目标节点有两个子节点:找到后继节点,作为目标节点父节点的对应子节点。(后继:目标节点子节点中大于目标节点最小的个。路径:目标节点右子的最小节点。) 
  169.      *  
  170.      * @param key 
  171.      */  
  172.     public void delete(int key) {  
  173.   
  174.         Node target = find(key);  
  175.         if (target == null) {  
  176.             return;  
  177.         }  
  178.   
  179.         boolean leftExsit = (target.getLeftNode() != null ? true : false);  
  180.         boolean rightExsit = (target.getRightNode() != null ? true : false);  
  181.         // 第一种情况,目标是叶子,直接设置为null  
  182.         if (!leftExsit && !rightExsit) {  
  183.             target = null;  
  184.             return;  
  185.         }  
  186.   
  187.         // 获得目标的父节点  
  188.         Node parent = getParent(key);  
  189.         Node child = null;  
  190.         if (leftExsit != rightExsit) {  
  191.             // 第二种情况:只有一个子  
  192.             child = (leftExsit ? target.getLeftNode() : target.getRightNode());  
  193.         } else {  
  194.             // 第三种情况:有两个子  
  195.             Node rightChild = target.getRightNode();  
  196.             child = getMin(rightChild);  
  197.             getParent(child.getKey()).setLeftNode(null);  
  198.             child.setRightNode(rightChild);  
  199.         }  
  200.   
  201.         if (parent == null) {  
  202.             root = child;  
  203.             target = null;  
  204.             return;  
  205.         }  
  206.   
  207.         if (parent.getLeftNode() != null && target.getKey() < parent.getLeftNode().getKey()) {  
  208.             // 目标是父的左子  
  209.             parent.setLeftNode(child);  
  210.         } else {  
  211.             // 目标是父的右子  
  212.             parent.setRightNode(child);  
  213.         }  
  214.         target = null;  
  215.     }  
  216.   
  217.     public Node getRoot() {  
  218.         return root;  
  219.     }  
  220.   
  221.     public static void main(String[] args) {  
  222.         BinaryTree tree = new BinaryTree();  
  223.         Random random = new Random();  
  224.         // INSERT  
  225.         for (int i = 1; i <= 10; i++) {  
  226.             int key = random.nextInt(100);  
  227.             tree.insert(key, "value" + key);  
  228.         }  
  229.         int key = 0;  
  230.         tree.insert(key, "value" + key);  
  231.         System.out.println("TARGET key: " + key);  
  232.         // FIND  
  233.         System.out.println("FIND: " + tree.find(key));  
  234.         // GETPARENT  
  235.         System.out.println("PARENT: " + tree.getParent(key));  
  236.         // MIX  
  237.         System.out.println("MAX: " + tree.getMax());  
  238.         // MIN  
  239.         System.out.println("MIN: " + tree.getMin());  
  240.         tree.delete(key);  
  241.         System.out.println();  
  242.         System.out.println("中遍历(升序):");  
  243.         tree.inOrderAsc(tree.getRoot());  
  244.         System.out.println("中遍历(降序):");  
  245.         tree.inOrderDesc(tree.getRoot());  
  246.     }  
  247.   
  248. }  

   节点类:

 

Java代码  收藏代码
  1. package org.acooly.datastructure.btree;  
  2.   
  3. /** 
  4.  * BTree 节点 
  5.  *  
  6.  * @author zhangpu 
  7.  *  
  8.  */  
  9. public class Node {  
  10.   
  11.     /** 节点KEY */  
  12.     private int key;  
  13.     private Object value;  
  14.     /** 左子节点 */  
  15.     private Node leftNode;  
  16.     /** 右子节点 */  
  17.     private Node rightNode;  
  18.   
  19.     public Node() {  
  20.         super();  
  21.     }  
  22.   
  23.     public Node(int key, Object value) {  
  24.         super();  
  25.         this.key = key;  
  26.         this.value = value;  
  27.     }  
  28.   
  29.     public int getKey() {  
  30.         return key;  
  31.     }  
  32.   
  33.     public void setKey(int key) {  
  34.         this.key = key;  
  35.     }  
  36.   
  37.     public Node getLeftNode() {  
  38.         return leftNode;  
  39.     }  
  40.   
  41.     public void setLeftNode(Node leftNode) {  
  42.         this.leftNode = leftNode;  
  43.     }  
  44.   
  45.     public Node getRightNode() {  
  46.         return rightNode;  
  47.     }  
  48.   
  49.     public void setRightNode(Node rightNode) {  
  50.         this.rightNode = rightNode;  
  51.     }  
  52.   
  53.     public Object getValue() {  
  54.         return value;  
  55.     }  
  56.   
  57.     public void setValue(Object value) {  
  58.         this.value = value;  
  59.     }  
  60.   
  61.     @Override  
  62.     public String toString() {  
  63.         return String.valueOf(key) + "=" + value;  
  64.     }  
  65.       
  66.     @Override  
  67.     public boolean equals(Object obj) {  
  68.         if(obj instanceof Node){  
  69.             Node n = (Node)obj;  
  70.             if(n.getKey() != getKey()){  
  71.                 return false;  
  72.             }  
  73.         }else{  
  74.             return false;  
  75.         }  
  76.         return true;  
  77.     }  
  78.   
  79. }  

分享到:
评论

相关推荐

    高级数据结构代码 红黑树 二叉树 B树

    高级数据结构代码 红黑树 二叉树 B树

    数据结构 树、二叉树的数据结构 哈夫曼树

    2. 实现哈夫曼树数据结构,使用哈夫曼树完成如下文档的编码与译码,假设该文档由5种符号字符(A、B、C、D、E)构成 ABACDEABBCEABAACCCDEACCBAABCCCA 3. 选做:实现二叉树的中序遍历线索化数据结构 4. 选做:使用...

    数据结构树和二叉树习题.pdf

    数据结构树和二叉树习题 数据结构树和二叉树习题.pdf 是一份涵盖了树和二叉树相关知识点的习题集。下面是对标题、描述、标签和部分内容的详细解释和相关知识点总结: 一、树和二叉树的基本概念 * 树是一种数据...

    数据结构 实验3:树和二叉树的应用-哈夫曼编码设计

    数据结构完整实验报告 实验3:树和二叉树的应用-哈夫曼编码设计

    数据结构实验报告-实现二叉树的基本操作-用顺序存储和链式存储结构

    参考资料:《数据结构》(C语言版)严蔚敏&&吴伟民&&米宁著 要求选用顺序存储结构和二叉链表存储结构实现抽象数据类型二叉树的基本操作。有个亮点是利用字符在dos界面显示二叉树的结构形态。 里面包含了完整的源程序和...

    二叉树基本操作 数据结构 实验报告

    二叉树基本操作数据结构实验报告 本实验报告旨在实现二叉树的基本操作,包括定义二叉树的结构类型、初始化二叉树、检查二叉树是否为空、前序、中序、后序遍历树的方式、求树的深度、求树的结点数目、清空二叉树等九...

    数据结构二叉树计算

    二叉树、哈夫曼树 【设计要求】 根据电文中各字符的权值构造哈夫曼树,并生成哈夫曼编码。 【难度】★★★★☆ 【输入示例】 请输入各个字符:A B C D E 请输入相应权值:5 9 2 3 1 【输出示例】 各字符的哈夫曼...

    二叉树和树PPT学习教案.pptx

    树的结构差别是指树的不同结构,例如二叉树、B树、 redhead树等。树的性质是指树的特征,例如树的高度、树的度等。 树的应用是指使用树来解决实际问题的过程。常见的树应用包括文件系统、数据库索引、数据压缩等。 ...

    数据结构实验-二叉树的建立、遍历、摩斯电码(哈夫曼树)的编码与解码实验代码

    数据结构关于二叉树的建立遍历以及应用二叉树进行编解码 实验要求 必做部分 1. 小明会按照前序的方式输入一棵二叉树。例如,输入$ACG##H##D##BE#I##F##的话,代表了下面这棵树: 2. 请分别按照前序、中序、后序...

    数据结构学习笔记——使用draw.io绘制的树结构图

    树结构 draw.io图 二叉树、顺序树、树的链式存储结构、树的数组存储结构、欧拉树 绘图软件为免费开源软件draw.io

    第六章 树和二叉树作业及答案(100分).docx

    7. 有abc三个结点的右单枝二叉树的顺序存储结构应该用( )示意。 A. a b c B. a b ^ c C. a b ^ ^ c D. a ^ b ^ ^ ^ c 8. 一颗有2046个结点的完全二叉树的第10层上共有( )个结点。 A. 511 B. 512 C. 1023...

    数据结构实验二叉树.doc

    本实验报告的主要目的是熟悉二叉树的存储结构和应用,设计一个十进制的四则运算计算器,并实现对算术表达式的求值运算。 一、实验目的 * 掌握二叉树的各种存储结构 * 熟悉对二叉树的基本操作 * 设计一个十进制的四...

    精心整理史上最全的数据结构flash演示动画,共5个版本,祝大家考研成功!

    \数据结构flash演示\版本1\9-2-1B-树.swf \数据结构flash演示\版本1\9-2-1顺序查找.swf \数据结构flash演示\版本1\9-2-2折半查找.swf \数据结构flash演示\版本1\9-2-3折半查找查找长度.swf \数据结构flash演示\...

    24.二叉树.wmv(目前数据结构最好的视频,共42集,需要哪一集的知识自己下,一集3积分)

    C/C++实现 数据结构与算法视频教程 01交换算法 02冒泡排序 03选择排序 04顺序排序 05折半排序 06递归算法 07递归折半查找 08算法_perm 09插入排序 10快速排序 11归并排序 12顺序栈 13顺序队列 14链表的基本概率 15...

    东北大学数据结构平时实验报告

    包含复数四则运算计算器(顺序表、链表),迷宫问题(栈和队列),图遍历生成树演示(树和图的应用),3阶B-树问题(查找和排序)。四次实验报告以及源码

    数据结构 平衡二叉树.cpp

    程序输入一个字符串(只包含小写字母),请按照字符的输入顺序建立平衡二叉排序树,并分别输出二叉树的先序序列、中序序列和后序序列,最后输出该二叉树向左旋转 90 度后的结构。 例如:向左旋转 90 度后,以每层向...

    二叉树排序树建立及平衡处理

    在根指针T所指的平衡二叉排序树中递归的查找其元素值等于e的数据元素, 若查找成功,则指针p指向该数据元素结点,并返回TRUE,否则指针p 指向查找路径上访问的最后一个结点并返回FALSE,指针f指向T的双亲, 其初始...

    【C语言->数据结构与算法】->树与二叉树概念&哈夫曼树的构造

    树状图是一种数据结构,它是由n(n&gt;=1)个有限结点组成一个具有层次关系的集合。把它叫做“树”是因为它看起来像一棵倒挂的树,也就是说它是根朝上,而叶朝下的。它具有以下的特点: 每个结点有零个或多个子结点;...

    考研-数据结构-殷人昆.zip

    1.1 针对考研数据结构的代码书写规范以及C 与C 语言基础1 1.1.1 考研综合应用题中算法设计部分的代码书写规范1 1.1.2 考研中的C 与C 语言基础3 1.2 算法的时间复杂度与空间复杂度分析基础 12 1.2.1 考研中的算法时间...

    C++数据结构知识点与经典算法整理

    13、B树、B-树、B+树、B*树、红黑树和trie树 44 14、最小生成树算法之Prim算法(C++实现) 49 15、最小生成树之kruskal算法 58 16、单源最短路径 62 三、算法部分 65 1、算法简介 65 2、实际算法 67 3、常用算法 73 四...

Global site tag (gtag.js) - Google Analytics