Data structure--Two fork tree Overview and its array (sequential storage) Expression method

Source: Internet
Author: User

Tree with two fork tree:

What is a tree? Is that there will be a lot of forked data structures on a node. In general, for a tree, we need the structure of a block of data and a few pointers, which is equivalent to a number of linked lists intertwined, in fact, the list can be considered a special kind of tree, and I want to talk about, is a special kind of tree-two fork tree.

For each node of the tree, there are two attributes, called degrees (degree), which means the number of child nodes that the node has. There is also an attribute, called depth (depth), that refers to the distance from the node to the root.

What is a two-fork tree? As the name implies, is the degree of two tree, it looks like this:

, in the list we need the head, and in the tree we need root (root), A is the root of the binary tree, according to the definition of the binary tree, each node points to two nodes, so B,c is called a children (child), we call child nodes, A is their parents (parent), Can be called a parent node. Because there are no more nodes behind DEFG, we call them leaf nodes.

Other concepts related to binary tree:

One, oblique tree: is inclined to long tree, for example, only left Abd or ACG is inclined tree.

Second, full two tree: if all the nodes exist Saozi and subtree, and the depth of all the leaves are the same, then this tree is called full two fork tree.

Three, a complete binary tree: We numbered the two-fork tree, for example, a (1) B (2) C (3) D (4) E (5) F (6) G (7) can be found, this tree has a total of 7 nodes (recorded as n), but if we delete G, then 6 nodes, for the new tree to traverse, And when it is full, the number of points is the same, then it is a complete binary tree. But if you take out F, then it's not a complete binary tree. Full two fork tree must be completely binary tree, complete binary tree is not necessarily full of two fork tree.

The nature of the binary tree:

Why is it that the binary tree is special? Because it has the following properties:

First, there are at most 2i-1 nodes in the first layer of the binary tree.

The two-fork tree with a depth of K has a maximum of 2k-1 nodes. (Note the difference from the 1th one is that the index is i-1, an exponent is k but overall-1)

The depth of a complete binary tree with n nodes is [(log2n)]+1. [] indicates rounding.

For any node with number n, if it has child nodes, its left child node number is 2n and the right node number is 2n+1. (This nature is important, which determines that the two-fork tree can be represented by an array).

Traversal of a binary tree:

There are three kinds of traversal methods in binary tree.

1, the first sequence traversal: The priority to traverse the root, and then priority to traverse the left node. The result of the first order traversal of the two-forked tree in the figure is abdecfg

2, post-order traversal: priority access to the bottom of the left node, the figure of the two-fork tree after the subsequent traversal of the result is DEBFGCA

3, the middle sequence traversal: first access to the bottom of the leaf, and from Zuozi to the parent node to the right subtree of the sequential traversal, the graph in the binary tree after the sequence of the result is DBEFCGA

Binary tree Sequential Storage method:

For the tree in the diagram, its number is this: a (1) B (2) C (3) D (4) E (5) F (6) G (7), using the nature of four, it is easy to establish a sequential binary tree.

We can discard the first position in the array because it is inconvenient. Let's take a look at how to enter the first order:

voidGet_tree (Char*tree,intsub) {    CharT; scanf ("%c",&t); Tree[sub]=T; if(t=='#')        return; Get_tree (Tree,2*sub); Get_tree (Tree,2*sub+1);}

Enter the # number to indicate that the node is empty, otherwise, it will never be finished!

Then the first order output is simple:

void print_tree (char *tree,int  sub) {    if(tree[sub]==' # ' )        return;    printf ("%c", Tree[sub]);    Print_tree (tree,2*sub);    Print_tree (tree,2*sub+1);}

The code is very simple and convenient, and want to a point of depth as long as the point subscript, the use of the nature of three on the line. Since there is no log in the MATH.H library with the bottom 2 function, so need to use a change of the formula to operate, I will not say more.

The overall code is as follows:

//two first order input and output of the fork tree#include <stdio.h>voidGet_tree (Char*,int);voidPrint_tree (Char*,int);intMain () {Chartree[ +]; Get_tree (Tree,1); Print_tree (Tree,1); return 0;}voidGet_tree (Char*tree,intsub) {    CharT; scanf ("%c",&t); Tree[sub]=T; if(t=='#')        return; Get_tree (Tree,2*sub); Get_tree (Tree,2*sub+1);}voidPrint_tree (Char*tree,intsub) {    if(tree[sub]=='#')        return; printf ("%c", Tree[sub]); Print_tree (Tree,2*sub); Print_tree (Tree,2*sub+1);}

Data structure--Two fork tree Overview and its array (sequential storage) Expression method

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.