Binary Tree for Data Structure Learning (theoretical)

Source: Internet
Author: User

Note: The main purpose of this article is to record your learning process and facilitate your communication. For reprint, please indicate from:

Http://blog.csdn.net/ab198604

1. What is a tree?

In the previous blog posts, we mainly talked about the data structure of chained storage, which is widely used. However, in practical applications, there is another very important data structure, which is a tree. The structure of the tree is as follows:

It is a data structure-tree. The main reason for the blank space in each box is that such a structure can convey important structure information according to the context. For example, we can think about it as follows:

1. The organizational structure of a company;

2. it can represent various job distribution charts from the upper layer to the bottom layer of a company;

3. It indicates the kinship of a family;

4. It can represent the directory structure in the computer;

........

You can give full play to your imagination, as long as the above structure can be called a tree structure. Through the above thinking, we can find that the tree structure is widely used. It represents a hierarchical relationship between data, and there is a certain relationship between layers, I think this is an important feature of the tree structure, which is very different from other structures.

For the tree, first understand the following basic concepts:

1

/\

2 3

/\/

4 5 6

1. Each tree has a "root", which is the "root" of the tree. It is called root. Through root, we can easily find the pivot points on the tree, "1" is the root of the tree;

2. Each node on a tree may have branches, but there may be no branches. The number of branches is called the branch factor. For example, the maximum branch knot factor is 2, and the branch factor of the "3" node is 1.

3. Each tree has a height. The number of data layers is the height of the tree, and the height of the middle tree is 3.

4. General concepts:

The relationship between 1 and 2 is: 1 is the parent, 2 is the left child, and 3 is the right child. 2 and 3 are called brothers. Nodes without children are called Leaf nodes, for example, 4 \ 5 \ 6 nodes.

Ii. What is a binary tree

For trees, we need to master binary trees. A binary tree is a special sequence tree. It specifies that there are two children, that is, the order of the left and right children cannot be replaced. Therefore, a binary tree is an ordered tree. The number of nodes in a binary tree is greater than 0 and less than or equal to 2. For binary trees, you must master the following attributes:

Property 1 has at most one node on the I layer of a binary tree (I> = 1)

It can be proved by data induction,

I = 1, the number of knots is 1

I = 2, the number of knots is 2

I = 3, the number of nodes is 4

I = 4, the number of nodes is 8

I = N, the number of knots is.

A binary tree with a depth of K has at most-1 node (k> = 1)

In other words, if the depth of a binary tree is determined, the maximum number of knots is also determined.

Proof (usable Nature 1)

The number of nodes in a K-depth Binary Tree = the sum of the number of nodes in each layer of the binary tree. That is:

= 1 + 2 + 4 + 8 +... + =-1 (proportional formula)

In the binary tree of nature 3, the number of terminal nodes has the following relationship with the number of nodes with degree 2:

= + 1

(Note: Degree indicates the number of branches, also refers to the branch factor, and the terminal node also refers to the leaf node)

Analysis: the degree of a node in a binary tree can be 0, 1, 2. That is to say, the relationship between a node with a degree of 0 and a node with a degree of 2 remains unchanged.

Proof: set the number of knots where the binary tree is moderate to I

The total number of nodes in the entire binary tree is: N = ++
------------ (1)

In addition to the root node, each node is the child of another node, so the number of children is n-1 -------- (2)

Nodes with an I (I =, 2) degree have I children,

Number of children = x 0 + X 1 + x
2 = 2 + --------------- (3)

Because :( 3) = (2), so,

N-1 = 2 + ------ (4)

(4)-(1), get,

-1 =-, that is, = +
1 certificate.

Properties 1, 2, and 3 are common features of Binary Trees.

 

Before introducing other properties, we should first understand another special binary tree, that is, full binary tree. Its definition is as follows:

A full Binary Tree is a binary tree with a depth of K and a node of-1.

Features: (1) the number of nodes on each layer reaches the maximum.

(2) The number of nodes whose degree is 1 = 0, that is, there is no node whose branch number is 1

The following is a full Binary Tree: (Note the sequence: the node sequence number method, which serial numbers the nodes of the binary tree from left to right layer from top to bottom from the root node)

1

/\

2 3

/\/\

4 5 6 7

When K = 3, knots-1 = 7

Full Binary Tree: a binary tree with a depth of K and A knots of N. if and only when the numbers of each node are one-to-one with a full Binary Tree of the same depth, it is called a Complete Binary Tree.

According to the definition can be understood: depth is K of the Complete Binary Tree, the total number of nodes than the depth of the K-1 of the full binary tree more, but must be less than the depth of K of the full binary tree. That is, the Complete Binary Tree Diagram is as follows:

1

/\

2 3

/\

4 5 (pay attention to the serial number order, one-to-one correspondence with full Binary Trees)

Property 4: A Complete Binary Tree with knots of N, whose depth is (rounded down) + 1

Defined by nature 2 and Complete Binary Trees:

The number of knots meets the following requirements:

Property 5: In a Complete Binary Tree with N knots numbered by sequence, any node I () has:

(1) When I = 1, node I is the root of the tree. Otherwise (I> 1), the parent of node I is I/2 (rounded down), as shown in

, Take I = 2.

(2) When 2I> N, node I has no left child and is a leaf node. Otherwise, node I's left child is node 2I.

(3) 2I + 1> N, node I has no right child; otherwise, node I's right child is node 2I + 1.

 

Nature 4 and nature 5 are for full Binary Trees. Nature 6 is for the chain storage structure of Binary Trees.

 

Property 6: There are n + 1 empty chain fields in a binary linked list containing N nodes.

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.