Binary Search Tree (BST)

原创 2015年11月21日 16:37:18

Based on“Data Structures and Algorithm Analysis Edition 3.2 (C++ Version)” from C. A. Shaffer

Basic Searching Algorithms

The simplest searching algorithm, Perfromance O(n).

A binary search halves the number of items to check with each iteration, so locating an item (or determining its absence) takes logarithmic time, performance O(log n)

In each interpolation search step, it calculates where in the remaining search space the sought item might be, based on the key values at the bounds of the search space and the value of the sought key, usually via a linear interpolation:

next=low+|xa[low]a[high]a[low](highlow+1)|
The key value actually found at this estimated position is then compared to the key value being sought. If it is not equal, then depending on the comparison, the remaining search space is reduced to the part before or after the estimated position. Performance O(log log n).

Block search refers to a search algorithm for ordered lists. It works by first checking all items Lkm, where and m is the block size, until an item is found that is larger than the search key. To find the exact position of the search key in the list a linear search is performed on the sublist L[(k1)m,km].

Binary Search Tree

Property

All elements stored in the left subtree of a node with value K have values < K. All elements stored in the right subtree of a node with value K have values >= K.

Operations

Traversal

To visit BST nodes in sorted order from lowest to highest, just need to do inorder traversal.

Start from the root node, if greater than what you want, go search left subtree; if less than what you want, go search right subtree, until you find the value you want.

Insert

Start from the root node, if greater than what you want, go search left subtree; if less than what you want, go search right subtree, until there is no subtree. Then creat a leaf node with the new value.

Remove

If it’s a leaf node, just delete it; if it’s an internal node with only one child, just let it’s child be it’s parent’s child, then delete the node ; Else, replace the value you want to delete with the smallest value in the right subtree(that is, the leftest value in the right subtree), then remove the leftest node in the right subtree.

Performance

For search, insert and remove operations, the costs are all O(logn).

版权声明:本文为博主原创文章,未经博主允许不得转载。

浅谈BST (Binary Search Tree)

Part I.Introduction         BST是一棵二叉树。         从微观上来讲,BST的每个节点都大于其左节点,且小于其右节点。         从宏观上来将,BST的每...
  • u013703661
  • u013703661
  • 2014年03月04日 16:45
  • 1067

Inorder Successor in Binary Search Tree BST中找中序遍历的后继节点

Inorder Successor in Binary Search Tree
  • hellobinfeng
  • hellobinfeng
  • 2014年02月27日 07:08
  • 4414

【LeetCode】Recover Binary Search Tree 解题报告

【题目】 Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree wit...
  • ljiabin
  • ljiabin
  • 2015年03月21日 11:58
  • 2146

数据结构-BST(Binary Search Tree)

/****************************************** 数据结构: BST(Binary Search Tree),二叉查找树; 性质: 若结点的左子树不空,则左子树...
  • Jarily
  • Jarily
  • 2013年03月15日 19:00
  • 7131

Leetcode: Binary Search Tree Iterator

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro...
  • doc_sgl
  • doc_sgl
  • 2015年01月03日 22:13
  • 3630

Convert Binary Search Tree (BST) to Sorted Doubly-Linked List

首先是中序遍历 其次记住这是要做成double list。 从 TreeNode head, prev; private void traverse(TreeNode node){ if (node ...
  • sjphiChina
  • sjphiChina
  • 2016年07月18日 05:23
  • 377

二分查找树的C++实现 Binary Search Tree(BST)

用C++实现了二叉查找树的 创建 插入 删除 遍历(宽度优先和三种深度优先[前序,中序,后序]) 查找某值 查找最大值和最小值 清空 复制二叉树(深复制) #include #include u...
  • showonstage1
  • showonstage1
  • 2014年03月13日 19:14
  • 1664

【LeetCode】Validate Binary Search Tree 解题报告

今天CSDN博客发生异常,折腾了大半天终于发出了这篇博文。 【题目】 Given a binary tree, determine if it is a valid binary sear...
  • ljiabin
  • ljiabin
  • 2014年12月03日 16:58
  • 9401

LeetCode--Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined...
  • shaya118
  • shaya118
  • 2015年01月14日 00:30
  • 580

[LeetCode] 501. Find Mode in Binary Search Tree 解题报告

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurre...
  • a921122
  • a921122
  • 2017年04月28日 05:41
  • 1213
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Binary Search Tree (BST)
举报原因:
原因补充:

(最多只允许输入30个字)