Data Structure Arrangement (2) tree and Data Structure Arrangement

Source: Internet
Author: User

Data Structure Arrangement (2) tree and Data Structure Arrangement
I. Preface

For project source code and other statements, see data structure (1) linear structure.

Ii. Related Concepts

As a one-to-multiple nonlinear data structure that is widely used, a tree not only has a direct relationship between data, but also has a hierarchical relationship. For example, see Figure 1. Because of the complex structure of the tree, we generally convert the tree into a binary tree for processing to simplify operation and storage. Therefore, this article mainly discusses the binary tree.

  1. Binary Tree
    A binary tree is a tree structure where each node can have up to two subnodes. If the root node is removed, the remaining nodes are divided into two Subtrees, which are called left and right Subtrees respectively. A binary tree is an ordered tree. The left and right Subtrees have a strict order. If they are reversed, they become a different binary tree.
  2. Full Binary Tree
      
    Full binary tree. As the name suggests, all nodes except the leaf node have two children, and the leaf node has a binary tree on the same layer. See figure 2.
  3. Full Binary Tree
      
    Complete Binary Tree: after the last layer is removed, it is a full binary tree, and the nodes at the last layer are continuously concentrated at the leftmost. See figure 3. 1 /// <summary> 2 /// first traverse (DLR) 3 /// </summary> 4 /// <! [CDATA [first access the Node, then traverse the left subtree, and finally the right subtree]> 5 static void PreOrder (Node <char> root) 6 {7 if (root = null) 8 {9 return; 10} 11 12 Print (root); 13 PreOrder (root. LChild); 14 PreOrder (root. RChild); 15} 16 17 // <summary> 18 // middle-order traversal (LDR) 19 /// </summary> 20 // <! [CDATA [traverse the left subtree first, then the root Node, and finally the right subtree]> 21 static void InOrder (Node <char> root) 22 {23 if (root = null) 24 {25 return; 26} 27 28 InOrder (root. LChild); 29 Print (root); 30 InOrder (root. RChild); 31} 32 33 // <summary> 34 // post-order traversal (LRD) 35 /// </summary> 36 // <! [CDATA [first traverse the left subtree, then traverse the right subtree, and finally traverse the root Node]> 37 static void PostOrder (Node <char> root) 38 {39 if (root = null) 40 {41 return; 42} 43 44 PostOrder (root. LChild); 45 PostOrder (root. RChild); 46 Print (root); 47} 48 49 // <summary> 50 // sequence traversal 51 /// </summary> 52 // <! [CDATA [from top down to left]> 53 static void LevelOrder (Node <char> root) 54 {55 if (root = null) 56 {57 return; 58} 59 CSeqQueue <Node <char> sq = new CSeqQueue <Node <char> (50); 60 sq. in (root); 61 while (! Sq. IsEmpty () 62 {63 Node <char> tmp = sq. Out (); 64 Print (tmp); 65 66 if (tmp. LChild! = Null) 67 {68 sq. In (tmp. LChild); 69} 70 71 if (tmp. RChild! = Null) 72 {73 sq. In (tmp. RChild); 74} 75} 76}

     

Related Article

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.