[C/C++ Data Structure] Basic and Conceptual Explanation of Tree and Binary Tree

Tree Structure and Concepts

tree structure

Definition of tree: Tree is a non-linear data structure, which is a set of hierarchical relationships composed of n (n>=0) finite nodes. It is called a tree because it looks like an upside-down tree, which means it has the roots pointing up and the leaves pointing down.

The structure of the tree has the following definition :

  • There is a special node, called the root node , the root node has no predecessor nodes

  • Except the root node, other nodes are divided into M (M>0) mutually disjoint sets T 1 , T 2 , . . . . . , T m T_1, T_2,..., T_mT1T2......Tm, where each set T i T_iTi(1<= i <= m) is another subtree with a structure similar to a tree. The root node of each subtree has one and only one predecessor, and can have zero or more successors.

  • Trees are defined recursively.

  • In a tree structure, there can be no intersection between subtrees, otherwise it is not a tree structure.

  • Any tree will be divided into roots and subtrees, and each root node of the tree is its subtree

    For example, in the right figure below, A is the root node, and its subtree is BCD; similarly, B is the root node, and its subtree is EF. And so on...

image1

Basic Concepts of Trees

image2

  • Degree of a node: The number of subtrees contained in a node is called the degree of the node; as shown in the figure above: A is 6, B is 0, D is 1, and F is 3 ;
  • Leaf node (terminal node): A node with a degree of 0 is called a leaf node ; as shown in the figure above: nodes such as B, C, H, I, etc. have no subtrees below them and the degree is 0.
  • Branch node (non-terminal node): a node whose degree is not 0 is a branch node ; as shown in the above figure: D, E, F, G... and other nodes all have subtrees and degree !=0, which is a branch node;
  • Parent node (parent node): If a node contains child nodes , this node is called the parent node of its child nodes ; as shown in the above figure: A contains B, and A is the parent node of B;
  • Child node (child node): the root node of the subtree contained in a node is called the child node of the node; as shown above: B is included in A, and B is the child node of A;
  • Brother nodes: Nodes with the same parent node are called brother nodes ; as shown in the above figure: B, C, D, F, and G have a common parent node A, which is a brother node, and the same is true for P and Q;
  • The degree of the tree: In a tree , the degree of the largest node is called the degree of the tree; as shown in the above figure: among all the nodes, the degree of A node is 6, so the degree of the tree is 6;
  • The level of nodes: starting from the definition of the root , the root is the first level, the child nodes of the root are the second level, and the A node has 4 levels;
  • Tree height or depth: the maximum level of nodes in the tree ; as shown above: the height of the tree is 4;
  • Cousin nodes: nodes whose parents are on the same layer are each other’s cousin nodes; as shown in the above figure: H, I, N, and M are each other’s cousin nodes;
  • **Ancestors of nodes: **From the root to all nodes on the branches of this node; as shown in the figure above: A is the ancestor of all nodes;
  • Descendants: Any node in the subtree rooted at a node is called a descendant of the node. As shown above: all nodes are descendants of A;
  • Forest: A collection of m (m>0) disjoint trees is called a forest.

Among the above concepts, the key points to understand are: leaf nodes, parent nodes, child nodes, tree height and depth, and descendants of nodes

tree structure implementation

After reading the above, have you ever thought about how to implement the above structure through code? Next, let's think about how to implement the tree structure more efficiently.

There are many ways to realize the tree, and the data representation is very simple, but the number of children under a node is not clearly specified in the tree, so the top priority of the tree should be how to store the children of each node.

**Method 1:** Assume that the degree of the tree is N;

struct TreeNode
{
    
    
    int data; 					// 用来存储数据的成员
    struct TreeNode* subs[N];   // 用来存储孩子结点(N个度)的指针数组 
} 
//这种方式的缺点显而易见,很有可能会出现不少的数组空间未使用导致内存空间的浪费

**Method 2:** The degree of the tree is not stated.

struct TreeNode
{
    
    
    int data; 		// 用来存储数据的成员
    SeqList s; 		// 在顺序表里存结点的指针
} 
typedef struct TreeNode SLDataType; // 这里是一个顺序表SeqList
// 这种方式的缺点就是,结构过于复杂。因为SeqList这里已经是一个二级指针

