11. Basic binary tree theory and 11 basic binary tree theory

Source: Internet
Author: User

11. Basic binary tree theory and 11 basic binary tree theory
I. Basic concepts of Binary Trees1. DefinitionA Binary Tree is a finite set of n (n> = 0) nodes. It is an empty set (or an empty Binary Tree ), it can also be composed of a root node and a binary tree of two left and right trees that are mutually exclusive and called the root node respectively.2. Binary Tree Features(1) Each node has a maximum of two Subtrees, so there is no node with a degree greater than 2 in the binary tree; (2) the Left and Right Subtrees are ordered, the order cannot be reversed at will. (3) Even if a node in the tree has only one subtree, it must be differentiated between the left subtree and the right subtree. In addition, binary trees have five basic forms:. null binary tree; B. only one root node; c. the root node has only the left subtree. d. the root node only has the right subtree. e. the root node has both the left subtree and the right subtree.3. Special Binary Tree(1) oblique tree: All nodes have only the left subtree binary tree called the left oblique tree; all nodes have only the right subtree binary tree called the Right oblique tree. Feature: each layer has only one node, and the number of nodes is the same as that of Binary Trees.(2) Full Binary Tree: In a binary tree, if all the branches have left and right trees, and all the leaves are on the same layer, such a binary tree is called a full binary tree. Full Binary Tree features: a) All leaves can only appear on the lowest layer and on the same layer; B) the degree of non-leaf nodes must be 2; c) in binary trees of the same depth, the maximum number of nodes and the maximum number of leaves for a full binary tree.(3) Complete Binary Tree: Number a binary tree with n nodes by sequence. If the number is I (1 = <I <= n) the node is exactly the same as the node with the same depth of the full binary tree number I in the binary tree, then this binary tree is called a full binary tree. Full Binary Tree features: a) leaf nodes can only appear in the bottom two layers; B) the bottom leaves must appear in the left continuous position; c) the last two layers. If there is a leaf node, must all be in the right continuous position; d) if the node degree is 1, the node only has the left child, that is, there is no right subtree; e) binary tree with the same number of points, the depth of A Complete Binary Tree is the minimum. In short: the full binary tree must be a complete binary tree, but the full binary tree may not be full. When determining whether the tree is a binary tree, we only need to number each node layer by layer according to the structure of the full binary tree. If there is a gap in the number, it means that the tree is not a full binary tree, and it is all there.
Ii. Nature of Binary Trees1. Nature 1At most 2 ^ (I-1) nodes (I> = 1) on the I layer of the binary tree. Analysis: calculate the number of points at a layer of the binary tree. Here "at most" refers to, when the binary tree is full binary tree, the mathematical induction demonstrated that the I layer can have a maximum of 2 ^ (I-1) nodes.2. Nature 2: K depth of the binary tree has at most 2 ^ K-1 nodes (k> = 1) Analysis: calculate the total number of Binary Tree nodes. Here "Depth for k" refers to a k-layer Binary Tree, when the binary tree is full Binary Tree, by mathematical induction demonstrated that the binary tree has at most 2 ^ K-1 nodes.3. Nature 3: For any binary tree T, if the number of terminal nodes is n0 and the number of nodes with the degree of 2 is n2, no = n2 + 1. analysis: a binary tree consists of n0 leaf nodes with 0 degrees, n1 nodes with 1 degree, and n2 nodes with 2 degrees. Therefore, the total number of nodes of the Binary Tree n = n0 + n1 + n2 (equation 1 ). from the number of connections in the binary tree, we can see that because the root node only goes out of the branch and no branches enter, the total number of all branch lines minus 1, that is, the total number of branch lines in the binary tree = n-1 = n1 + 2n2 (equation 2) (n1 indicates the number of knots whose degree is 1 and n2 indicates the number of knots whose degree is 2 ). Simultaneous equations: n0 = n2 + 1.4. Nature 4: The depth of the Complete Binary Tree with n nodes is "log2 ^ n" + 1 ("x" indicates the maximum integer not greater than x ). Analysis: From the nature of 2 we can know that the depth of k full Binary Tree knots n = 2 ^ K-1. The obtained Level of the full binary tree is k = log2 ^ (n + 1 ). Because the full Binary Tree is a subset of the full binary tree, its knots must be less than or equal to the same degree of the full Binary Tree knots 2 ^ K-1, but must be more than 2 ^ (k-1)-1. That is, 2 ^ (k-1)-1 <n <2 ^ K-1. since point n is an integer, n <2 ^ k-1 = <2 ^ k and n> 2 ^ (k-1)-1> = 2 ^ (k-1 ), SO 2 ^ (k-1) <n <2 ^ k, then the logarithm of the two sides of the inequality, get the k-1 = <log2 ^ n <k, and k as the degree is also an integer, therefore, k = "log2 ^ n" + 1.5. Nature 5:If a Complete Binary Tree with n nodes (its depth is "log2 ^ n" + 1) nodes are numbered by sequence number (from layer 1st to the "log2 ^ n" + layer 1, each layer is left to right), for any node I (1 = <I <= n) include:
(1) If I = 1, the node is the root of the Binary Tree of I, and has no parent; if I <1, its parent is the node [I/2]; (2) if 2i> n, node I has no left child (node I is a leaf node); otherwise, node I is node 2i; (3) if 2i + 1> n, node I has no right child; otherwise, the right child is node 2i + 1.Iii. Binary Tree Storage Structure1. Binary Tree sequential Storage StructureThe binary tree is a special tree that enables sequential storage structure. The ordered storage structure of a binary tree stores nodes in a one-dimensional array, and the storage location of the nodes, that is, the subscript of the array, must reflect the logical relationship between nodes, for example, the relationship between two parents and children, and between the left and right siblings. (K nodes, need to allocate 2 ^ K-1 storage unit space) Note: sequential storage structure is generally used only for full binary tree. 2. Binary linked list(1) Core IdeasEach node of a binary tree has a maximum of two children. All data domains and two pointer domains are designed for it to point to the left and right children of the node. We call this linked list a binary linked list.(2) node Structure
Data is the data field, and lchild and rchild are both pointer fields, respectively storing pointers pointing to left and right children.(3) node structure definition code of the binary linked list/* Tree Binary Tree binary linked list node structure definition */typedef struct BiTNode/* node Structure */{TElemtype data; // node data struct BiTNode * lchild, * rchild; // left and right child pointer} BiTNode, * BiTree;(4) Structured instances> Binary linked list> triplicate linked list: the parent pointer field stores the pointer pointing to the parent node.


Related Article

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.