Data Structure Tutorial 27th lesson Experiment 62 Fork Tree Experiment

Source: Internet
Author: User

Teaching Purpose: grasping the chain-type storage structure of binary tree

Teaching emphases: A chain-type storage implementation method of two-fork tree

Teaching Difficulties:

Teaching Content:

The following two fork trees are generated and three traversal results are obtained:

A chain-type storage structure representation of a one or two-fork tree

typedef struct bitnode{

Telemtype data;

struct Bitnode *lchild,*rchild;

}bitnode,*bitree;

Implementation of chain storage algorithm for two or two-fork tree

Createbitree (&t,definition);

Insertchild (T,P,LR,C);

Recursive traversal of three or two-fork tree

Preordertraverse (T,visit ());

Inordertraverse (T,visit ());

Postordertraverse (T,visit ());

Sample source Program

#include <alloc.h> #define ERROR 0;


#define OK 1;

typedef int ELEMTYPE;
  typedef struct BINARYTREE {elemtype data;
  struct BinaryTree *l;
struct BinaryTree *r;

}*bitree,binode;

Binode * NEW () {return ((Binode *) malloc (sizeof (Binode)));} Createsubtree (bitree *t,elemtype *all,int i) {if (all[i]==0) | |
      I&GT;16) {*t=null;
    return OK;
  } *t=new ();
  if (*t==null) return ERROR;
  (*t)->data=all[i];
  Createsubtree (& (*t)->l), all,2*i);
Createsubtree (& (*t)->r), all,2*i+1);
  } createbitree (Bitree *t) {elemtype all[16]={0,1,2,3,0,0,4,5,0,0,0,0,6,0,0,0,};
Createsubtree (t,all,1);

} printelem (Elemtype d) {printf ("%d\n", d);} Preordertraverse (Bitree t,int (*visit) (Elemtype D)) {if (T) {if (Visit (T->data)) if (preordertraverse
    , Visit)) if (Preordertraverse (t->r,visit)) return OK;
  return ERROR;
else return OK;
  Main () {Bitree root;
  Createbitree (&root); Preordertraverse (Root,printelem);

} 

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.