A simple collection of tree operations--based on C implementation

Source: Internet
Author: User

Many of the data structure of the book on the interpretation of the information structure is implemented by pseudo-code, in fact, it is not intuitive, so for all the data structure operation I will use C to implement it again.
The tree is the basis of learning the binary tree, but also the basis of understanding B-tree, plus tree, the following is a few simple operation of the tree, easy to understand.

Data
//-------数据结构----------------------------------------#define m 3  //定义度为3的树typedefchar datatype;typedefstruct node{    datatype data;    struct node* child[m];}treenode;//------------------------------------------------------
Basic operations
//--------operation------------------------------------------voidPreorder (treenode* t) {intIif(t) {printf("%c", T->data); for(i=0; i<m;i++) {preorder (t->child[i]); }    }}voidPostorer (treenode* t) {intIif(t) { for(i =0; i<m;i++) {postorer (t->child[i]); }printf("%c", T->data); }}//must use a pointer pointer, if the argument is a pointer, the parameter is just a copy of the argument pointer, so the value of the argument pointer is not changed. voidCreatetree (treenode** p) {//input directly to ab## #C # # #D # # # #回车    intICharChscanf("%c", &ch);if(ch = =' # ') {*p = NULL; }Else{*p = (treenode*)malloc(sizeof(node)); (*p)->data = ch; for(i =0; i<m;i++) {Createtree (& (*p)->child[i]); }    }}//------------------------------------------------------

For the operation of the tree does not need to do too much, most of them are for the operation of the binary tree, so give a few simple operations, enough.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

A simple collection of tree operations--based on C implementation

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.