您的位置:首页 > 其它

Binary Search Tree

2015-10-09 10:06 260 查看

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

Binary Search Tree



Binary Search Tree

1 Definition

A binary search tree is a binary tree, where each node has a restriction that the key in any node is larger than the key in its left-child subtree and smaller than any key in its right-child subtree.

2 Search

Obviously, if the search key is samller than the key in node, search the key in the node's left subtree;otherwise, search the key in the node's right subtree.

3 Insert

It's similar with the search immplemention. At first, search the insert key, if retrun null, then we replace it with the insert key.

4 Delete

To delete a node x by replacing it with its successor.

1.If node x is null, delete

2.If node x only has one child, then x = x.left/x.right

3.If node x has two child; t =x, x = min(t.right), and x.left = t.left, x.right = deleteMin(t.right)

deleteMin(Node x)
if x.left == null
return x.right;
x.left = deleteMin(x.left)


5 Analysis

The running times of algorithms on binary search trees depend on the shapes of the trees, which, in turn,depend on the order in which keys are inserted. In the best case, a tree with N nodes could be perfectly balanced, with ~ lgN nodes between the root and each null link. In the worst case there could be N nodes on the search path. The balance in typical trees turns out to be much closer to the best case than the worst case.

Author: mlhy

Created: 2015-10-09 五 10:06

Emacs 24.5.1 (Org mode 8.2.10)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: