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)