Python data structure tree and two fork tree introduction _python

Source: Internet
Author: User

The definition of a tree

Tree-shaped structure is a kind of important nonlinear structure. A tree structure is a structure with a branch and a hierarchical relationship between nodes. It is very similar to the tree in nature.
Recursive definition of tree:
A tree is a finite set of n (n≥0) nodes t,t is called an empty tree, otherwise it satisfies the following two conditions:
(1) Having and having only a specific node called root (Roots);
(2) The remaining nodes can be divided into m (m≥0) disjoint subsets of Tl,t2,...,tm, each of which is a tree and is called the root of subtree (subree).

Definition of two or two fork tree

The binary tree is a finite set composed of N (n≥0) nodes, and an ordered tree with a maximum of two subtrees per node. It is either an empty set, or consists of a root and two disjoint two-forked trees called the left and right subtrees.
Characteristics:
(1) binary tree is an ordered tree, even if there is only one subtree, you must distinguish the left and right subtree;
(2) The degree of each node of the binary tree can not be greater than 2, only 0, 1, 23 one;
(3) There are 5 kinds of nodes in the binary tree: The empty node, the node without the left and right subtree, only the node of the Zuozi, the node of the left subtree and the node with the tree.

The properties of the three or two-forked tree

Nature 1: The first layer of the two fork tree has at most one node.
Property 2: The two-fork tree with a depth of H has a maximum of 1 nodes.
Property 3: The largest integer with n nodes that is not less than the height of the two-fork tree.
Property 4: Any tree in a binary tree, if the number of nodes in the leaf is n0, the number of nodes with a degree of 2 is N2, then there must be n0=n2+1.
Full two fork tree: If the depth is H of two fork tree, exactly have-1 nodes, then called full two fork tree.
Complete binary tree: If the logical structure of a two-fork tree with n nodes is identical to the logical structure of the first n nodes of the two-fork tree, the two-fork tree is called a complete binary tree.
Expansion of the binary tree: In addition to leaf nodes, the remaining nodes must have two children of two fork trees.

Four or two-fork tree storage structure

The storage structure of binary tree has sequential storage structure, chain type storage structure
Sequential storage: structs are stored in one-dimensional arrays. According to the nature of the binary tree 6, we can calculate the subscript of parent node and left child node. Therefore, the storage of the two-fork tree and the complete binary tree can be stored in one-dimensional array, the nodes are stored in the array in order from top to bottom, left to right, and the relationship between nodes can be calculated by the formula of Nature 6.
Chained storage: The structure uses the chain table to store the data elements in the binary tree, and establishes the relationship between the nodes in the two-fork tree by the chain. Binary tree The most commonly used chain storage structure is a two-fork chain, each node contains three domains, respectively, data element fields, the left child chain domain Lchild and the right child chain domain rchild. Similar to the two cases of single linked list leading node and not leading node, the two-fork tree of binary chain storage structure also has the lead node and not the lead node two kinds.

Operation of five or two fork tree

An example of constructing binary tree of Python data structure

Traversal example of binary tree of Python data structure

Example of statistics and transformation of binary tree of Python data structure

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.