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