Introduction to Binary Trees

Source: Internet
Author: User

In computer science, a binary tree is an ordered tree with a maximum of two Subtrees at each node. Generally, the root of a subtree is called left subtree and right subtree ). Binary Trees are often used as binary search trees and binary heaps. Each node of a binary tree has at most two Subtrees (nodes with no degree of presence greater than 2). the Subtrees of a binary tree have left and right points, and the order cannot be reversed. The second layer of the binary tree has at most 2 (I-1) to the power of the node; the depth of K of the binary tree has at most 2 ^ (k)-1 node; for any binary tree T, if the number of terminal nodes (that is, the number of leaf nodes) isN0. The number of knots whose degree is 2 isN2, thenN0 =N2 + 1.

Two major differences between trees and binary trees:

1. There is no limit on the maximum number of nodes in the tree, while the maximum number of nodes in the binary tree is 2;

2. Tree nodes have no left or right, while Tree nodes have left or right .......

A tree is an important non-linear data structure. It is a structure in which data elements (called nodes in a tree) are organized by branches, just like trees in nature. Tree structures are widely used in the objective world, such as the genealogy of human society and various social organizations. Tree is also widely used in the computer field. For example, when the source program is compiled as follows, a tree can be used to indicate the following syntax structure of the source program. Another example is that tree structure is also an important form of information organization in database systems. All hierarchical issues can be described in a tree.

Tree Overview

The tree structure features that each of its nodes can have more than one direct successor. All nodes except the root node have only one direct forward trend. The following describes the definition of the tree and the data structure of the tree.

Tree Definition

A tree is a finite set of one or more nodes:

A worker must have a specific node called the root node;

The remaining worker nodes are divided into N> = 0 sets of T1, T2,... TN, and each of these sets is a tree. Tree T1, T2,... TN are called the root subtree ).

The recursive definition of a tree is as follows: (1) there must be at least one node (called the root) (2) other child trees that are not mutually exclusive

1. The degree of the tree, that is, the width, that is, the number of branches of the node. The maximum degree in each node of the tree is used as the degree of the tree. For example, the degree of the tree is 3. the node with a moderate degree of zero in the tree is called a leaf node or a terminal node. A node with a moderate degree of non-zero in a tree is called a branch node or a non-terminal node. All branch nodes except the root node are called internal nodes.

2. Tree depth: the maximum layer of each node in the tree. For example, the depth is 4;

3. forest refers to the set of several trees that do not meet each other. For example, Remove root node A. The set of the original two subtree T1, T2, and T3 {T1, T2, t3} is the forest;

4. Ordered Tree: it refers to a tree with Layer Nodes arranged in order from left to right. The order between them cannot be exchanged. Such a tree is called an Ordered Tree, otherwise it is called an unordered tree.

Tree Representation

There are many methods to represent the tree. The common method is to enclose the root node in a pair of parentheses, and then place the child tree from left to right into parentheses, the child tree uses the same method. The child tree and its root node are enclosed in parentheses, separated by commas (,), and enclosed in closed brackets. The format is as follows:

(A (B (E (K, L), F), C (G), D (H (M), I, j )))

Binary Tree 1. Basic Forms of Binary Trees

Binary Trees are also defined recursively. Their nodes have left and right Subtrees. Logically, binary trees have five basic forms:

(1) null binary tree -- ();

(2) binary tree with only one root node -- (B );

(3) only the right subtree -- (C );

(4) only the left subtree -- (d );

(5) Complete Binary Tree -- (E)

Note: Although Binary Trees and trees have many similarities, binary trees are not a special case of trees.

2. Two important concepts

(1) Complete Binary Tree-if the height of the binary tree is H, except for the H layer, other layers (1 ~ H-1) are all reached the maximum number of points, the H layer of all nodes are continuously concentrated on the leftmost, This Is The Complete Binary Tree.

(2) Full Binary Tree-except for leaf nodes, each node has left and right leaves and leaf nodes are at the bottom of the binary tree ,.

