# Data structure (ii) Two-fork Tree

Source: Internet
Author: User

Basic Concepts

Two fork Tree A tree, where no more than two sons can be found at each node.

two forks The sorting tree is either an empty tree or a two-tree with the following properties:

(1) The Joz tree is not empty, then the value of all nodes on the left subtree is less than or equal to the value of its root node;

(2) If the right subtree is not empty, the value of all nodes on the right subtree is greater than or equal to the value of its root node;

(3) The left and right sub-trees are also two-fork sorting trees respectively;

Traversal of a binary tree

The traversal of a binary tree means that all nodes in the binary tree are accessed sequentially from the root node, so that each node is accessed once and accessed only once. There are many ways to traverse a binary tree, such as pre-sequence traversal, sequence traversal, and post-order traversal.

Pre-sequence traversal

The rule of the pre-order traversal is: If the binary tree is empty, then an empty operation returns, otherwise the root node is accessed first, then the left subtree is traversed, and then the right subtree is traversed by the preceding sequence.

Middle Sequence traversal

The rules for the middle order traversal are: If the tree is empty, an empty operation is returned, otherwise the root node is started (note that the root node is not first accessed), the middle sequence traverses the left subtree of the root node, then the root node is accessed, and the last middle sequence traverses the right subtree. As you can see, if it is a two-fork sort tree, the result of the middle sequence traversal is an ordered sequence.

Post-post traversal

The rule of post-order traversal is: If the tree is empty, then an empty operation is returned, then the left subtree is traversed, then the right subtree is traversed, and finally the root node is accessed, while the left and right subtrees are traversed, the left subtree is traversed first, then the right subtree, and the root node is traversed.

Delete a node.

Other operations for binary sort trees, such as inserting, traversing, etc., are easier to understand, while deletions are relatively complex. There are three scenarios for the nodes to be deleted:

1. Leaf nodes;

2. Only the nodes of the Zuozi or right sub-tree;

3. There are nodes in the left and right sub-trees;

For 1 (To delete the node is a leaf node) directly removed, that is, the immediate removal of the parent node reference, for the 2nd case (the node to be deleted only one son), only the child nodes to replace the parent node, and for the node to be deleted there are two sons of the case, the more common processing logic is, Find a node in its subtree to replace it, and this node we become the secondary node of the sequence of orders.

As you can see, the node that we find to replace can be the smallest node (6) of the right subtree of the deleted node, or the largest node of its left subtree (4), which guarantees that the whole structure of the replacement tree will not change. Why is it called the secondary node of the middle order? Let's take a look at the results of the central sequence traversal of this tree 1-2-3-4to6-7-8-9. Can be very clear to see, in fact, to find this node, can be a node 5 precursor or successor.

Code Implementationtwo basic operation of the fork Tree

Execution results

Data structure (ii) Two-fork Tree

Related Keywords:

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.