"Data Structure" Binary Tree

1. Concept 

  A binary tree is a finite set of n (n>=0) elements. The set is either empty or consists of a root and two disjoint left subtrees and right subtrees. The left subtree and the right subtree are also Both are binary trees.

   Any node of a binary tree has two subtrees (any one of them can be an empty subtree), and there is an order relationship between the two trees, and they become a different binary tree after swapping positions. Any left subtree and right subtree on the binary tree are respectively called the left child and right child of the node.

Five forms of binary trees.

 

Two, type classification 

According to the type, it is divided into complete binary tree and full binary tree.

The appearance of a complete binary tree is that it is arranged in order, and each layer is arranged in order from left to right. The wrong order cannot be called a complete binary tree.

A full binary tree is that each branch of the level has two children. Unless it is a leaf node. (Leaf nodes are nodes without branches)

A full binary tree must be a complete binary tree. 

Third, the nature of the binary tree

Property 1: There are at most 2i-1 (i≥1) nodes on the i-th level of the binary tree [6]  

Property 2: The binary tree with depth h contains at most 2h-1 nodes [6].

Property 3: If in any binary tree, there are n0 leaf nodes and n2 nodes with degree 2, then there must be n0=n2+1 [6].

Property 4: The depth of a complete binary tree with n nodes is log2x+1 (where x represents the largest integer not greater than n) [6].

Property 5: If a complete binary tree with n nodes is numbered sequentially (1≤i≤n), then for the node numbered i (i≥1): [6] 

When i=1, the node is the root, and it has no parent node [6].

When i>1, the number of the parent node of the node is i/2 [6].

If 2i≤n, there is a left leaf numbered 2, otherwise there is no left leaf [6].

If 2+1≤n, there is a right leaf numbered 2i+1, otherwise there is no right leaf [6].

Fourth, the traversal of the binary tree

The traversal of binary tree includes pre-order traversal, middle-order traversal and post-order traversal.

Pre-order traversal is to visit the root node first, then the left subtree, and finally the right subtree. Root→left→right

In-order traversal is to visit the left subtree first, visit the root node, and finally visit the right subtree. Left→root→right 

Post-order traversal is to visit the left subtree first, then the right subtree, and finally the root node. Left→right→root

There is also level traversal, which is from top to bottom, from left to right, traversal,

 

Guess you like

Origin blog.csdn.net/weixin_43472073/article/details/108874660