Binary Tree roaming programming technology and skills Summary (I): recursive Technology

Source: Internet
Author: User
Tags addall
Binary Tree roaming-Summary of programming technology and skills (I): recursive technology this series is divided into three articles to summarize common techniques and skills in binary tree programming. The first article is about the recursion Technology of Binary Tree programming. The second article discusses how to convert recursive Programs into non-recursive Programs. The third article discusses other methods and technologies of Binary Tree programming. One and two forks

Binary Tree roaming-Summary of programming technology and skills (I): recursive technology this series is divided into three articles to summarize common techniques and skills in binary tree programming. The first article is about the recursion Technology of Binary Tree programming. The second article discusses how to convert recursive Programs into non-recursive Programs. The third article discusses other methods and technologies of Binary Tree programming. One and two forks



Binary Tree roaming-Summary of programming technology and skills (I): recursive Technology

This series is divided into three articles, which summarize common technologies and skills in binary programming. The first article is about the recursion Technology of Binary Tree programming. The second article discusses how to convert recursive Programs into non-recursive Programs. The third article discusses other methods and technologies of Binary Tree programming.

I,Binary Tree programming Overview

Program = Data Structure + algorithm. More precisely, the function implementation of any program can be achieved from a technical point of view by selecting an appropriate data structure and efficient algorithms. Binary Tree is a non-linear data structure, that is, each element may have 0, 1, or 2 successor nodes. This makes Binary Tree programming more difficult than linear table programming. On the other hand, because Binary Trees have natural recursion characteristics, to master the binary tree programming technology, you must understand how to use recursion to think about problems and master the programming of recursive Programs. This is for beginners, it is undoubtedly a threshold that must be crossed.

II,Recursive technology Overview

Recursive technology is not mysterious. From the method perspective, recursive technology uses the same method to solve subproblems that are smaller than the original problem, and merges the subproblems for implementation. recursion and grouping are closely related; technically, recursion is implemented through the same function call. That is, P (n) = P (I), P (j ),..., P (s), G (1 )).

It is not difficult to write A recursive program. There are three skills: A. analyze and find the recursive part; B. Determine the parameter form of the recursive call; C. Determine the recursive end condition. With these skills, you can write elegant recursive code without having to know more about the recursive mechanism. In the following example, some related methods and techniques will be given gradually.

III,Recursive solution technology for Binary Trees

The recursive solution of Binary trees can be summarized as follows:

S1: Solve the root node;

S2: recursively solving the left subtree;

S3: recursively solves the right subtree;

S4: merges the root node, left subtree, and right subtree to obtain the original problem.

Smart readers immediately realized that the above steps are very similar to the first-order traversal of Binary Trees. For specific problems, the sequence of S1, S2, and S3 may change.

A. analyze and find the recursive part:

Obviously, the left and right subtree of the root node of a binary tree are both Binary Trees. Therefore, you can use the same method to solve the left and right subtree. This is the recursive part;

B. Determine the parameter form of recursive call:

This is also obvious, that is, the root node of the binary tree is used as the call parameter. According to the problem, some other parameters may be added to indicate the depth of the current traversal and the list to be returned.

C. Determine recursive conditions. Generally, the root node needs to meet the conditions as recursive termination conditions according to the application.

Here is A tip: You can first analyze the basic binary tree structures of A/A (B,)/A (, B)/A (B, C, to understand the behavior and Mechanism of recursive Programs.

Iv. Example

(1) recursive traversal of Binary Trees in ascending, central, and descending order. This is the most basic. Please read the relevant books on your own;

(2) calculate the number of summary points of a binary tree.

A. analyze and find out the recursive part: Obviously, the number of sum points of A binary tree is 1 + the number of sum points of the Left subtree + the number of sum points of the right subtree. 1 indicates the root node.

B. recursive call parameters: the root node of the binary tree.

C. Recursive end condition: the root node is empty.

Now you can start writing recursive Programs. First, write the situation at the end of recursion, and then continue recursion. Mastering the writing of recursive Programs is not achieved overnight.

Publicint size (TreeNode root ){

If (root = null) {return 0 ;}

Else {

Return1 + size (root. getLChild () + size (root. getRChild ());

}

}

(3) Exchange left and right Subtrees of all nodes of a binary tree

At first glance, it seems very difficult. However, using recursive thinking, it is easy to think that if the root node is not empty, the left and right subtree of the root node will be exchanged; recursive solution to the left subtree; recursively solves the right subtree. Therefore, you can write a recursive program:

Publicvoid swapTree (TreeNode root)

{

If (root! = Null ){

TreeNodetmp = root. getLChild ();

Root. setLChild (root. getRChild ());

Root. setRChild (tmp );

SwapTree (root. getLChild ());

SwapTree (root. getRChild ());

}

}

(4) Calculate the longest path of a binary tree (if there are multiple, output one)

This problem seems to have no obvious recursion. In this case, you need to analyze it carefully. First, the longest path of a binary tree must include a non-empty root node. Secondly, the longest path must arrive at the leaf node. Then, what is the correlation between the longest path of a binary tree and the longest path of the left and right subtree? It is easy to think of: the longest path of a binary tree = non-empty root node + Max (the longest path of the Left subtree and the longest path of the right subtree ). In this way, you can sort out the following ideas:

LongestPath (root ):

If (root! = Null) {// if the root node is not empty, it is saved in the longest path.

LongestPath. add (root );

If (root. getLChild () = null & root. getRChild () = null ){

// Reach the leaf node and output the path

Returnpath;

}

Else {

// Recursively calculates the longest path of the Left subtree

LeftLongestPath = LongestPath (root. getLChild ());

// Recursively solves the longest path of the right subtree

RightLongestPath = LongestPath (root. getRChild ());

If (leftLongestPath. size ()> = rightLongestPath. size ())

{

// If the longest path of the Left subtree is greater than or equal to the longest path of the right subtree, the longest path of the Left subtree is used.

Path. addAll (leftLongestPath );

}

Else {

// Otherwise, take the longest path of the right subtree

Path. addAll (rightLongestPath );

}

}

}

Because the recursive solution of Binary Trees is usually very simple and the operation efficiency is within the acceptable range, some people even suggest that the recursive technology is preferred for binary tree problem solving. This article discusses how to compile a binary tree recursive program at the method level. These methods and techniques allow you to write very elegant recursive code even if you are not familiar with the recursive mechanism.

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.