[Data Structure] Basic Concept of Binary Tree

Insert image description here

1. Tree

1. What is a tree?

Tree is a non-linear data structure, which is a set of hierarchical relationships composed of n (n>=0) limited nodes.
Insert image description here

  • subtrees are disjoint
  • Except for the root node, each node has one and only one parent node.
  • A tree with N nodes has N-1 edges.

2. Concepts related to trees

Degree of node: The number of subtrees contained in a node is called the degree of the node; As shown in the figure above: the degree of A is 3

Leaf node or terminal node: Nodes with degree 0 are called leaf nodes; As shown in the figure above: Nodes such as J, F, K, L, H, and I are leaf nodes.

Non-terminal node or branch node: A node whose degree is not 0; As shown in the figure above: Nodes such as B, C, D, E, G are branch nodes.

Parent node or parent node: If a node contains child nodes, then this node is called the parent node of its child node; As shown in the figure above: A is the parent node of B

child node or child node: The root node of the subtree contained by a node is called the child node of the node; As shown in the figure above: B is the child node of A

Sibling node: Nodes with the same parent node are called sibling nodes; as shown above: B and C are sibling nodes.

degree of tree: In a tree, the degree of the largest node is called the degree of the tree; as shown above: the degree of the tree is 3

Node level: Starting from the definition of the root, the root is the 1st level, the root's child nodes are the 2nd level, and so on;

height or depth of tree: The maximum level of nodes in the tree; as shown above: the height of the tree is 4

cousin node: Nodes whose parents are on the same level are cousins ​​of each other; as shown in the figure above: F and G are brother nodes of each other.

Ancestor of node: All nodes on the branches from the root to the node; as shown in the figure above: A is the ancestor of all nodes

descendants: Any node in the subtree rooted at a certain node is called a descendant of that node. As shown above: all nodes are descendants of A

forest: A set of m (m>0) disjoint trees is called a forest.

3. Representation of tree

The tree structure is more complex than a linear table. When storing, it is necessary to save both the value range and the relationship between nodes. There are many ways to represent trees, such as: parent representation, child representation, child parent representation, and Child brother representation, etc. The most commonly used one is the child brother notation.

//孩子兄弟表示法
typedef int DataType;
struct Node
{
    
    
    struct Node* firstChild; // 第一个孩子结点
	struct Node* nextBrother; // 指向其下一个兄弟结点
	DataType data; // 结点中的数据域
};

Insert image description here

Insert image description here

2. Binary tree

1. The concept of binary tree

A binary tree is a finite set of nodes, which is either empty or consists of a root node plus two binary trees called left subtree and right subtree.

Insert image description here

  • There is no node with degree greater than 2 in a binary tree
  • The subtrees of a binary tree can be divided into left and right subtrees, and the order cannot be reversed, so the binary tree is an ordered tree.

2. Several situations of binary trees

Any binary tree is composed of the following situations:

Empty tree:
Insert image description here
only the root node:
Insert image description here
only the left subtree:
Insert image description here
only the right subtree:
Insert image description here
both left and right subtrees:
Insert image description here

3. Special binary tree

Full binary tree : A binary tree. If the number of nodes in each layer reaches the maximum, then the binary tree is a full binary tree. That is to say, if the number of levels of a binary tree is K and the total number of nodes is, then it is a full binary tree.

Insert image description here

Complete binary tree : A complete binary tree is a very efficient data structure. A complete binary tree is derived from a full binary tree. For a binary tree with depth K and n nodes, it is called a complete binary tree if and only if each node corresponds one-to-one with the nodes numbered from 1 to n in the full binary tree with depth K.

Insert image description here
A full binary tree is a special kind of complete binary tree

4. Properties of binary trees

  1. If the number of levels of the root node is specified to be 1, then there are at most 2 (i-1) nodes on the i-th level of a non-empty binary tree .
  2. If the number of levels of the root node is specified to be 1, then the maximum number of nodes of a binary tree with depth h is 2 h -1.
  3. For any binary tree, if the number of leaf nodes is n 0 with degree 0 and the number of branch nodes with degree 2 is n 2 , then n 0 =n 2 +1
  4. If the number of levels of the root node is specified to be 1, the depth of a full binary tree with n nodes, h= log 2 (n + 1) log _ 2 ^ (n+1)log2(n+1).
  5. For a complete binary tree with n nodes, if all nodes are numbered starting from 0 in array order from top to bottom, left to right, then for the node with serial number i:
  • If i>0, the parent number of the node at position i: (i-1)/2; i=0, i is the root node number, and there is no parent node
  • If 2i+1<n, left child number: 2i+1, 2i+1>=n otherwise there is no left child
  • If 2i+2<n, the right child number: 2i+2, 2i+2>=n otherwise there is no right child

5. Storage structure of binary tree

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

Sequential storage:

Sequential structure storage is to use arrays for storage. Generally, arrays are only suitable for representing complete binary trees, because if they are not complete binary trees, there will be a waste of space. In reality, only the heap uses arrays for storage. The sequential storage of binary trees is physically an array and logically a binary tree.

Insert image description here
Insert image description here
Chain storage:

The linked storage structure of a binary tree means that a linked list is used to represent a binary tree, that is, a chain is used to indicate the logical relationship of elements.
The usual method is that each node in the linked list consists of three fields, the data field and the left and right pointer fields. 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 respectively.
The chain structure is divided into two-pronged chain and three-pronged chain.

Insert image description here
Insert image description here

typedef int BTDataType;
// 二叉链
struct BinaryTreeNode
{
    
    
	struct BinTreeNode* pLeft; // 指向当前节点左孩子
	struct BinTreeNode* pRight; // 指向当前节点右孩子
	BTDataType data; // 当前节点值域
}
// 三叉链
struct BinaryTreeNode
{
    
    
	struct BinTreeNode* pParent; // 指向当前节点的双亲
	struct BinTreeNode* pLeft; // 指向当前节点左孩子
	struct BinTreeNode* pRight; // 指向当前节点右孩子
	BTDataType data; // 当前节点值域
}

Guess you like

Origin blog.csdn.net/zcxyywd/article/details/132774776