(3) Binary lookup tree (binary search tree)

Source: Internet
Author: User

I. What is a two-fork search tree?

Binary search tree, or an empty tree, or a two-tree with the following properties:

(1). If its left subtree is not empty, the value of all nodes on the left subtree is less than the value of its root node, (2). If its right subtree is not empty, then all nodes on its right subtree are more than the value of its root node, and (3). Its left and right subtrees are also two-fork search trees.

is a binary search tree

Binary search tree is a two-fork tree with special properties, and its node data structure is defined as follows:

 PackageSearchtree;/*** Created by Xinfengyao on 16-12-28.*/ Public classBinarynode<t>{T data; Binarynode<T>Left ; Binarynode<T>Right ;  PublicBinarynode () {} PublicBinarynode (T data) { This(Data,NULL,NULL); }     PublicBinarynode (T data, binarynode<t> left, binarynode<t>Right ) {         This. data =data;  This. left =Left ;  This. right =Right ; }}

Two. Tree operations

Understand what is a two-fork search tree, then the basic operation of the two-fork lookup tree How to implement it?

1. Find operations

In the binary lookup tree, the process of finding node X is as follows:

(1). If the binary tree is an empty tree, the lookup fails

(2). If x equals the data of the root node, the lookup succeeds, otherwise.

(3) If x is the root node of the data, then recursively find its left subtree, otherwise

(4). Recursively finds its right subtree.

Based on the above steps, you can write the code for the lookup operation of the two-fork lookup tree: (Subsequent additions ...)

2. Insert operation

Binary Find tree b the process of inserting x is as follows:

(1). If B is an empty tree, insert the inserted node directly as the root node

(2). If x equals the value of the root node's data, it is returned directly, otherwise

(3). If x is the value of the root node data, change the position of the node where x is to be inserted to the left subtree of B, otherwise

(4). Change the position of X to insert the node to the right subtree of B.

Based on the above steps, write out the insert operation code for the two-fork lookup tree: (Subsequent additions ...)

3. Delete operation

(3) Binary lookup tree (binary search tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.