Data Structure Learning Experience Series (iii)

Source: Internet
Author: User

This blog is intended to consolidate the basics, master please bypass. Section code and content reference Min People's post and telecommunications publishing house "data Structure"

Tree and two-fork tree

Tree

A tree is a finite set Tof N (n≥0) nodes, in any non-empty tree:

There is only one specific node, called the root (root) of the tree,

When n>1 , the remaining nodes can be divided into m (m>0) non-intersecting finite set t1,t2,... Tm, where each set itself is a tree, called the root subtree

Features of the tree:

There is at least one node in a non-empty tree-the root

Each subtree in a tree is a collection of disjoint

Basic terms of the tree :

Node-represents an element in a tree, including data items and several branches that point to its subtree

Node degree (degree)--number of subtrees owned by the node

Leaf (leaf)-a node with a degree of 0

Parent (parents)--the upper node of the child's knot is called the parent of the knot.

Brother (sibling)--children of the same parents

The degree of the tree-the largest node in a tree

Node level--from the root node, the root is the first layer, its child is the second layer ...

Depth (depth)--the maximum number of layers in a tree's nodes

Forest (forest)--m (m³0) A collection of disjoint trees

Two-fork Tree

A binary tree is a special kind of tree.

Definition: Two a fork tree is a finite set of N (n³0) nodes, which is either an empty tree (n=0)or a disjoint two-ary tree consisting of a root node and two respectively called the Saozi right subtree.

Characteristics:

Each node has a maximum of 2 subtrees trees ( i.e. nodes with no more than 2 );

The subtree of a binary tree has left and right points, and its order cannot be arbitrarily reversed.

5 Basic types of binary trees:

Empty two-fork tree

A two-prong tree with only the root node.

Right sub-tree is empty

Left dial hand tree is empty

Left and right subtrees are not empty

Binary Tree Properties

Property 1: In a non-empty binary tree, there are at most 2i-1 nodes on layer i (i≧1).

Property 2: Two fork tree with a depth of k at most 2k-1 nodes (k≧1)

Property 3: For any binary tree, if its leaf knot point is n0, the degree of 2 of the node is N2, then n0=n2+1

The full binary tree depth of 4:N nodes is: ë㏒2nû+1.

Where symbol: Ëxû represents the largest integer not greater than X. Éxù represents the smallest integer not less than x.

Property 5: If a node of a complete binary tree with N nodes ( depth of └㏒2n┘+ 1) is on a layer (from layer 1 to layer ë㏒2nû+1) ) sequence is numbered from left to right , for nodes numbered I (1≦i≦n):

⑴ if I=1: Then node i is the root of two fork tree, no parent node; otherwise, if i>1, then its parent node number is ëi/2û.

⑵ if 2i>n: The node i is a leaf node, no left child; otherwise, the left child node number is 2i.

⑶ if 2i+1>n: Then node I has no right child; otherwise, the right child node number is 2i+1.

Full two fork Tree

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

Complete binary Tree

If the depth is K, the two-Tree of 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, the two-fork tree is called a complete binary tree. Or a full two-tree with a depth of K, the first n nodes numbered from 1 to n form a complete binary tree with a depth of K.

Traversing a binary tree

Three ways to Traverse

1.dlr--First ( root ) sequence traversal.

2.ldr-- ( root ) sequence traversal.

3.lrd--after ( root ) sequence traversal.

expression as shown in the two-fork Tree: (a+b* (c-d)-e/f)

Traversing this binary tree in different order, the order in which the visited nodes are arranged in order is:

Its first sequence column:-+a*b-cd/ef

Which sequence is listed as: a+b*c-d-e/f

The subsequent sequence is listed as: abcd-*+ef/-

The traversal of a tree

The traversal of a tree can be transformed into a two-fork tree before it is traversed.

There are three types of tree traversal :

1. First root ( sequence ) traversal : If the tree is not empty, first access the root node, and then the root in turn to traverse the subtrees tree.

2. Post -root ( sequential ) traversal : If the tree is not empty, then the root of the subtrees tree is traversed first, then the root node is accessed.

3. Traverse by hierarchy : If the tree is not empty, the top-down from left to right accesses each node in the tree.

There are two types of forest traversal :

1. Sequential traversal ( First root traversal of each tree in the forest )

If the forest is not empty, then visit the root node of the first tree in the forest ;

The Serring of the first tree in the forest is first sequenced ;

The first sequence traverses the forest ( except the first tree ) of the rest of the trees.

2. Middle sequence traversal ( post-root traversal of each tree in the forest )

If the forest is not empty, then the middle sequence traverses the Serring of the first tree in the forest ;

Access the root node of the first tree in the forest ;

The middle sequence traverses the forest ( except the first tree ) of the rest of the trees.

Huffman Tree

Suppose n weights (W1 ... wn), try to construct a two-fork tree with n leaf nodes, each leaf node right is wi, then the length of the weighted path WPL the smallest two-fork tree is called the optimal binary tree, also known as Huffman Tree.

Generally, in the optimal binary tree, the higher the weight, the closer the leaves are from the root.

The method of constructing Huffman tree--Huffman algorithm

Assuming that there are n weights of w1,w2,..., WN, the Huffman tree is constructed with n leaf nodes. n weights The construction rules of Huffman tree are:

(1) the n nodes are regarded as n single node two fork tree, the weights of the nodes are W1, W2, ..., WN ; n binary tree constitutes a binary tree set of M.

(2) Selecting two root nodes in set M with the lowest weight of two fork tree merging, as the left and right subtree of a new tree, and the root node weights of the new tree are the sum of their left and right subtree node weights;

(3) Delete the selected two binary trees from the set M and add the new tree to the collection

(4) repeat (2),(3) step, until There is only one binary tree in the set M, the two fork tree is the Huffman tree we have obtained.

Huffman Coding method

The Huffman tree is constructed with the character set C as the leaf node, the number of times or the frequency set W as the weighted value of the nodal points. The left branch of Huffman tree stands for "0" and the right branch represents "1".

The string of "0" or "1" on the branch of the path from the root node to each leaf node, and the corresponding encoding for that node, called Huffman coding.

Data Structure Learning Experience Series (iii)

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.