Graphical Data Structures - Trees

Definition of a tree

A tree is a finite set of n (n>=0) nodes. When n=0, it is called an empty tree.

In any non-empty tree:

(1) There is one and only one specific node called the root (Root).

(2) When n>1, the remaining nodes can be divided into m (m>0) mutually disjoint finite sets T1, T2, ....., Tm, each of which is itself a tree , and is called a subtree of the root (SubTree).

The following figure fits the definition of a tree:

mark

The root node A has two subtrees:

markmark

It should be noted that although the number of subtrees is not limited, they must not interact with each other. The following diagram obviously does not conform to the principle of mutual non-interaction , so it is not a tree.

markmark

node of tree

A node of a tree contains a data element and branches that point to its subtrees . The number of subtrees a node has is called its degree . The degree of a tree is the maximum degree of each node in the tree.

mark

mark

The level of nodes is defined from the root, the root is the first level, the children of the root are the second level, and so on. The depth or height of a tree is the maximum level of nodes in the tree.

mark

tree storage structure

mark

binary tree

Definition of a binary tree

A binary tree is a finite set of n (n>=0) nodes, which is either an empty set (empty binary tree), or consists of a root node and two disjoint trees, called root nodes, respectively. The left subtree of the point and the binary tree of the right subtree (the subtree is also a binary tree).

Features of binary tree

  • Each node has at most two subtrees, so there is no node with degree greater than 2 in the binary tree .
  • The left subtree and right subtree are in order, and the order cannot be arbitrarily reversed.
  • Even if a node in the tree has only one subtree, it is necessary to distinguish whether it is a left subtree or a right subtree.

Five basic forms of binary tree

  1. empty binary tree
  2. only one root node
  3. The root node has only left subtree
  4. The root node has only the right subtree
  5. The root node has both left and right subtrees

Several special binary trees

oblique tree

mark

Left slope: markRight slope:mark

full binary tree

mark

Full binary tree:mark

complete binary tree

mark

Complete binary tree:mark

Properties of Binary Trees

Binary tree properties 1

Property 1: There are at most 2i-1 nodes on the i-th level of the binary tree (i>=1)

Binary tree properties 2

Property 2: A binary tree with depth k has at most 2k-1 nodes (k>=1)

Binary tree properties 3

Property 3: For any binary tree T, if the number of terminal nodes is n0 and the number of nodes with degree 2 is n2, then n0 = n2+1.

A binary tree, except for terminal nodes (leaf nodes), is a node of degree 1 or 2. Assuming that the number of nodes with degree n1 is 1, the total number of nodes in T is n=n0+n1+n2. Let's look at the number of connecting lines in the tree T from another angle. Since the root node only branches out and no branches enter, the number of connecting lines is the total number of nodes minus 1. That is, n-1=n1+2n2, n0+n1+n2-1 = n1+2n2 can be deduced, and n0 = n2+1 can be obtained by continuing the deduction.

Binary tree properties 4

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

From property 2, the number of nodes in a full binary tree is 2k-1, and it can be deduced that the depth of a full binary tree is k=log2(n + 1). For a complete binary tree, its leaf nodes will only appear in the bottom two layers, so the number of nodes must be less than or equal to 2k-1, but must be more than 2k-1-1. Since n is an integer, 2k-1 <= n < 2k, taking the logarithm of both sides of the inequality to get: k-1 <= log2n <k. Because k as depth is also an integer, so k=[log2n]+1.

Binary tree properties 5

Property 5: If the nodes of a complete binary tree with n nodes (its depth is [log2n] + 1) are numbered in level order (from level 1 to level [log2n] + 1, each level from the left to the right), for any node i (1<=i<=n):

  1. If i=1, the node i is the root of the binary tree and has no parents; if i>1, its parents are node [i/2].

  2. If 2i>n, node i has no left child (node ​​i is a leaf node); otherwise, its left child is node i.
  3. If 2i+1>n, node i has no right child; otherwise, its right child is node 2i+1.

It is easy to understand with the following figure:

mark

storage structure of binary tree

Binary tree sequential storage structure

mark

^ represents a non-existing node.

For right-sloping trees, sequential storage structures waste storage space:

mark

The disadvantage of the sequential storage structure of binary trees is obvious: it cannot reflect the logical relationship; for special binary trees (left-slope tree, right-slope tree), storage space is wasted. Therefore, the binary tree sequential storage structure is generally only used for complete binary trees.

binary linked list

Each node of the linked list contains a data field and two pointer fields:

mark

Where data is the data field, and lchild and rchild are both pointer fields, pointing to the left and right children, respectively.

mark

Binary linked list node structure definition of binary tree:

/*二叉树的二叉链表结点结构定义*/
typedef struct BiNode
{
    char data;      /*结点数据*/
    struct BiNode *lchild, *rchild;     /*左右孩子指针*/
}BiNode,*BiTree;

Reference: "Dahua Data Structure"

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324930128&siteId=291194637