Tree and binary tree

Source: Internet
Author: User

Tree and binary tree
A tree is an important non-linear data structure defined by the hierarchy defined by the branch relationship: the tree is a finite set T of n (n> = 0) nodes, where: when n = 0 is null tree n> 0, there is only one specific node, called the root of the tree. When n> 1, other nodes can be divided into m (m> 0) finite sets that do not conflict with each other T1, T2 ,...... Tm, in which each set itself is a tree, called the root subtree. features: there must be at least one node in a non-empty tree. Each subtree in the root tree is a set of different elements. The basic concept is node, which indicates the elements in the tree, including data items and the degree (degree) of several branch nodes pointing to its Subtrees-number of sub-trees owned by the node (leaf)-node child with a degree of 0) -- the root of the node tree is the Child parent (parents). The upper node of the child node is called the ~ of the node ~ Brother (sibling) -- The degree of the Child tree of the same parent -- the level of the maximum node level node in a tree -- From the root node, the root layer is the first layer, its child is the second layer ...... Depth: the maximum number of layers of nodes in the tree (forest) -- m (m> = 0) the set of Binary Tree definitions of trees that do not meet each other: A binary tree is a finite set of n (n> = 0) nodes. It is also an empty tree (n = 0 ), A tree consists of one root node and two trees, called the left and right trees respectively. 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: 1. at most 2i-1 nodes (I> = 1) exist in layer I of a binary tree. A binary tree with a depth of k has at most 2k-1 nodes (k> = 1) 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, n0 = n2 + 1 proves: n1 is the number of knots where T is 1 in a binary tree. Because the degree of all knots in the binary tree is less than or equal to 2, the total number of knots n = n0 + n1 + n2 is in another binary tree, except for the root node, all the other nodes only have one branch to enter and Set B to the total number of branches. Then n = B + 1 and the branch is shot from a node with the degree of 1 and the degree of 2, that is, B = n1 + 2n2, n = B + 1 = n1 + 2n2 + 1 = n0 + n1 + n2: n0 = n2 + 1 full Binary Tree definition: a depth of k, the full binary tree with 2k-1 nodes is defined as a k-depth binary tree, A binary tree with n nodes only corresponds to nodes numbered from 1 to n in a full binary tree with a depth of k: the leaf node may only appear on any node on the maximum two layers of the hierarchy. If the maximum level of the child of the right branch is I, then the largest hierarchy of its children under the left branch must be I or I + 1: 4. A Complete Binary Tree with k nodes, with a depth of (log2k) rounded down and added a property: 5. if the nodes of a Complete Binary Tree with n knots are numbered by sequence, then for any node I (1 <= I <= n), there are: (1) if I = 1, the node I is the root of the 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, access the root node, and then traverse the Left and Right sub-trees in a descending order. First, traverse the left sub-tree in a descending order, and then access the root node, finally, traverse right subtree in ascending order and then traverse in descending order: Traverse left and right subtree, then access the root node in hierarchical traversal: access each node from top to bottom and from left to right. Reference code: copy the code void printq (Tree * T) {if (T! = NULL) {printf ("% c", T-> c); printq (T-> l); printq (T-> r );}} copy code copy code void printz (Tree * T) {if (T! = NULL) {printz (T-> l); printf ("% c", T-> c); printz (T-> r );}} copy code copy code void printh (Tree * T) {if (T! = NULL) {printh (T-> l); printh (T-> r); printf ("% c", T-> c );}} copy code copy code 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 );}}

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.