Data Structure Learning notes (4) --- traversing Binary Trees

Source: Internet
Author: User

Traverse Binary Trees

 

A binary tree is a non-linear data structure. The so-called traversal binary tree is to access each node in the binary tree in a certain order, requiring each node to be accessed once and only once.

The traversal operation is actually a process of linearity of the non-linear structure, and the result is a linear sequence.

 

Binary Tree operations

(1) first-order traversal-the final condition is whether the binary tree is empty.

First, access the root node;

Then, access the left subtree in sequence;

Then, access the right subtree in sequence.


(2) sequential traversal-the condition for ending is whether the binary tree is empty LTr.

Traverse the left subtree in the middle order;

Then access the root node;

Traverse the right subtree in the middle order.


(3) Post-order traversal-the condition for ending is whether the binary tree is empty LRT.

Traverse the left subtree sequentially;

Traverse the right subtree in the descending order;

Then, access the root node in sequence.


(4) Two traversal sequences are known to be used to obtain the original binary tree.

1. If the first, middle, middle, and back orders are known, the original binary tree can be introduced.

2. If the first and last orders are known, the original binary tree cannot be obtained.

 

Steps:

First, find the root node;

Determine the left and right subtree Based on the root node.

 

Example 1:

Preface: abcdefgh

Central order: bdceafhg

After order?


Descending order: decbhgfa

 

Example 2:

Preface: abdghcefi

Middle Order: gdhbaecif

After order?


Post-order: ghdbeifca

 

Example 3:

Central order: bdceafhg

Descending order: decbhgfa

What is the first order?


Preface: abcdefgh


Data Structure Learning notes (4) --- traversing Binary Trees

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.