Tree and binary tree

Source: Internet
Author: User

A tree is an important non-linear data structure and a hierarchy defined by the branch relationship.

Definition: tree is the Finite Set t of n (n> = 0) nodes, where:

Empty tree when n = 0

When N> 0, there is only one specific node, called the root of the tree)

When n> 1, the remaining nodes can be divided into M (M> 0) finite sets that do not overlap, T1, T2 ,...... TM,

Each set is itself a tree, called the root subtree)

Features:
A non-empty tree has at least one node-root.

Each subtree in a tree is a set of different trees.

Basic concepts:

Node indicates the elements in the tree, including data items and several branches pointing to its Subtrees.

Degree: Number of Subtrees owned by a node
Leaf (leaf)-node with a degree of 0
Child-the child whose root of the knot tree is called the node
Parents-the upper node of the child node is called the node ~
Siblings-children of the same parent
Tree degree-the maximum node degree in a tree
Level of a node-from the root node, the root is the first layer, and its child is the second layer ......
Depth: the maximum number of nodes in the tree
Forest (forest) -- M (M> = 0) a set of trees that do not match each other


Binary Tree

Definition:A binary tree is a finite set of n (n> = 0) nodes. It is either an empty tree (n = 0), or a root node and two trees are called respectively.

The left and right subtree are mutually exclusive trees.
Features:
Each node has at most two Subtrees (that is, nodes with no degree of presence greater than 2)
The left and right Subtrees of a binary tree cannot be reversed in any order.

Nature: 1.At most layer I of a binary tree2I-1Nodes (I> = 1)

2. Binary Trees with a depth of K have at most2K-1Nodes (k> = 1)

3. For any binary tree T, if the terminal node number is N0 and the node number of degree 2 is N2, N0 = n2 + 1
Proof: N1 is the number of knots where T is 1 in a binary tree.
Because the degree of all nodes in a binary tree is less than or equal to 2
Therefore, the total number of nodes N = N0 + N1 + N2
In another binary tree, except for the root node, only one branch of the other nodes enters
If B is set to the total number of branches, n = B + 1
Also: The Branch is shot from a node with the degree of 1 and the degree of 2, that is, B = N1 + 2n2
Therefore, n = B + 1 = N1 + 2n2 + 1 = N0 + N1 + N2
That is, N0 = n2 + 1

Full Binary Tree

Definition: a depth of K2K-1Binary Tree of nodes

Full Binary Tree

Definition: a binary tree with a depth of K and N nodes.

One-to-one correspondence with nodes numbered from 1 to n in a full binary tree with a depth of K

Features:
The leaf node may only appear on the maximum two layers of the hierarchy.
For any node, if the maximum level of the child of the right branch is I, the maximum level of the child of the left branch must be I or I + 1

Nature:4.A Complete Binary Tree with K nodes, with a depth of (log2k) rounded down and added

Nature:5.If the nodes of a Complete Binary Tree with N knots are numbered by sequence, for any node I (1 <= I <= N), there are:
(1) If I = 1, node I is the root of a binary tree and has no parent. If I> 1, its parent is I/2.
(2) If 2I> N, node I has no left child. If 2I <= N, the left child is 2I.
(3) If 2I + 1> N, node I has no right child. If 2I + 1 <= N, the right child is 2I + 1.


Binary tree traversal
Method
First traverse: first visit the root node, and then traverse the left and right subtree respectively.
Middle-order traversal: first, the middle-order traversal of the Left subtree, then access the root node, and finally the Middle-order traversal of the right subtree
Post-order traversal: Traverse left and right sub-trees sequentially, and then access the root node
Traverse by hierarchy: access nodes from top to bottom and from left to right

Reference code:

void printq(Tree *T) {    if(T!=NULL){        printf("%c",T->c);        printq(T->l);        printq(T->r);    }}
First traverse the output
void printz(Tree *T)  {    if(T!=NULL){        printz(T->l);        printf("%c",T->c);        printz(T->r);    }}
Sequential traversal output
void printh(Tree *T)  {    if(T!=NULL){        printh(T->l);        printh(T->r);        printf("%c",T->c);    }}
Post-order traversal output
void ccbl(Tree *T)   {    q.push(T);    while(!q.empty()){        T=q.front();        q.pop();        printf("%c",T->c);        if(T->l!=NULL)            q.push(T->l);        if(T->r!=NULL)            q.push(T->r);    }}
Hierarchical traversal output

 


 

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.