Data structure algorithm (basic algorithm of tree)

Source: Internet
Author: User
Tags serialization

The serialization and deserialization of a tree

1) serialization and deserialization of the two-fork tree; the use of a pre-order.

1  PackageCom.tree;2 3 Importjava.util.LinkedList;4 ImportJava.util.Queue;5 6 //serializes a two-pronged tree into a string;7 //and deserializes a string into a tree.8  Public classtreenode_serialization {9      Public Static voidMain (string[] args) {TenTreenode_serialization s =Newtreenode_serialization (); OneString str = "2!3!10!#!#!12!123!#!#!#!18!100!#!#!#!";//Pre-order ATreeNode node =S.pre_deserializationtreenode (str); - s.test (node); - System.out.println (); the System.out.println (S.pre_serializtreenode (node)); -     } -     //serializes a two-pronged tree into a string -      PublicString pre_serializtreenode (TreeNode tree) { +         if(Tree = =NULL) { -             return"#!"; +         } AString str = tree.val + "!"; atstr + =Pre_serializtreenode (tree.left); -str + =Pre_serializtreenode (tree.right); -         returnstr; -     } -     //deserializes a string into a tree;//deserialization of a pre-sequence traversal -      PublicTreeNode Pre_deserializationtreenode (String str) { instring[] Arrstr = Str.split ("!"); -Queue<string> qu =NewLinkedlist<string>(); to          for(String s:arrstr) { + Qu.add (s); -         } the         returnPre_deserializationtreenodecore (qu); *     } $     //Pre-sequence traversal deserialization. Panax Notoginseng     PrivateTreeNode Pre_deserializationtreenodecore (queue<string>qu) { -String str =Qu.poll (); theTreeNode tree =NewTreeNode (); +         if("#". Equals (str)) { A             return NULL; the         } +Tree.left =Pre_deserializationtreenodecore (qu); -Tree.val =integer.valueof (str); $Tree.right =Pre_deserializationtreenodecore (qu); $         returnTree; -     } -      Public voidTest (TreeNode tree) { the         if(Tree! =NULL) { - test (tree.left);WuyiSystem.out.print (Tree.val + ""); the test (tree.right); -         } Wu     } -}
View Code

2) serialization and deserialization of the two-fork tree; A hierarchical traversal is used.

Second, the traversal of the tree (before and after the "recursive and non-recursive", hierarchical, showing the structure of the hierarchy traversal)

  

Data structure algorithm (basic algorithm of tree)

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.