My path to the soft test (IV.)--Data structures and algorithms (2) tree and two fork tree

Source: Internet
Author: User

Oberbaun describes the linear structure of the data structure, we introduce the nonlinear structure of this blog-tree and two-fork tree. I would like to introduce some basic concept tree, tree traversal, and then introduce the concept and characteristics of the two-fork tree. And a two-fork tree traversal. Cross-tree control, summary.

Tree in order to describe the hierarchical structure of the real world, a data element in a tree structure can have two or more direct successor elements of two or more.

Basic concepts of the tree:  

The concept of a tree is the key to learning a tree. Master the basic concept of the tree, learn the tree with two fork tree, so easy.

I use a tree to understand the basic concept of a tree. For example with


1. The degree of node

The degree of the node is the number of child nodes. For example: Node 1 has three characters node 2,3,4, so node 1 is 3.

2, the degree of the tree

The degree of the tree is equal to the highest value of all node degrees. For example, the highest node degree is 3, so the tree has a degree of 3.

3. Leaf knot

The leaf node is a node with a degree of 0, which has no sub-nodes. For example: Middle 3,5,6,7,9. 10.

4. Branch node

The branch node is in addition to the leaf node. All the other nodes in the tree.

For example: The branch node of the tree above is 1,2,4,8.

5. Internal node

The internal nodes are removed from the root node, except for the root node and the leaf nodes, or on the basis of the branch nodes. For example: The inner node of the tree above is 2,4,8.

6. Parent node, sub-node, sibling node.

The parent node, child nodes, and sibling nodes are relative. For example: Node 1 is node 2. 3. 4 Parent, Node 2. The 3,4 is also the node 1 's child node. The knot 2,3,4 is also the brother Knot point.

7. Level

We've got the table in the picture. The root is the first layer. The child of the root is the second layer, and so on, if a node is on layer I. The child's node is in the i+1 layer.


The traversal of a tree

the traversal of the tree is particularly simple, so let's take the tree above as an example:

1. Pre-sequence traversal

Basic Idea : the pre-order traversal is the first visit to the root node. Visit the leaf node again.

The previous sequence traversal of the tree in the graph is: 1, 2. 5,6. 7,3,4,8. 9,10.

2. Post-sequential traversal

Basic idea : This post-order traversal is to first visit the child nodes, and then visit the root node .

The sequential traversal of the tree in the graph is: 5, 6. 7,2,3,9,10,8,4. 1.

3. Hierarchical traversal

Basic idea : Start from the first layer and traverse each layer to the end.

The hierarchical traversal of the tree in the graph is: 1, 2. 3,4,5. 6,7,8. 9,10.


Some related concepts and characteristics of binary tree

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvaml1cwl5dwxpyw5n/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">

learning the characteristics of a binary tree almost can help us solve all the two-fork tree problem, in learning the binary tree characteristics must be given above the two fork tree to practice the truth, the same time, the impression will be more profound.

General Binary Tree Properties:

    1. On the K-layer of a non-empty binary tree. Maximum of 2k nodes (k>=0)
    2. Up to 2k+1-1 nodes (k>=0) in a two-fork tree with a height of k
    3. For whatever a non-empty two-fork tree, assume that the number of leaf nodes is n0, the number of 2 nodes is N2. Then there are: N0 = n2 + 1

Fully binary Tree nature:

    1. The height of a fully binary tree with n nodes is K [log2n]
    2. For a fully binary tree with n nodes, assuming that all nodes in the two-ary tree are numbered from 0 to n-1 from the top (root node) to the bottom (leaf node) and from left to right, then for arbitrarily labeled K nodes, there are:
    • Assuming k=0, it is the root node. It has no parent node. Assuming k>0, the subscript of its parent node is [(I-1)/2];
    • Assuming 2k+1 <= n-1, the subscript for the left Dial hand node of the nodes labeled K is 2k+1; otherwise, the node labeled K has no left dial hand nodes.
    • Assuming 2k+2 <= n-1, the subscript for the right child node of the node labeled K is 2k+2; otherwise, the node labeled K has no right child node

Full two fork Tree nature:

In a full two-fork tree, the number of leaf nodes is more than the number of nodes 1

Binary Tree Traversal

1. Pre-sequence traversal (as with the tree's pre-order traversal)

Basic idea : first visit the root node, then the first sequence to traverse the left subtree. Finally, the first sequence traverses the right subtree -the root-left-right .

The result of the preceding sequence traversal in the figure is: 1,2,4. 5,7. 8,3,6.

2, Middle sequence traversal

Basic idea : The middle sequence traverses the left subtree before visiting the root node. Finally, the middle sequence traverses the right subtree, which is left-root-right .

the results of the middle sequence traversal in the figure are: 4,2,7,8. 5. 1,3,6.

3. Post-sequential traversal

basic idea : sequential traversal of the left subtree, and then the next step to traverse the right subtree, and finally visit the root node is left-right-root .

The results of post-post traversal in the figure are: 4, 8. 7,5,2. 6,3,1.

4. Hierarchical traversal (as with the tree's hierarchical traversal)

Basic idea : Start from the first layer and traverse each layer to the end.

The result of the hierarchy traversal in the figure is: 1, 2. 3,4. 5,6. 7. 8.


The tree differs from the two fork tree


1, the tree can have more sub-nodes. A binary tree can only have two nodes at most.

2, the sub-nodes in the tree are unordered, the binary tree is divided into left dial hand nodes and right child nodes.

3, binary tree is not a special tree. It is a separate data structure.


Summarize

This blog post is the basic content of the tree, which can help you to understand the other aspects of the tree more deeply. Only if you can work hard, the world is full of love.

Maybe the blog update list, please look forward to.

My path to the soft test (i)--Opening ( updated )

My road of Soft Examination (ii)--J2SE macro Summary ( updated )

My path to the soft test (iii)--Data structure and algorithm (1) Linear table ( updated )

My path to the soft test (IV.)--Data structure and algorithm (2) tree with two fork tree ( updated )

My path to the soft test (v)--data structure and algorithm (3) Diagram ( updated )

My path to the soft test (vi)--Data structure and algorithm (4) eight sorts ( updated )

My path to the soft test (vii)--Data structure and algorithm (5) Find ( updated )


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

My path to the soft test (IV.)--Data structures and algorithms (2) tree and two fork 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.