Binary tree traversal

Source: Internet
Author: User

The first and middle orders in the binary tree are easy to confuse and record the concept.

Traversal changes the node information in a binary tree from a non-linear arrangement to a linear arrangement in a certain sense. That is to say, traversal operations make the nonlinear structure linear.

A binary tree consists of three parts: the root node, the left subtree, And the right subtree. If it is specified that D, L, and R represent traversing the root node, traversing the left subtree, and traversing the right subtree respectively, binary tree traversal methods include DLR, DRL, LDR, LRD, RDL, and RLD. Since there is no essential difference in algorithm design between traversing the left subtree first and traversing the right subtree first, we only discuss three methods: DLR (first-order traversal) and LDR (middle-order traversal) and LRD (post-order traversal ).

 

We will perform sequential traversal (DLR), central sequential traversal (LDR), and post-sequential traversal (LRD) on the following trees. We will try to write a variety of traversal letters and then look at the answer.

1. Sequential traversal (DLR)

The basic idea of sequential traversal is: first access the root node, then traverse its left subtree in sequence, and then traverse its right subtree in sequence.

DLR result A B D H I E J C F G

 

2. DRL

The basic idea of DRL is to first access the root node, then traverse its right subtree in sequence, and then traverse its left subtree in sequence.

DRL result A C G F B E J D I H
 

3. The basic idea of LDR is: first traverse the left subtree of the root node in the middle order, then access the root node, and finally traverse its right subtree in the middle order.

LDR result H D I B J E A F C G

 

4. RDL

The basic idea of RDL is: first traverse the right subtree of the root node in the middle order, then access the root node, and then traverse its left subtree in the middle order.

RDL result G C F A E J B I D H

 

5. The basic idea of post-sequential traversal (LRD) Post-sequential traversal is: first, traverse the left subtree of the root node in a descending order, and then traverse the right subtree of the root node in a descending order, last access to the root node

LRD result H I D J E B F G C

 

6. RLD

The basic idea of RLD is: first traverse the right subtree of the root node sequentially, then traverse the left subtree of the root node sequentially, and finally access the root node.

RLD result G F C J E I H D B

 

Achieve binary tree sorting, find /// <summary> /// 1. Select the first element of the array to be arranged as the following node. // 2. Start from the element whose index is 1 to the last one, compared with the root node, if <= root node, it goes through the left subtree of the root node until the last node. If <= last node, it is added to the left of the node, otherwise, the root node is added to the right of the node. // if the root node is added, the right subtree of the root node is used until the last node, if <= The Last node is added to the left of the node, otherwise, to the right of the node, // 3. Print the binary tree in ascending order and print out the characters. // </summary> public class BinaryTreeSort {// member variable private NodeSort _ head; // Head pointer private int [] aInt; // used to construct a binary tree public NodeSort head // Head pointer {get {return _ head ;}}/ /Constructor public BinaryTreeSort (int [] arrint) {aInt = arrint; _ head = new NodeSort (aInt [0]); // Add a header node for (int I = 1; I <aInt. length; I ++) {Insert (aInt [I]) ;}} public void Insert (int I) {NodeSort newNode = new NodeSort (I); NodeSort current = _ head; nodeSort parent; while (true) {parent = current; if (I <current. value) {current = current. left; if (current = null) {parent. left = newNode; break;} Else {current = current. right; if (current = null) {parent. right = newNode; break ;}}} public void PreOrder (NodeSort) // first traverse {if (NodeSort! = Null) {Console. write ("{0}", NodeSort. toString (); // print the character PreOrder (NodeSort. left); // recursive PreOrder (NodeSort. right); // recursive} public void MidOrder (NodeSort) // perform the Middle-order traversal {if (NodeSort! = Null) {MidOrder (NodeSort. left); // recursive Console. write ("{0}", NodeSort. toString (); // print the MidOrder (NodeSort. right); // recursive} public void AfterOrder (NodeSort) // perform subsequent traversal {if (NodeSort! = Null) {AfterOrder (NodeSort. left); // recursive AfterOrder (NodeSort. right); // recursive Console. write ("{0}", NodeSort. toString (); // print characters} public bool Find (int value) {return SubFind (_ head, value);} bool flg = false; private bool SubFind (NodeSort rootnode, int value) {if (rootnode! = Null) {if (value> rootnode. Value) {if (! Flg) SubFind (rootnode. Right, value);} else if (value <rootnode. Value) {if (! Flg) SubFind (rootnode. left, value) ;}else {flg = true ;}} return flg ;}} public class NodeSort {// member variable private int _ data; // data private NodeSort _ left; // Left child private NodeSort _ right; // right child public int Data {get {return _ data;} public NodeSort left // Left child {get {return _ left ;} set {_ left = value ;}} public NodeSort Right // right child {get {return _ right ;}set {_ Right = value ;}} // constructor public NodeSort (int data) {_ data = data;} public override string ToString () {return _ data. toString () ;}public int Value {get {return _ data ;}}}

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.