简单数据结构实现——二叉查找树
来源:互联网 发布:下载国泰安数据 编辑:程序博客网 时间:2024/05/29 09:45
二叉查找树(Binary Search Tree)具有以下基本性质:
1.若它的左子树不空,则左子树上所有结点的值均小于它的根节点的值;
2.若它的右子树不空,则右子树上所有结点的值均大于它的根节点的值;
3.它的左右子树也分别为二叉排序树。
通常采取二叉链表作为二叉排序树的存储结构。在java中构造了如下的记node类来代表单个节点:
public class BinaryNode<AnyType> {AnyType data;BinaryNode<AnyType> left;BinaryNode<AnyType> right;public BinaryNode(AnyType newData){this(newData, null, null);}public BinaryNode(AnyType newData, BinaryNode<AnyType> lt, BinaryNode<AnyType> rt){data=newData;left=lt;right=rt;}}
初始化构建的时候,只要定义一个节点node=null,即可,在后续插入的过程中逐渐形成BST。
向一个二叉查找树b中插入一个节点s的算法:
1.若b是空树,则将s所指节点作为根节点插入,否则:
2.若s.data等于b的根节点的数据值,则返回,否则:
3.若s.data小于b的根节点的数据值,则把s所指节点插入到左子树中,否则:
4.把s所指节点插入到右子树中。
实现插入操作的代码如下:
public boolean insert(AnyType element){if(root==null){root=new BinaryNode<AnyType>(element);size++;return true;}else return insert(element,root);}public boolean insert(AnyType element, BinaryNode<AnyType> node){if(element.compareTo(node.data)<0){//Operate on left-child.if(node.left==null){BinaryNode<AnyType> newNode=new BinaryNode(element);node.left=newNode;size++;return true;}else{return insert(element,node.left);}}else{//Operate on right-child.if(node.right==null){BinaryNode<AnyType> newNode=new BinaryNode(element);node.right=newNode;size++;return true;}else{return insert(element,node.right);}}}
二叉查找树的查找算法:
1.若b是空树,则搜索失败,否则:
2.若x等于b的根节点的数据值,则查找成功,否则:
3.若x小于b的根节点的数据值,则搜索左子树,否则:
4.搜索右子树。
相关代码如下:
//Search in the whole treepublic boolean contains(AnyType element){return contains(element, root);}//Search it recursively!private boolean contains(AnyType element, BinaryNode<AnyType> node){if(node==null) return false;if(element.compareTo(node.data)<0) return contains(element, node.left);else if(element.compareTo(node.data)>0) return contains(element, node.right);else return true;}
在这里附上查找最大值和最小值的方法,分别查找最左端节点和最右端节点即可,不难实现:
//This is the external method to find the minimum value.//The data value will be returned! Not the node.public AnyType findMin(){return findMin(root).data;}//This the internal method to find the minimum NODE recursively.//The node with the minimum value will be returned! Not the value!private BinaryNode<AnyType> findMin(BinaryNode<AnyType> node){if(node==null) return null;else if(node.left!=null) return findMin(node.left);else return node;}public AnyType findMax(){return findMax(root).data;}//This is the internal method to find the maximum NODE.//But it's done with a while loop instead of recursion.//The node with the maximum value will be returned! Not the value!private BinaryNode<AnyType> findMax(BinaryNode<AnyType> node){if(node!=null)while(node.right!=null)node=node.right;return node;}
删除节点的算法,相对来说最复杂:
在二叉查找树中删去一个节点,分三种情况讨论:
1.若P节点为叶子节点,即PL和PR均为空树,则直接删除该节点即可(修改它的父节点即可)。
2.若P节点只有左子树PL或右子树PR,此时只要修改P的父节点的相关数据域,将其直接连接到PL或PR即可,该修改不会破坏二叉排序树的特性。
3.若P节点的左子树和右子树均不为空,此时的做法是,找到P节点的右子树中的最小值,将该值取代P,然后对该右子树中最小值所在的节点进行删除操作。
相关代码如下:
public AnyType findMin(){return findMin(root).data;}//This the internal method to find the minimum NODE recursively.//The node with the minimum value will be returned! Not the value!private BinaryNode<AnyType> findMin(BinaryNode<AnyType> node){if(node==null) return null;else if(node.left!=null) return findMin(node.left);else return node;}public AnyType findMax(){return findMax(root).data;}//This is the internal method to find the maximum NODE.//But it's done with a while loop instead of recursion.//The node with the maximum value will be returned! Not the value!private BinaryNode<AnyType> findMax(BinaryNode<AnyType> node){if(node!=null)while(node.right!=null)node=node.right;return node;}
遍历算法:
相当简单,直接给出代码,调整三行语句的顺序可以改变为前序遍历与后序遍历,实现层次遍历需要队列。主语 !=null 条件:
public void printTree(){if(isEmpty()){System.out.println("The tree is empty!");}else{printTree(root);}}//This is the internal method which will implements inorder traversal.//Change the order of statements can change it into a preorder/postorder traversal.//To have a level order traversal will use a queue.private void printTree(BinaryNode<AnyType> node){if(node!=null){printTree(node.left);System.out.println(node.data);printTree(node.right);}}
- 简单数据结构实现——二叉查找树
- 简单数据结构实现——二叉查找树
- [数据结构]二叉查找树 简单实现
- 浅谈数据结构——二叉查找树实现原理分析
- Java实现数据结构——二叉查找树
- 数据结构——二叉查找树的详细实现(c++)
- 二叉查找树简单实现
- 数据结构——二叉查找树
- 基本数据结构——二叉查找树
- 数据结构专题——二叉查找树
- 数据结构(Java)——二叉查找树
- 数据结构——二叉(查找)树
- 数据结构——二叉查找树
- 数据结构之查找(五)——二叉查找树
- C 实现数据结构二叉查找树
- 数据结构二叉查找树C++实现
- 数据结构--二叉查找树的java实现
- Java实现数据结构之二叉查找树
- 【Apache04】Apache虚拟目录/虚拟主机
- Review_2012_11_19_11_20_11_21_11_22_11_23
- 【预览图】【2013/7/6已发布】AstrOrz Player Metro 0.1预览版!Metro界面音乐播放器!
- ZOJ 1203 Swordfish(Kruskal 最小树)
- JAVA学习笔记总结(四):数组(Array)
- 简单数据结构实现——二叉查找树
- 将TaintDroid编译进Android 2.3
- 黑马程序员-ado.net笔记
- Java NIO API详解
- android开发省电之--Determining and Monitoring the Connectivity Status(根据网络连接状况去省电)
- Oracle中的文件的执行命令
- 35 岁前程序员要规划好的四件事
- 35 岁前程序员要规划好的四件事
- Android在标准linux基础上对休眠唤醒的实现