`

数据结构-二叉树(BTree)

阅读更多

BTree

特征:

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

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

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

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

 

JAVA代码实现:

 

 

package org.acooly.datastructure.btree;

import java.util.Random;

/**
 * 特征:一个节点的左子节点的关键字小于这个节点,右子节点的关键字大于等于该节点。
 * 
 * @author zhangpu
 * 
 */
public class BinaryTree {

	/** 根节点 */
	private Node root;

	/**
	 * 查找
	 * 
	 * @param key
	 * @return
	 */
	public Node find(int key) {
		if (root == null) {
			return null;
		}
		Node current = root;
		while (current.getKey() != key) {
			if (key < current.getKey()) {
				// 小于本节点在左边
				current = current.getLeftNode();
			} else {
				// 大于等于本节点在右边
				current = current.getRightNode();
			}
			if (current == null) {
				// 搜索到最后叶子为空,表示没有找到
				return null;
			}
		}
		return current;
	}

	public Node getParent(int key) {
		if (root == null) {
			return null;
		}
		Node current = root;
		Node parent = root;
		while (current.getKey() != key) {
			if (key < current.getKey()) {
				// 小于本节点在左边
				parent = current;
				current = current.getLeftNode();
			} else {
				// 大于等于本节点在右边
				parent = current;
				current = current.getRightNode();
			}
			if (current == null) {
				// 搜索到最后叶子为空,表示没有找到
				return null;
			}
		}
		return parent;
	}

	/**
	 * 插入
	 * 
	 * @param key
	 * @param value
	 */
	public void insert(int key, Object value) {
		Node node = new Node(key, value);
		if (root == null) {
			root = node;
			return;
		}
		Node current = root;
		while (true) {
			if (key < current.getKey()) {
				if (current.getLeftNode() == null) {
					current.setLeftNode(node);
					return;
				} else {
					current = current.getLeftNode();
				}
			} else {
				if (current.getRightNode() == null) {
					current.setRightNode(node);
					return;
				} else {
					current = current.getRightNode();
				}
			}
		}

	}

	/**
	 * 中遍历(升序)
	 * 
	 * @param startNode
	 */
	public void inOrderAsc(Node startNode) {

		if (startNode != null) {
			inOrderAsc(startNode.getLeftNode());
			System.out.println(startNode);
			inOrderAsc(startNode.getRightNode());
		}

	}

	/**
	 * 中遍历(降序)
	 * 
	 * @param startNode
	 */
	public void inOrderDesc(Node startNode) {
		if (startNode != null) {
			inOrderDesc(startNode.getRightNode());
			System.out.println(startNode);
			inOrderDesc(startNode.getLeftNode());
		}

	}

	/**
	 * 最大值 算法:树中最底层的右子叶
	 * 
	 * @return
	 */
	public Node getMax() {
		Node current = root;
		while (current.getRightNode() != null) {
			current = current.getRightNode();
		}
		return current;
	}

	/**
	 * 算法:树中最底层的左子叶
	 * 
	 * @return
	 */
	public Node getMin() {
		return getMin(root);
	}

	/**
	 * 指定节点的最小节点,如果指定节点为root,则是树的最小节点
	 * 
	 * @param localRoot
	 * @return
	 */
	private Node getMin(Node localRoot) {
		Node current = localRoot;
		while (current.getLeftNode() != null) {
			current = current.getLeftNode();
		}
		return current;
	}

	/**
	 * 删除节点存在3中情况 <li>目标节点是叶子:直接删除,置为null <li>
	 * 目标节点只有一个子节点:如果目标节点是在父节点的左边,直接使用子节点作为父节点的左子,反正则为右子。 <li>
	 * 目标节点有两个子节点:找到后继节点,作为目标节点父节点的对应子节点。(后继:目标节点子节点中大于目标节点最小的个。路径:目标节点右子的最小节点。)
	 * 
	 * @param key
	 */
	public void delete(int key) {

		Node target = find(key);
		if (target == null) {
			return;
		}

		boolean leftExsit = (target.getLeftNode() != null ? true : false);
		boolean rightExsit = (target.getRightNode() != null ? true : false);
		// 第一种情况,目标是叶子,直接设置为null
		if (!leftExsit && !rightExsit) {
			target = null;
			return;
		}

		// 获得目标的父节点
		Node parent = getParent(key);
		Node child = null;
		if (leftExsit != rightExsit) {
			// 第二种情况:只有一个子
			child = (leftExsit ? target.getLeftNode() : target.getRightNode());
		} else {
			// 第三种情况:有两个子
			Node rightChild = target.getRightNode();
			child = getMin(rightChild);
			getParent(child.getKey()).setLeftNode(null);
			child.setRightNode(rightChild);
		}

		if (parent == null) {
			root = child;
			target = null;
			return;
		}

		if (parent.getLeftNode() != null && target.getKey() < parent.getLeftNode().getKey()) {
			// 目标是父的左子
			parent.setLeftNode(child);
		} else {
			// 目标是父的右子
			parent.setRightNode(child);
		}
		target = null;
	}

	public Node getRoot() {
		return root;
	}

	public static void main(String[] args) {
		BinaryTree tree = new BinaryTree();
		Random random = new Random();
		// INSERT
		for (int i = 1; i <= 10; i++) {
			int key = random.nextInt(100);
			tree.insert(key, "value" + key);
		}
		int key = 0;
		tree.insert(key, "value" + key);
		System.out.println("TARGET key: " + key);
		// FIND
		System.out.println("FIND: " + tree.find(key));
		// GETPARENT
		System.out.println("PARENT: " + tree.getParent(key));
		// MIX
		System.out.println("MAX: " + tree.getMax());
		// MIN
		System.out.println("MIN: " + tree.getMin());
		tree.delete(key);
		System.out.println();
		System.out.println("中遍历(升序):");
		tree.inOrderAsc(tree.getRoot());
		System.out.println("中遍历(降序):");
		tree.inOrderDesc(tree.getRoot());
	}

}

   节点类:

 

package org.acooly.datastructure.btree;

/**
 * BTree 节点
 * 
 * @author zhangpu
 * 
 */
public class Node {

	/** 节点KEY */
	private int key;
	private Object value;
	/** 左子节点 */
	private Node leftNode;
	/** 右子节点 */
	private Node rightNode;

	public Node() {
		super();
	}

	public Node(int key, Object value) {
		super();
		this.key = key;
		this.value = value;
	}

	public int getKey() {
		return key;
	}

	public void setKey(int key) {
		this.key = key;
	}

	public Node getLeftNode() {
		return leftNode;
	}

	public void setLeftNode(Node leftNode) {
		this.leftNode = leftNode;
	}

	public Node getRightNode() {
		return rightNode;
	}

	public void setRightNode(Node rightNode) {
		this.rightNode = rightNode;
	}

	public Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		this.value = value;
	}

	@Override
	public String toString() {
		return String.valueOf(key) + "=" + value;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Node){
			Node n = (Node)obj;
			if(n.getKey() != getKey()){
				return false;
			}
		}else{
			return false;
		}
		return true;
	}

}
 

 

分享到:
评论

相关推荐

    btree_btree数据结构_数据结构_二叉树btree_

    许多实际问题抽象出来的数据结构往往是二叉树形式,即使是一般的树也能简单地转换为二叉树,而且二叉树的存储结构及其算法都较为简单,因此二叉树显得特别重要。二叉树特点是每个结点最多只能有两棵子树,且有左右之...

    C/C++,树算法-二叉树(BTree)的基本数据结构

    C/C++,树算法——二叉树(BTree)的基本数据结构

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

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

    数据结构算法二叉树实现

    本主题聚焦于“数据结构算法二叉树实现”,这是一个关于如何在编程中实际操作和管理二叉树的实践性课题。二叉树是一种特殊的数据结构,它由节点构成,每个节点最多有两个子节点,通常分为左子节点和右子节点。 ...

    数据结构 C++ 二叉树建立

    ### 数据结构C++二叉树建立 #### 一、引言 在计算机科学领域,二叉树是一种常用的数据结构,广泛应用于算法设计、数据库管理、编译器优化等多个方面。本篇文章将详细介绍如何使用C++语言实现二叉树的创建,并通过...

    VC++2012编程演练数据结构《23》二叉树排序

    在本实践项目“VC++2012编程演练数据结构《23》二叉树排序”中,我们主要探讨的是如何使用C++编程语言,特别是VC++2012环境,来实现基于二叉树的数据结构进行排序。二叉树是一种非线性的数据结构,它由节点构成,每...

    数据结构课程设计+二叉树的动态演示C++

    在这个项目中,我们关注的是二叉树,这是一种特殊的数据结构,它在计算机科学的许多领域都有广泛应用,如搜索算法、文件系统和编译器设计等。二叉树由节点组成,每个节点最多有两个子节点,分别称为左子节点和右子...

    数据结构实验 二叉树先序遍历

    ### 数据结构实验:二叉树先序遍历 在计算机科学领域,数据结构是存储、组织数据的一种方式,使得数据可以高效地被访问和修改。其中,二叉树是一种非常重要的非线性数据结构,它是由一系列节点组成的,每个节点最多...

    二叉树遍历 数据结构

    根据给定的信息,本文将详细解释二叉树的数据结构、二叉树的创建与遍历方法,特别是基于C++语言实现的二叉树先序、中序遍历序列构建二叉树并进行后序遍历的过程。 ### 二叉树简介 二叉树是一种常用的数据结构,它...

    BTree数据结构课程设计C++版

    **BTree数据结构详解** BTree(B-Tree,或称B树)是一种自平衡的树数据结构,常用于数据库和文件系统中。它能够保持数据排序,允许对数据进行快速查找、添加和删除操作。BTree的主要特性是每个节点可以有多个子节点...

    C语言中文网 Go语言二叉树数据结构的应用 源码

    C语言中文网 Go语言二叉树数据结构的应用 源码。一开始以为“btree”是golang的包呢。后来发现不是。又找以为是“https://github.com/google/btree.git”。发现还不是。这才想起来可能是自己写的包。绕了一圈子。 ...

    btree.zip实现二叉树的可视化处理,很好的源码资料供大家学习。。

    通过研究“btree.zip”中的源码,我们可以学习到如何将抽象的数据结构与直观的图形界面结合,提升对二叉树的理解。这不仅可以加深对二叉树算法的掌握,也有助于提高MFC编程技巧,特别是对于UI设计和事件处理部分。在...

    swift-BTree高性能集合快速排序

    在压缩包"attaswift-BTree-8343537"中,可能包含了源代码、示例项目或者文档,供开发者学习和使用。通过这些资源,你可以更深入地了解如何在实际项目中集成和使用这个B-Tree实现,以提升你的应用性能。 总的来说,...

    PrintBTree.zip_print_btree_二叉树 图形化_打印二叉树

    在IT领域,二叉树是一种基础且重要的数据结构,它由节点构成,每个节点最多有两个子节点,通常称为左子节点和右子节点。本文将深入探讨如何使用C语言实现一个名为`print_btree`的功能,以图形化方式在控制台中打印...

    数据结构 作业哈夫曼、排序二叉树

    根据给定的文件信息,我们可以总结出以下关于“哈夫曼编码”与“排序二叉树”的相关知识点。 ### 哈夫曼编码 #### 定义 哈夫曼编码是一种广泛...这些知识点对于理解数据结构的基础概念以及实际应用都是非常重要的。

    Creat BTree.rar_btree_btree java_creatBtree_java 二叉树_二叉树 程序

    在IT领域,二叉树是一种基础且重要的数据结构,它在很多算法和程序设计中扮演着核心角色。这里我们关注的是“创建二叉树”的Java程序,这涉及到对二叉树概念的理解,以及如何用Java语言来实现它。 首先,我们要理解...

    数据结构二叉树的实验报告.docx

    这篇实验报告主要涉及二叉树的基本操作,包括创建、复制、...在实际应用中,这样的代码可以帮助理解和实现二叉树的相关操作,特别是在互联网领域,二叉树是数据结构和算法的重要组成部分,常用于搜索、排序和组织数据。

    BTree 树状数据结构台吗

    **BTree(B树)是一种自平衡的树数据结构,广泛应用于数据库和文件系统中,以支持高效的数据存取操作。这种数据结构的设计允许在磁盘等慢速存储设备上保持较高的性能。** BTree的核心特性包括以下几点: 1. **多路...

Global site tag (gtag.js) - Google Analytics