Implementation example of Python two-fork tree _python

Source: Internet
Author: User

Definition of tree

Tree is an important non-linear data structure, intuitively, it is the data elements (called nodes in the tree) by the branching relationship of the structure, very much like the tree in nature. Tree structure is widely existed in the objective world, such as the Genealogy of human society and various social organizations can be represented by tree image. Trees are also widely used in the computer field, such as when compiling the source program, the tree can be used to represent the grammatical structure of the source program. As in the database system, the tree structure is also one of the important organizational forms of information. All questions that have a hierarchical relationship can be described by trees.
The tree structure is characterized by: each node can have more than one direct successor, all nodes outside the root node have and only a direct precursor.
The recursive definition of a tree is as follows: (1) at least one node (called the root) (2) The other is disjoint subtree

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.

two fork Tree features:
(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.

Binary Tree basic data structure

Copy Code code as follows:

#!/usr/bin/python
#-*-Coding:utf-8-*-

Class TreeNode (object):
def __init__ (self,data,left,right):
Self.data = Data
Self.left = Left
Self.right = Right


Class Btree (object):
def __init__ (self,root=0):
Self.root = root

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.