**Method 3:** Structural array storage (parent representation)

// 这种方法的主要操作就是,通过下面的结构体TreeNode存储有效信息,最后将其存储到parentArr数组中。
struct TreeNode // 数组存储信息
{
    
    
    int data; 		// 结点数据
    int parent; 	// 父亲结点的下标位置
} 
struct TreeNode parentArr[10]; // 存储结点的数组
// 在这个TreeNode结构体中,data表示当前结点的数据,而parent为这个结点的父节点的下标位置。根节点标记为-1;写入数据后存入结构体数组;
// 具体存储结果可以参考如下举例

image3

data A B C D E H I J
parent -1 0 0 0 0 3 4 4
subscript 0 1 2 3 4 5 6 7
  • data: node data

  • parent: the subscript position of the parent node

  • Subscript: array subscript

The above three methods have their own advantages and disadvantages, but the most common and practical way is ------ left child right brother
method 4
: left child right brother method. The commonly used tree structure is also the left child right brother structure.

image4

It will first start from the root node , record the first child of the child node through child from left to right , then use brother to find the brother node C, and then use child to record the child node to continue the recursion. In this way, no matter how many child nodes the parent node has, it can be recorded. For example, if you want to find the child nodes of node A, first go to the first child through child, and then find other child nodes just like traversing the linked list.

At the same time, let's take a look at the defects of this structure. You will find that there is no waste of space in this structure, and it is a direct link relationship when traversing. So far, this structure has no disadvantages!

Reference Code:

typedef int DataType;
struct Node
{
    
    
    DataType data;				// 节点数据
    struct Node* firstChild;	// 第一个孩子的节点(永远指向第一个孩子,叶子则为NULL)
    struct Node* pNextBrother;	// 指向其下一个兄弟节点(永远指向其的兄弟节点,否则为NULL)
};
data A B C D E F G H I
firstChild B D G NULL H NULL NULL NULL NULL
pNextBrother NULL C NULL E F NULL NULL I NULL

Binary tree concept and structure

The concept of a binary tree:

A binary tree is a finite set of nodes that:

  1. may be empty;

  2. It consists of a root node plus two binary trees, also known as the left subtree and the right subtree

picture5

As can be seen from the figure above:

  1. There is no node with degree greater than 2 in the binary tree (the maximum degree is 2)

  2. The subtrees of a binary tree are divided into left and right, and the order cannot be reversed , so a binary tree is an ordered tree.

Any binary tree can have the following situations

image6

special binary tree

full binary tree

A binary tree is a full binary tree if the number of nodes in each layer reaches the maximum value .

image7

Properties of a full binary tree

  • All leaf nodes are in the last layer;

  • All branch nodes have two children;

  • If the number of layers of a full binary tree is k, then there are $2^{k-1}$ nodes in the kth layer of the tree;

  • If the number of layers of a full binary tree is h, the total number of nodes in the tree is 2 h − 1 2^{h-1}2h1

  • If a full binary tree has N nodes, the height of the tree is $log_2 (N+1) $.

complete binary tree

A complete binary tree is a very efficient data structure, and a complete binary tree is derived from a full binary tree. For a binary tree with a depth of K and n nodes, it is called a complete binary tree if and only if each node has a one-to-one correspondence with the nodes numbered from 1 to n in the full binary tree with a depth of K. It should be noted that a full binary tree is a special kind of complete binary tree .

image8

Properties of a complete binary tree:

  1. The first N-1 floors are full;

  2. The last layer is not full, but the last layer is continuous from left to right. (For example, in the last layer of the above figure, if the node has only right children but no left children, it is not a complete binary tree)

  3. For a complete binary tree of height h, the range of nodes is [ 2 h − 1 2^{h-1}2h1, 2 h − 1 2^h - 1 2h1]。

Properties of Binary Trees

The properties of a binary tree can have the following points:

  1. If the number of layers of the root node is specified as 1, then there are at most 2 i − 1 2^{i-1} on the i-th layer of a non-empty binary tree2i 1 nodes.

  2. If the number of layers of the root node is specified as 1, then the maximum number of nodes in a binary tree with a depth of h is 2 h − 1 2^h - 12h1 .

