Introduction to python Data Structure Tree and binary tree

Source: Internet
Author: User

1. Tree Definition

Tree structure is an important non-linear structure. A tree structure is a structure with branches and hierarchical relationships between nodes. It is very similar to a tree in nature.
Recursive definition of the tree:
A Tree is a finite set T of n (n ≥ 0) nodes. If T is null, it is called an empty Tree. Otherwise, it meets the following two conditions:
(1) There is only one specific Root node;
(2) The remaining nodes can be divided into m (m ≥ 0) subset Tl, T2 ,..., Tm, where each subset is itself a tree and is called as the root subtree (Subree ).

Ii. Binary Tree Definition

A binary tree is a finite set of n (n ≥ 0) nodes. Each node has a maximum of two Subtrees. It is either an empty set, or a root tree and two non-overlapping Binary Trees called left and right subtree.
Features:
(1) A binary tree is an ordered tree. Even if there is only one subtree, the left and right Subtrees must be differentiated;
(2) The degree of each node of a binary tree cannot be greater than 2, and only one of 0, 1, and 2 can be used;
(3) there are five types of nodes in a binary tree: empty node, no left and right subtree node, only left subtree node, only right subtree node and left and right subtree node.

Iii. Nature of Binary Trees

Property 1: A binary tree has a maximum of nodes on layer I.
Property 2: A binary tree with a depth of h has a maximum of-1 nodes.
Property 3: the height of a binary tree with n nodes is not smaller than the maximum integer.
Property 4: In any binary tree, if the number of leaf nodes is n0 and the number of degree 2 is n2, n0 = n2 + 1 is inevitable.
Full Binary Tree: If a binary tree with a depth of h has exactly-1 node, it is called a full binary tree.
Full Binary Tree: If the logical structure of a binary tree with n nodes is exactly the same as that of the first n nodes of the full binary tree, the binary tree is called a full binary tree.
Extended Binary Tree: Except for leaf nodes, all other nodes must have two children's Binary Trees.

Iv. Binary Tree Storage Structure

Binary Tree storage structures include sequential and chained storage structures.
Sequential storage:The structure is stored in a one-dimensional array. Based on the nature of the Binary Tree 6, the subscript of the parent node and the left and right child node can be calculated. Therefore, the storage of full Binary Trees and full binary trees can use a one-dimensional array to store nodes in the array in the order from top to bottom and from left to right, the relationship between nodes can be calculated by the formula of Property 6.
Chain storage:The structure uses a linked list to store data elements in a binary tree, and uses a chain to establish the relationship between nodes in a binary tree. The most common chain storage structure of a binary tree is binary chain. Each node contains three fields: data element domain data, lChild of the left child chain domain, and rChild of the right child Chain Domain. Similar to the two cases of single-chain strap head knots and no-lead nodes, the binary tree of the binary chain storage structure also has the lead node and no-lead node.

V. Binary Tree operations

Example of building a binary tree in python Data Structure

Python Data Structure-binary tree traversal instance

Python Data Structure-Example of Binary Tree statistics and conversion

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.