Tree of data structure and two-fork tree (theory article)

Source: Internet
Author: User

First, the tree

Tree definition: The tree is a finite set of n (n>=0) nodes.

For any non-empty tree: (1) There is only one particular node known as the root node, and (2) when n>1, the remaining nodes can be divided into m (m>0) disjoint finite set t1,t2,t3,tm, each of which is itself a tree, and is called the root subtree.

Nodes: The nodes of a tree contain a data element and several branches that point to its subtree.

Degree (degree): The number of subtree owned by a node is called the degree of the node.

Leaf (leaf): A node with a degree of 0 is called a leaf or terminal node; a node with a degree of 0 is called a non-terminal node or branch node, and a branch node is also called an internal node outside the root node.

The degree of the tree: the maximum value of the degree of each node within the tree.

Child: The root of the node's subtree is called the child of the node

Parent: The node is called the parent of the child.

Brother (Sibling): The same parent's children are called Brothers.

Ancestor: The ancestor of a node is all nodes from the root to the branch of the node.

Descendants: Conversely, any node in a subtree that is rooted in a node is called a descendant of that node.

Level: defined from the root: The root is the first layer and the child of the root is the second layer. Parents on the same level of the knot are cousins.

Depth or height of the tree (depth): The maximum level of the nodes in the tree is called the depth or height of the tree.

Ordered tree: If each subtree of a node in the tree is considered to be ordered from left to right (that is, it cannot be interchanged), the tree is called an ordered tree, otherwise it is an unordered tree.

Forest (forest): The forest is a collection of M (m>=0) disjoint trees. For each node in the tree, the collection of its subtrees is the forest.


Two or two fork tree

Binary tree definition: Two fork tree is another type of tree structure, it is characterized by a node at most only two subtrees tree (that is, the binary tree does not exist more than 2 nodes), and the subtree of the binary tree has left and right points, its order can not be arbitrarily reversed.

5 Types of binary tree: defined by recursion, a binary tree is empty, or a root node plus two separate two-fork trees, called Saozi right subtrees. Since these two subtrees trees are also binary trees, they can also be empty trees.

The nature of binary tree

Property 1: There are at most 2^ (i-1) nodes on the first layer of the binary tree (i>=1).

Property 2: Two fork trees with a depth of k at most 2^k-1 nodes

Proof: 2^0+2^1+ +2^ (k-1) =2^k-1

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

Proof: Total number of nodes: N=n0+n1+n2 (a)

Set B as the total number of branches, because in addition to the root node, each node has a branch entry, then: N=b+1 (1), and because these branches from degrees 1 and degrees 2 of the knot burst out, then: B=N1+2N2 (2), by (1) (2), n=n1+2n2+1 (B)

From (a) (b), n0=n2+1

Full two fork tree: a two-fork tree with a depth of K and a 2^k-1 node is called a full two-fork tree.

A fully binary tree: a two-fork tree with a depth of k, with n nodes, when and only if each of its nodes corresponds to a node one by one in a full two-tree with a depth of k from 1 to N, called a complete binary tree.

The characteristics of the complete binary tree: (1) leaf nodes may only appear on the two layers with the largest level, and (2) to any node, if the maximum level of the descendants under the right branch is L, then the maximum level of the descendants under the left branch of the branches must be L or l+1.

Binary Tree Storage structure:

(1) Sequential storage structure: the node elements on the complete binary tree are stored sequentially from top to bottom, from left to right with a contiguous set of storage units, and the nodes that are numbered I on the complete binary tree are stored in the component labeled I-1 in a one-dimensional array. A "0" indicates that the node does not exist. Thus, this sequential storage applies only to complete binary trees.

(2) Chained storage structure: a node in a linked list that represents a binary tree contains at least 3 fields: a data field and a left and right pointer field. Sometimes, to make it easier to find the parent of a node, you can also add a pointer field to the node structure that points to its parent node. The two-tree storage structure using these two node structures is called the two-fork list and the triple-linked list respectively. The head pointer of the linked list points to the root node of the binary tree.

There are n+1 empty links in the two-linked list containing n nodes.

Proof: Nodes with degrees 2 and 1 are public 2n2+n1 pointer fields, n nodes 2n pointer field, and N0=N2+1 (1), N=N0+N1+N2 (2)

By (1) (2) n=2n2+n1+1, so the empty link domain =2n-(2n2+1) =2n-(n-1) =n+1


Three or two traversal of a fork tree

Definition: Two fork tree in the application, it is often required to do some operations on all nodes in the tree, which puts forward the problem of traversing the binary tree, that is, how to visit each node in the tree according to a search path, so that each node is accessed once and only once.

(1) First Order traversal: If the binary tree is empty, then empty operation;

Access root node--first traversal left subtree--First order traversal right subtree

(2) Middle sequence traversal: If the binary tree is empty, then empty operation;

First-order traversal of the left subtree--access to the root node--first sequence traversal right subtree

(3) post- order traversal: If the binary tree is empty, then empty operation;

First-order traversal of the left sub-tree--first-order traversal of the right subtree--access to the root node

First order traversal to get the prefix representation (Polish), the middle order to get infix expression, after the post-suffix to be represented (inverse polish)


Reference: Min "Data Structure" (C language version)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Tree of data structure and two-fork tree (theory article)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.