(The number of each layer is a geometric sequence)

  1. For any binary tree , if the degree is 0, the number of leaf nodes is n 0 n_0n0, the number of branch nodes with degree 2 is n 2 n_2n2, then there are n 0 n_0n0 n 2 n_2 n2+1 (a degree of 0 is always one more than a degree of 2)

  2. If the layer number of the root node is specified as 1 , it has ** nnThe depth hh of a full binary tree of n nodesh,** h = l o g 2 ( n + 1 ) h = log_2(n+1) h=log2(n+1)

  3. For a complete , if all nodes are numbered from 0 according to the order of the array from top to bottom and from left to right , then for the node with the sequence number i:

    1. If i>0, the parent number of the i position node: (i-1)/2 ; i=0, i is the root node number, no parent node;

    2. If 2i+1<n, left child serial number: 2i+1, 2i+1>=n otherwise there is no left child ;

    3. If 2i+2<n, the serial number of the right child: 2i+2, 2i+2>=n, otherwise there is no right child .

  4. A full binary tree must be a complete binary tree, but a complete binary tree is not necessarily a full binary tree.

Binary tree storage structure

Binary trees can generally be stored using two structures, a sequential structure and a chain structure.

sequential structure

Sequential structure storage uses arrays for storage . **Binary tree sequential storage is physically an array and logically a binary tree. **However, in reality, only the heap will be stored in an array, and the heap will be specifically explained in the following chapters.

Binary tree sequential storage is generally only suitable for representing a complete binary tree , because not a complete binary tree will waste space.

image9 image10

As can be seen from the above figure, through sequential storage, it will be found that they are stored very regularly, and each node can be well represented. In the way of sequential storage, the subscript position of its parent node or left and right children can be calculated through the node subscript.

Assuming that parent is the subscript of the parent node in the array, then:

leftchild = parent * 2 + 1 rightchild = parent * 2 + 2

Assuming that child is a child node, regardless of left and right, there are:

parent = (child - 1) / 2

// This will play a very important role in the implementation of the heap later.

chain storage

The linked storage structure of the binary tree means that a linked list is used to represent a binary tree, that is, a link is used to indicate the logical relationship of elements. The usual method is that each node in the linked list is composed of three fields, the data field and the left and right pointer fields, and the left and right pointers are used to give the storage addresses of the link points where the left child and right child of the node are located.

The chain structure is divided into two-fork chain and three-fork chain

Binary chain: There are two pointers pointing to the left and right children

Trident chain: There are not only two pointers to store the left and right children, but also a pointer to store the location of the parent node.

Generally, binary chains are used, and triple chains are used in high-level data structures such as red-black trees.

image11

thinking questions

  1. There are 399 nodes in a binary tree, among which there are 199 nodes with degree 2, then the number of leaf nodes in the binary tree is ( )

    A. There is no such binary tree

    B、200

    C、198

    D、199

  2. In a complete binary tree with 2n nodes, the number of leaf nodes is ( )

    A、n

    B、n+1

    C、n-1

    D、n/2

  3. The number of nodes in a complete binary tree is 531, then the height of this tree is ( )

    A、11

    B、10

    C、8

    D、12

  4. A complete binary tree with 767 nodes, the number of leaf nodes is ()

    A、383

    B、384

    C、385

    D、386

Reference answer:

  1. Answer: B,

    Analysis: leaf node: a node with a degree of 0 is called a leaf node; and the nature of a binary tree, there is always one more node with a degree of 0 than one with a degree of 2. There are 199 nodes with a degree of 2 in the question, then the other There are 199+1=200 leaf nodes (degree 0);

  1. Answer: A,

    Analysis: This question uses the property derivation method;

Let the number of nodes with degree 0 of the complete binary tree be x 0 x_0x0, the number of nodes with degree 1 is x 1 x_1x1, the number of nodes with degree 0 is x 2 x_2x2, and according to the title it has 2n nodes

