An algorithm for sequencing, middle order and post-order traversal with two-fork tree stored in binary list

Source: Internet
Author: User

a binary tree linked list is used as the storage structure to complete the establishment of the two-fork tree, the operation of the total number of leaves and nodes by the sequence, sequence and order and the operations of the hierarchy traversal.



#include <iostream>
#include <cstdio>
#include <stdlib.h>
using namespace Std;
typedef int ELEMTYPE;
typedef struct BITNODE
{
Elemtype data;//Data domain
struct bitnode* lchild,*rchild; Left and right sub-tree domains;
}bitnode,*bitree;

int Create (Bitree *t)
{
Elemtype ch;
Elemtype temp;
scanf ("%d", &ch);
Temp=getchar ();
if (ch==-1)
{
*t=null;
}
Else
{
*t= (bitree) malloc (sizeof (Bitnode));
if (! ( *T))
{
Exit (-1);
}
Else
{
(*t)->data=ch;
printf ("Please enter the value of the left node of%d", ch);
Create (& (*t)->lchild);
printf ("Please enter the value of the right node of%d", ch);
Create (& (*t)->rchild);
}

}
return 1;

}

void Traverse (Bitree T)//Pre-order traversal binary tree
{
if (null==t)
{
Return
}
Else
{
printf ("%d", t->data);
Traverse (T->lchild);
Traverse (T->rchild);
}

}

Middle sequence Traversal binary tree
void Midtraverse (Bitree T)
{
if (t==null) {return;}
Midtraverse (T->lchild);
printf ("%d", t->data);
Midtraverse (T->rchild);
}

Sequential traversal of binary tree
void Lastraverse (Bitree T)
{
if (t==null) {return;}
Lastraverse (T->lchild);
Lastraverse (T->rchild);
printf ("%d", t->data);

}

Finding the depth of a binary tree
int Treedeep (Bitree T)
{
int deep=0;
if (T)
{
int Leftdeep=treedeep (t->lchild);
int Rightdeep=treedeep (t->rchild);
deep=leftdeep>=rightdeep?leftdeep+1:rightdeep+1;

}
return deep;

}

Finding the number of leaf nodes in binary tree
int Leafcount (Bitree t,int &num)
{
if (T)
{
if (t->lchild==null&&t->rchild==null)
{
num++;
}
Leafcount (T->lchild,num);
Leafcount (T->rchild,num);
}
return num;
}



int main ()
{
Bitree T;
Bitree *p= (bitree*) malloc (sizeof (bitree));
int deepth=0,num=0;
printf ("Please enter the value of the first node, 1 means no leaf node: \ n");
Create (&AMP;T);
printf ("Sequential traversal of binary tree: \ n");
Traverse (T);
printf ("\ n");
printf ("middle order traversal binary tree: \ n");
Midtraverse (T);
printf ("\ n");
printf ("Post-secondary traversal of binary tree: \ n");
Lastraverse (T);
printf ("\ n");
Deepth=treedeep (T);
printf ("Depth of tree:%d\n", deepth);
printf ("\ n");
Leafcount (T,num);
printf ("The number of leaf nodes in a binary tree is:%d\n", num);
printf ("\ n");
return 0;


}

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.