`
taimukang
  • 浏览: 183918 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Binary Searching Tree

 
阅读更多

a binary search tree (BST ), which may sometimes also be called an ordered or sorted binary tree , is a node-based binary tree data structure which has the following properties:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • Both the left and right subtrees must also be binary search trees.

Generally, the information represented by each node is a record rather than a single data element. However, for sequencing purposes, nodes are compared according to their keys rather than any part of their associated records.

The major advantage of binary search trees over other data structures is that the related sorting algorithms and search algorithms such as in-order traversal can be very efficient.

Binary search trees are a fundamental data structure used to construct more abstract data structures such as sets , multisets , and associative arrays .

 

Implements in Java.

 

Tree Node:

public class Node<E extends Comparable<E>>{
E key = null;
//parent node
Node<E> parent;
//left child node
Node<E> lchild;
//right child node
Node<E> rchild;
Node(E key){
this.key = key;
}
}
 

BSTree:

public class BSTree<T extends Comparable<T>> {
	
	private Node<T> root = null;

	public BSTree(){}
	
	public Node<T> getRoot() {
		return root;
	}
	
	public boolean insert(T value){
		if(value == null){
			return false;
		}
		if(root==null){
			root = new Node<T>(value);
			return true;
		}else{
			return BSTInsert(root,value);
		}
	}
	
	private boolean BSTInsert(Node<T> node,T value){
		int result = node.key.compareTo(value);
		if(result == 0){
			throw new RuntimeException("the value has already existed");
		}else if(result >0){
			if(node.lchild==null){
				node.lchild = new Node<T>(value);
				node.lchild.parent = node;
				return true;
			}else{
				return BSTInsert(node.lchild, value);
			}
		}else{
			if(node.rchild==null){
				node.rchild = new Node<T>(value);
				node.rchild.parent = node;
				return true;
			}else{
				return BSTInsert(node.rchild, value);
			}
		}
	}
	
	public boolean search(T value){
		if(value==null||root==null){
			return false;
		}
		return (BSTSearch(root,value)==null);
	}
	
	private Node<T> BSTSearch(Node<T> node, T value){
		int result = node.key.compareTo(value);
		if(result == 0){
			return node;
		}else if(result >0){
			return BSTSearch(node.lchild,value);
		}else{
			return BSTSearch(node.rchild,value);
		}
	}
	
	public boolean delete(T value){
		if(root==null||value == null){
			return false;
		}
		Node<T> delNode = BSTSearch(root, value);
		if(delNode == null){
			return false;
		}
		Node<T> parent = delNode.parent;
		if(delNode.rchild==null){
			if(parent.lchild.key == value){
				parent.lchild = delNode.lchild;
			}else{
				parent.rchild = delNode.lchild;
			}
			return true;
		}
		if(delNode.lchild==null){
			if(parent.lchild.key == value){
				parent.lchild = delNode.rchild;
			}else{
				parent.rchild = delNode.rchild;
			}
			return true;
		}
		// if either the left child nor the right child is null
		Node<T> p = delNode;
		Node<T> s = delNode.lchild;
		// find the most right node of s, and p is the parent of s
		while(s.rchild!=null){
			p = s;
			s = s.rchild;
		}
		//replace the value of delNode with the value of s
		delNode.key = s.key;
		//rebuild the left child 
		if(p!=delNode){
			p.rchild = s.lchild;
		}else{
			p.lchild = s.lchild;
		}
		s.lchild.parent = p;
		s.parent = null;
		s.lchild = null;
		return true;
	}
}
 

 

 

 

 

 

 



  


  
分享到:
评论

相关推荐

    hw3lij.rar_The Work

    Binary Search Tree: This project practices the searching algorithm: Binary Search Tree. In this work, two java projects were created, one use the unbounded queue to contain the tree traverse sequence,...

    普林斯顿算法课程 Robert Sedgewick

    insertion sort, shell sort, quick sort, merge sort, heap sort), searching algorithms (Binary search tree, Red-Black BST, hashing), 所以程序都用java实现,代码风格简洁,很值得学习。

    Data.Structures.and.Algorithms.USING.C

    Helping readers build efficient C data structures, this ...32. Binary Search Tree 33. AVL Trees 34. Spanning Tree 35. Heaps RECURSION 36. Recursion ─ Basics 37. Tower of Hanoi 38. Fibonacci Series

    BST_Paths.rar_them

    This is a C++ code. Given a set of numbers, the task is to build a binary search tree, and to certain numbers, if they are searching in the tree, and say which way to reach them.

    Learning.JavaScript.Data.Structures.and.Algorithms.1783554878

    After this, you will be taught what trees are, and how to use the binary tree and the binary search tree. In subsequent chapters, you will learn about graphs, DFS, and BFS. Finally, we will round ...

    数据结构英文教学课件:25_searching_01.pdf

    3. 树索引方法:这类方法利用树形数据结构,如二分查找树(Binary Search Tree)或B树等,来提高搜索效率。它们通常适用于有序数据,允许在对数时间内完成搜索。 对于无序数组的搜索,线性搜索是最常见的方法,虽然...

    自己动手写搜索引擎 第4章 光盘实例

    * that combines the compact size of a binary search tree with the speed of a digital search trie, and is * therefore ideal for practical use in sorting and searching data.&lt;/p&gt; * * This data ...

    chapter数据集合上的搜索Searching算法实用PPT学习教案.pptx

    二叉搜索树(Binary Search Tree,BST)是动态数据集的一种高效实现方式,特别适用于搜索操作。它是二叉树的变种,其中每个节点包含一个键、关联值、左子树和右子树。在二叉搜索树中,所有左子树节点的键小于父节点...

    计算机IT英语专业词汇.pdf

    2. 平衡二叉树(Balanced Binary Tree):一种二叉树,每个节点的左右子树高度差不超过1。 3. 满二叉树(Full Binary Tree):一种二叉树,每个节点都有两个子节点。 4. 完全二叉树(Complete Binary Tree):一种...

    数据结构英文教学课件:26_searching_02.pdf

    本教学课件“26_searching_02.pdf”专注于数据结构中的搜索技术,特别是二叉搜索树(Binary Search Trees, BSTs)。本文将深入探讨二叉搜索树的原理、操作以及平衡树的概念。 二叉搜索树是一种特殊类型的二叉树,其...

    Algorithms and Data Structures - Niklaus Wirth

    - **Tree Search and Insertion**: Searching and inserting nodes in a binary tree. - **Tree Deletion**: Removing nodes while maintaining tree structure. - **Analysis of Tree Search and Insertion**: ...

    数据结构与算法(JAVA语言版)-中文

    - **二叉树的性质**:探讨二叉树的一些重要性质,如二叉树的高度、满二叉树(full binary tree)、完全二叉树(complete binary tree)等。 - **二叉树的存储结构**:介绍二叉树的链式存储和数组存储方式。 - **...

    数据结构基本英语词汇

    - **Binary Sort Tree**(二叉排序树):对于任意一个节点,其左子树中的所有节点的值小于该节点的值,右子树中的所有节点的值大于该节点的值。 - **Binary Search Tree**(二叉查找树):与二叉排序树相同,是一种...

    数据结构(栈,队列,二叉树,图,排序查找等)

    二叉树分为二叉查找树(BST,Binary Search Tree)、完全二叉树、满二叉树和平衡二叉树(如AVL树和红黑树)等。二叉树广泛应用于文件系统、数据库索引、表达式解析等领域。二叉树的操作包括查找、插入和删除。 5. ...

    Searching-Algorithms

    6. **二叉查找树搜索**(Binary Search Tree, BST): 二叉查找树是一种特殊的二叉树,每个节点的左子树只包含小于该节点的元素,右子树包含大于该节点的元素。搜索操作在BST中通常非常高效,时间复杂度为O(log n)...

    Swift.Data.Structure.and.Algorithms

    Merge Sort, and Quick Sort and understand the performance trade-offs between them See how to implement various binary trees, B-Tree, and Splay Trees Perform advanced searching methods using Red-Black...

    Coding-DS-Algo:DS和Algo练习以提高编程技能

    - **二叉搜索树(Binary Search Tree, BST)**:BST是一种自平衡的搜索树,每个节点包含一个键、一个关联的值、左子树和右子树,用于高效查找、插入和删除操作。 - **二叉树(Binary Tree)**:二叉树的每个节点...

    algorithm-zh

    - 完全二叉树(Complete Binary Tree) - 平衡二叉树(Balanced Binary Tree) - **遍历方式**: - 前序遍历(Preorder Traversal) - 中序遍历(Inorder Traversal) - 后序遍历(Postorder Traversal) #### Huffman...

    Data Structures and Algorithms with Python

    - 二叉树(Binary Tree) - 特点与性质 - 遍历算法 - 平衡二叉树(AVL Tree) - 自平衡机制 - 插入与删除操作 - B-Tree - 结构与特点 - 应用场景 - 图(Graph) - 基本概念 - 表示方法(邻接矩阵、邻接表) -...

    Schaum's Outline of Data Structures with Java, Second Edition

    - **平衡二叉搜索树(Balanced Binary Search Tree)**:如AVL树、红黑树等,保持了高度平衡,确保操作效率。 - **图(Graph)**:由节点(顶点)和边组成的数据结构,用于表示对象之间的关系。 #### 五、关键概念与...

Global site tag (gtag.js) - Google Analytics