Binary Tree, binary tree c ++

Source: Internet
Author: User

Binary Tree, binary tree c ++

Tree structure is an important non-linear data structure, where Binary Trees are commonly used. Each node of a binary tree has at most two Subtrees (namely, the binary tree does not contain any node greater than 2), and the binary tree has left and right Subtrees.

Binary Tree attributes:

1. Binary Trees have five basic forms. (a) Empty Binary Trees (B) Binary Trees with only root nodes (c) Binary Trees with empty right subtree (d) left and right subtree are non-empty binary tree (e) binary tree with left subtree empty

The nature of Binary trees;

Nature 1At most 2 ^ (I-1) nodes (I segment 1) on layer I of a binary tree)

Nature 2A k-depth binary tree can have at most 2 ^ k-1 nodes (k-1)

Nature 3For any binary tree T, if the number of terminal nodes is n0 and the number of nodes with degree 2 is n2, n0 = n2 + 1. (degree: There are several child nodes under the parent node );

Nature 4The depth of the Complete Binary Tree with n nodes is [limit n] + 1

Nature 5If there is a complete binary tree with n nodes (its depth is [limit n] + 1) nodes are numbered by sequence (from layer 1st to layer [㏒ ₂ n] + 1, each layer is left to right ), for any node I (1 ≤ I ≤ n ),

① If I = 1, the node I is the root of the Binary Tree, and has no parent; if I> 1, the parent node is [I/2];

② If 2i> n, node I has no left child (node I is the leaf node); otherwise, the left child is 2i.

③ If 2i + 1> n, no child exists; otherwise, the right child node is 2i + 1.

Binary tree traversal:

1. Hierarchical traversal (from top to bottom, from left to right)

2. PreOrder traversal (PreOrder (T) = T root node + PreOrder (T left subtree) + PreOrder (T right subtree ))

3. In-order traversal (InOrder (T) = InOrder (T's left subtree) + T's root node + InOrder (T's right subtree ))

4. Post-order traversal (PostOrder (T) = PostOrder (left subtree of T) + PostOrder (right subtree of T) + root node of T)

(If any reposted, please specify the source) 

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.