Data structure and algorithm (4) tree

Source: Internet
Author: User

Basic concepts of the tree:

1. The degree of node

The degree of the node is the number of child nodes. For example: Node 1 has three characters 2,3,4, so the 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: Medium 3,5,6,7,9,10.

4. Branch node

Branch nodes are all other nodes in the tree except the leaf nodes. 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 the parent node of the junction 2,3,4, the node 2,3,4 is also the node 1 of the child nodes, node 2,3,4 is also a sibling knot point.

7. Level

We have shown the table, the root of the first layer, the root of the child is the second layer, and so on, if a node in the layer I, then its children node in the i+1 layer.

The traversal of a tree  

1. Pre-sequence traversal

Basic idea: The pre-order traversal is to access the root node first, and then access the leaf node.

The pre-order 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 access the child nodes, and then access 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 with the first layer and iterate through each layer until the end.

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

Two-fork Tree

General Binary Tree Properties:

Up to 2k nodes (k>=0) on the K-tier of a non-empty binary tree

Up to 2k+1-1 nodes (k>=0) in a two-fork tree with a height of k

For any non-empty two-fork tree, if the number of leaf nodes is n0, the number of nodes with degrees 2 is N2, then there are: N0 = n2 + 1

Full Binary Tree properties:

The height of a fully binary tree with n nodes k is [log2n]

For a full binary tree with n nodes, if all nodes in the two-ary tree are numbered from 0 to n-1 in order from the top (root node) to the lower (leaf node) and left to right, then for any node with the subscript K, there are:

Full Binary Tree properties:

    • If k=0, it is the root node, it has no parent node, and if k>0, the subscript of its parent node is [(I-1)/2];
    • If 2k+1 <= n-1, the subscript of the node labeled K is 2k+1; otherwise, the node labeled K has no left dial hand nodes.
    • If 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 access the root node, then the first sequence to traverse the left subtree, and then the first sequence to traverse the right sub-tree namely 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: First the middle sequence traverses the left subtree, then accesses the root node, and then the middle sequence traverses the right sub-tree namely 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 sub-tree, and then the order to traverse the right subtree, and finally access the root node is left-right-root.

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

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

The tree differs from the two fork tree

1, the tree can have more sub-nodes, the two-fork tree can be up to two nodes.

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, but a separate data structure.

Data structure and algorithm (4) 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.