Binary Search Tree

<?xml version="1.0" encoding="utf-8"?> 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)

转载于:https://www.cnblogs.com/mlhy/p/4863057.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值