Sequence-threaded binary tree in C + +

Source: Internet
Author: User

1#include <iostream>2 using namespacestd;3 4typedefstructTbtnode5 {6     Chardata;7     intLtag,rtag;8     structTbtnode *Lchild;9     structTbtnode *Rchild;Ten }tbtnode; One  ATbtnode *Inittbtnode () - { -Tbtnode * p= (tbtnode*)malloc(sizeof(Tbtnode)); theP->lchild=0; -P->rchild=0; -p->ltag=0; -p->rtag=0; +     returnp; - } +  ATbtnode * Inittbtree (Tbtnode *p) at { -Tbtnode * a=Inittbtnode (); -Tbtnode * b=Inittbtnode (); -Tbtnode * c=Inittbtnode (); -Tbtnode * d=Inittbtnode (); -Tbtnode * e=Inittbtnode (); in  -A->data='A'; toB->data='B'; +C->data='C'; -D->data='D'; theE->data='E'; *  $A->lchild=B;Panax NotoginsengA->rchild=C; -B->rchild=D; theC->lchild=E; +  Ap=A; the  +     returnp; - } $  $ voidInthread (Tbtnode *p,tbtnode *&pre) - { -     if(p!=0) the     { -Inthread (p->lchild,pre);Wuyi         if(p->lchild==0) the         { -P->lchild=Pre; Wup->ltag=1; -         } About         if(pre!=0&&pre->rchild==0) $         { -Pre->rchild=p; -pre->rtag=1; -         } APre=p; +Inthread (p->rchild,pre); the     } - } $  theTbtnode * FIRST (Tbtnode *p) the { the      while(p->ltag==0) theP=p->Lchild; -     returnp; in } the  theTbtnode * Next (Tbtnode *p) About { the     if(p->rtag==0) the         returnFirst (p->rchild); the     Else +         returnP->Rchild; - } the Bayi voidInorder (Tbtnode *root) the { the      for(Tbtnode *p=first (root);p! =0;p =Next (p)) -cout << P->data <<" "; -cout <<Endl; the } the  the voidCreateinthread (Tbtnode *root) the { -Tbtnode * pre =Inittbtnode (); the     if(root!=0) the     { the Inthread (root,pre);94Pre->rchild=0; thepre->rtag=1; the     } the }98  About intMainintargcChar*argv[]) - {101Tbtnode *p=Inittbtnode ();102p=Inittbtree (p);103Tbtnode *pre=Inittbtnode ();104 Createinthread (p); the inorder (p);106     return 0;107}


Sequence-threaded binary tree in C + +

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.