∴ The equation of the total number of available nodes is: x 0 x_0x0 + x 1 x_1 x1 + x 2 x_2 x2= 2n ;

∵ In any binary tree, the one with degree 0 is always one more than the one with degree 2; that is: x 2 x_2x2 = x 0 x_0 x0 - 1;

∴ get the equation 2 x 0 x_0x0 + x 1 − 1 x_1 - 1 x11 = 2n;

Ask the number of leaf nodes in the question, that is to ask x 0 x_0x0How many is the number, but now x 1 x_1x1(degree 1) can not be obtained, now let's review the nature of the complete binary tree.

image8

In a complete binary tree, the first N-1 levels are full and the last level is not, but the last level is continuous from left to right . Now we can conclude that the range of the number of nodes with a degree of 1 in a complete binary tree is x 1 x_1x1∈ [0, 1], there is at most one node with degree 1.

Back to this question again, the equation 2 x 0 x_0x0 + x 1 x_1 x1-1 = 2n ;, and the value range of x1 is [0, 1], then x 1 x_1x1

Can be 0 or 1. That x 1 x_1x1What would be good?

When x 1 x_1x1= 0, the node equation is 2 x 0 x_0x0- 1 = 2n, x 0 x_0 x0= (2n+1) ÷ 2;

When x 1 x_1x1= 1, the node equation is 2 x 0 x_0x0 = 2n, x 0 x_0 x0 = n;

x 1 x_1 x1= 1, x 0 x_0x0Non-integer number, discarded;

x 1 x_1 x1= 1 is in line with the meaning of the question, so the answer is A n;

This question not only needs to combine the properties of a complete binary tree, but also needs to be combined with a formula to solve the problem. It is a very good question. Try to calculate the fourth question of the same type!

  1. Answer: B,

    Analysis: In the nature of the complete binary tree, we mentioned that a complete binary tree with a height of h has a range of nodes in [ 2 h − 1 2^{h-1}2h1, 2 h − 1 2^h - 1 2h1 ]. So what should its reasoning process look like?

    In a complete binary tree, the biggest situation is that the number of nodes in the last layer of the complete binary tree is full, which is a full binary tree, so the maximum value can be taken to 2 h − 1 2^h - 12h1 , so what is the minimum value? Just imagine, the situation with the least last layer should be that there is only one node in the last layer, all of which are the number of nodes in the previous layer plus 1, so when the maximum number of layers is h, the number of nodes in the h-1 layer is2 h − 1 − 1 2^{h-1}-12h11 , plus the nodes of the last layer is2 h − 1 − 1 + 1 2^{h-1}-1+12h11+1 , so there is a complete binary tree with height h whose node number range is [2 h − 1 2^{h-1}2h1, 2 h − 1 2^h - 1 2h1]。

In summary, when h = 11, the range of the number of nodes of the complete binary tree is [1024, 2047] When h = 10, the range of the number of nodes of the complete binary tree is [512, 1023] When h = 8, the The range of the number of nodes of the complete binary tree is [128, 255] When h = 12, the range of the number of nodes of the complete binary tree is [2048, 4095]

Therefore, the answer is B 10

  1. Answer: A 383,

    Analysis: Same as Question 2.

words written on the back

The basics of the binary tree are finished! When learning binary trees, you usually don't directly learn the addition, deletion, checking and modification of binary trees, because binary trees are usually not directly used to store data, but various transformations are performed by binary trees to meet various needs, so in learning We pay more attention to learning the structure and concept of binary tree in the case of binary tree . Later, such as AVL and red-black tree, we will learn to add, delete, check and modify when we can really store data structures efficiently. When we start to enter the complete structure operation of binary tree, we need to learn a content ------ Heap , this is a data structure that needs to be understood before starting to learn binary trees.

epilogue

It’s not easy to create, if you think this article is useful to you, don’t forget to like and watch + follow !

In the future, we will send the latest articles to the WeChat public account: "01 Programming Cabin" as soon as possible. Follow the cottage and learn programming without getting lost. Don't forget to follow our public account so as not to miss it!

insert image description here

Guess you like

Origin blog.csdn.net/weixin_43654363/article/details/124218210