Trees and Binary Trees --- Data Structures

Table of contents

1. The concept and structure of the tree

1.1 The concept of a tree

1.2 Tree Representation

1.3 Application of trees in real life

2. The concept and structure of binary tree 

2.1 Concept

2.2 Special Binary Trees

2.3 Properties of Binary Trees

2.4 Storage structure of binary tree


1. The concept and structure of the tree

1.1 The concept of a tree

A 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 .

  • 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) disjoint sets T1, T2, ... Tm, each set Ti (1 <= i <= m) is A subtree whose structure is similar to a tree . The root node of each subtree has one and only one predecessor, and can have 0 or more successors
  • Therefore, the tree is defined recursively.

Tree structure: Note: In the tree structure, there can be no intersection between subtrees, otherwise it is not a tree structure

 

Related concepts of trees:

 

The degree of the 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

Leaf nodes or terminal nodes: nodes with a degree of 0 are called leaf nodes; as shown in the figure above: nodes such as B, C, H, I... are leaf nodes

Non-terminal nodes or branch nodes: nodes whose degree is not 0; as shown in the figure above: nodes such as D, E, F, G... are branch nodes

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

Child node or 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 the child node of A

Brother nodes : nodes with the same parent node are called brother nodes; as shown in the figure above: B and C are sibling nodes

Degree of the 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 6

The level of the node: starting from the definition of the root, the root is the first level, the child node of the root is the second level, and so on;

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 cousins; as shown in the above figure: H and l are sibling nodes

Ancestors of a node: all nodes on the branch from the root to the node; as shown 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;

1.2 Tree Representation

The tree structure is more complicated than the linear table, and it is more troublesome to store and express. It is necessary to save the value range and the relationship between nodes and nodes . In practice, there are many ways to express trees, such as: parent representation method , child notation , child parent notation and child brother notation , etc. Let's take a brief look here, the most commonly used is the child brother notation .

1. Parent representation

struct TreeNode
{
    int data;
    int parent;    //双亲的下标
}

2. Child Representation

If the degree of the tree is specified, it can be defined:

struct TreeNode
{
    int data;
    struct TreeNode* child1;
    struct TreeNode* child2;
    // . . .
    //树的度是几就定于几个孩子
}

If you don't know the degree of the tree, you can use a linear table to store children, usually using a combined structure of "order table + linked list"

typedef struct CTNode{
    //链表中每个结点存储的不是数据本身,
    //而是数据在数组中存储的位置下标!!
    int child;
    struct CTNode * next;
}ChildPtr;

typedef struct TreeNode
    //结点的数据类型
    TElemType data;
    //孩子链表的头指针
    ChildPtr* firstchild;
}TreeNode;

3. The representation of the child's parents is equivalent to the circular linked list in the linked list, so I won't introduce it here

4. Child brother notation

typedef int DataType
struct Node
{
    struct Node* fristChild;    //指向该节点的第一个孩子节点
    struct Node* NextBrother;   //指向该节点的下一个兄弟节点
    DataType data;    //该节点中的数据
}

 

1.3 Application of trees in real life

The file system of the Windows operating system, the file tree, and the two file trees of the C drive and the D drive can be considered as a forest.

Linux operating system tree directory structure

2. The concept and structure of binary tree 

2.1 Concept

— A binary tree is a finite set of nodes that:

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

 As can be seen from the figure above:

  1. A binary tree does not have a node with degree greater than 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

Note: For any binary tree, it is composed of the following situations:

A real binary tree:

2.2 Special Binary Trees

  1. Full binary tree : a binary tree, if the number of nodes in each layer reaches the maximum value, then this binary tree is a full binary tree. That is to say, if a binary tree has K layers and the total number of nodes is 2^K-1, then it is a full binary tree.
  2. 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 corresponds to the nodes with the same number from 1 to n in a 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. The value range of the number of complete binary tree nodes is [2^(K-1), 2^K-1].

 

 

2.3 Properties of Binary Trees

1. If the root node is specified as 1, then there are at most 2^(i-1) nodes on the i-th layer of a non-empty binary tree

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.

3. For any binary tree, if the number of leaf nodes with degree 0 is N0, and the number of branch nodes with degree 2 is N2, then N0 = N2+1

The total number of nodes in the binary tree is N, there is N=N0+N1+N2, and the sum of the degrees of the binary tree is 0*N0+1*N1+2*N2 = N-1

N0+N1+N2-1 = 0*N0+1*N1+2*N2

N0 = N2+1;

4. If the number of layers of the root node is specified as 1, the depth of a full binary tree with n nodes is h=log2(n+1). (ps: log2(n + 1) is log base 2, and n+1 is logarithm)
5. For a complete binary tree with n nodes, if all nodes are arranged in the order of the array from top to bottom and from left to right Numbering starts from 0, then for the node with the serial number i:

  1. If i>0, the parent number of the node at i position: (i-1)/2;; i=0, i is the root node number, no parent node
  2. If 2i+1<n, left child number: 2i+1, 2i+1 >= n has no left child
  3. If 2i+2<n, the right child number: 2i+2, 2i+2 >= n has no right child

*6. Given N nodes, h(N) different binary trees can be formed. h(N) is the Nth term of the Cattelan number. h(n)=C(n,2*n)/(n+1).

*7. There are i branch points, I is the sum of road lengths of all branch points, J is the sum of road lengths of leaves J=I+2i

It is too difficult to bring *, generally not used.

example:

1. A binary tree has a total of 399 nodes, of which 199 are nodes with a degree of 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. Among the following data structures, which one is not suitable for sequential storage structure ()

A Incomplete binary tree
B Heap
C Queue

D stack

3. 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

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

A 11
B 10

C 8

D12

5. A complete binary tree with 767 nodes, the number of leaf nodes is ()
A 383
B 384

C 385

D 386


Answers: 1.B 2.A 3.A 4.B 5.B

Analysis of Question 3:

Because N0 = N2 + 1

N0 + N1 + N2 = 2n

N0+N1+N0-1 = 2n

2N0 +N1-1 = 2n 2n is an even number, and the number of nodes with a complete binary tree degree of 1 is either 1 or none. So it can only be 1

2N0 = 2n N0 = n Similarly, N1= 1 N2 = n-1

2.4 Storage structure of binary tree

Binary trees can generally be stored using two structures, a sequential structure and a chain structure.
1. Sequential storage
Sequential structure storage uses arrays for storage. Generally, arrays are only suitable for representing complete binary trees , because not complete binary trees will waste space. In reality, only the heap will use arrays for storage, and we will specifically explain the heap in the following articles. Binary tree sequential storage is physically an array and logically a binary tree.

2. Chained storage
The chained 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 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 further divided into binary chains and triple chains . At present, we generally use binary chains in our studies. Later courses will learn high-level data structures such as red-black trees, which will use triple chains.

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;     //当前节点值域
};

 The specific implementation we will introduce in the following article

end of article

Guess you like

Origin blog.csdn.net/qq_72916130/article/details/131982392
Recommended