3. The nature of Binary Trees

(1) In a binary tree, the total number of nodes in the I layer cannot exceed 2 ^ (I-1 );

(2) A binary tree with a depth of H can have a maximum of 2 ^ (H)-1 nodes (h> = 1) and at least H nodes;

(3) For any binary tree, if the number of leaf nodes is N0, and the number of nodes with a degree of 2 is N2,

Then N0 = n2 + 1;

(4) the depth of the Complete Binary Tree with N nodes is int (log2n) + 1

(5) If the nodes of the Complete Binary Tree with N nodes are stored in sequence, the nodes have the following relationships:

If I is a node number, if I <> 1, the ID of its parent node is I/2;

If 2 * I <= N, the number of the Left son (that is, the root node of the Left subtree) is 2 * I. If 2 * I> N, NO LEFT son exists;

If 2 * I + 1 <= N, the node number of the right son is 2 * I + 1. If 2 * I + 1> N, no right son exists.

(6) given n nodes, different Binary Trees of H (n) can be formed.

H (n) is the nth entry of the catlan number. H (n) = C (n, 2 * N)/(n + 1 ).

4. Binary Tree Storage Structure

(1) sequential Storage

Type node = record

Data: datatype

L, R: integer;

End;

VaR TR: array [1 .. n] of node;

(2) linked list storage methods, such:

Type btree = ^ node;

Node = record

Data: datatye;

Lchild, rchild: btree;

End;

5. convert a common tree into a binary tree

All the brothers are connected by a line, and then the father and son are removed from the line, leaving only the line between the parent and the first child.

A binary tree is like a inverted tree, from the root to the big branches, small branches, until the leaves link the data. This data structure is called a tree structure, or tree for short. Each split point in a tree is called a node, and the Start Node is called a root node. The connection between any two nodes is called a branch. No branches under a node are called leaves. The front node of a node is called the "parent" of the node, and the back node of the node is called the "child" or "child" of the node ", the "children" of the same node are mutually called "brothers ".

Full Binary Tree

For a full Binary Tree, starting from the first layer of the node (that is, the root), from the bottom up, from the left and right, according to the order of node numbers, you will get an order of full Binary Trees. According to the serial number, the full binary tree is defined as follows: a binary tree with N nodes and a depth of K is defined, if and only when all nodes correspond to the knots numbered from 1 to n in the full binary tree with a depth of K, the binary tree is a Complete Binary Tree. Figure 4 is a Complete Binary Tree.

Binary tree traversal

Traversal is the most basic operation of a tree. The so-called traversal of a binary tree is to travel all nodes of the binary tree in a certain rule and order, so that each node is accessed once, and only once. Because the binary tree structure is non-linear, tree traversal essentially converts each node of the binary tree into a linear sequence for representation.

If l, D, and r represent traversing the left subtree, the access root node, and the right subtree, there are three situations for traversing a binary tree: DLR (called first root order traversal ), LDR (called root order traversal) and lrd (called root order traversal ).

(1) sequential Traversal

Access root; traverse the left subtree In the first order; traverse the right subtree In the first order

(2) sequential Traversal

Traverse the left subtree in the middle order; access the root; traverse the right subtree in the middle order

(3) Post-order traversal

Traverse the left subtree in descending order; traverse the right subtree in descending order; access the root

(4) Layered Traversal

That is, hierarchical access is usually performed using queues. Access the root, access the children, and then access the children of the children (the lower the hierarchy) (the level of the two children is the same)

Special Binary Tree

1. Complete Binary Tree

Complete Binary Tree

If the height of a binary tree is set to H, except for layer H, the other layers (1 ~ H-1) are all reached the maximum number of points, the H layer from the right to the left consecutive lack of a number of nodes, This Is The Complete Binary Tree.

2. Full Binary Tree

Full Binary Tree:

A binary tree of height H that contains exactly 2-1 elements is called a full binary tree.

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.