A binary search tree is a binary tree. It may be empty. If it is not
empty, it satisfies the following properties: (1) Every node has a
key, and no two nodes have the same key. (2) The keys in a nonempty
left subtree must be smaller than the key in the root of the subtree.
(3) The keys in a nonempty right subtree must be larger than the key
in the root of the subtree. (4) The left and right subtrees are also
binary search trees. Sample binary search trees are shown in Figure 1.
Figure 1. binary search trees To search for a node with a key k in a
binary search tree T , we begin at the root. If T is empty, T contains
no keys and the search is unsuccessful. Otherwise, we compare k with
the key in root. If k equals root’s key, then the search terminates
successfully. If k is less than root’s key, we search the left subtree
of the root. If k is larger than root’s key, we search the right
subtree of the root. In the same way, we can proceed the search in the
left or right subree of T . To insert a new key k into a binary search
tree T where k is different from those of existing keys in T , we
first search the tree T . The search will be unsuccessful, then we
insert the key at the point the search terminated. For instance, to
insert a key 80 into the Figure 1(a), we first search the tree for 80.
This search terminates unsuccessfully, and the last node examined has
key 40. We insert a new node containing 8
LA 4847 binary search tree
最新推荐文章于 2022-02-18 09:14:48 发布