Implementation of binary tree sequential structure/chain-structured implementation

Source: Internet
Author: User

Geoscience "Big Talk Data Structure" this part of the source of the mess up eight bad. Summarize yourself under:



Sequential structure:

#include "stdafx.h"
#include "string.h"
#include "stdio.h"
#include "Stdlib.h"
#include <iostream>
using namespace Std;


#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0


#define MAXSIZE 100
#define MAX_TREE_SIZE 100
typedef int TELEMTYPE;
typedef TELEMTYPE SQBITREE[MAX_TREE_SIZE];//Sequential structure is equivalent to the array structure
#define MAX_NODE_SIZE 100//two fork tree maximum node count
typedef char SQBITREE[MAX_NODE_SIZE+1]; Number of NO. 0 unit nodes


Create a two-fork tree
void Creat_tree (Sqbitree &t)
{
int i=0;
Char ch;
while ((Ch=getchar ())! = ' $ ')
{
i++;
T[i]=ch;
}
T[0]=i;
}


Gets the node position of the left child for a given node (position)
int leftchild_locate (sqbitree &t,int node)
{
if ((2 * node) > T[0])
return-1;
Else
Return 2 * node;
}
Gets the node position of the right child for a given node (position)
int rightchild_locate (sqbitree &t,int node)
{
if ((2 * node+1) > T[0])
return-1;
Else
Return 2 * NODE+1;
}


Sequence traversal
void Level_order (Sqbitree &t)
{
for (int i=1;i<=t[0];i++)
if (t[i]!= ' $ ')
cout<<t[i]<< "";
}
First Order traversal
void Pre_order (Sqbitree &t,int i)
{
if (t[0]<=0)
{
cout<< "Empty tree!" <<endl;
}
Else
{
if (t[i]!= ' $ ')
cout<<t[i]<< "";
if (Leftchild_locate (t,i)!=-1)//If left dial hand node exists, recursion
Pre_order (T,leftchild_locate (t,i));
if (Rightchild_locate (t,i)!=-1)//If the right child node exists, recursion
Pre_order (T,rightchild_locate (t,i));
}
}
Middle Sequence traversal
void Mid_order (Sqbitree &t,int i)
{
if (t[0]<=0)
{
cout<< "Empty tree!" <<endl;
}
Else
{
if (Leftchild_locate (t,i)!=-1)//If left dial hand node exists, recursion
Mid_order (T,leftchild_locate (t,i));
if (t[i]!= ' $ ')
cout<<t[i]<< "";
if (Rightchild_locate (t,i)!=-1)//If the right child node exists, recursion
Mid_order (T,rightchild_locate (t,i));
}
}//post-sequential traversal
void Back_order (Sqbitree &t,int i)
{
if (t[0]<=0)
{
cout<< "Empty tree!" <<endl;
}
Else
{
if (Leftchild_locate (t,i)!=-1)//If left dial hand node exists, recursion
Back_order (T,leftchild_locate (t,i));
if (Rightchild_locate (t,i)!=-1)//If the right child node exists, recursion
Back_order (T,rightchild_locate (t,i));
if (t[i]!= ' $ ')
cout<<t[i]<< "";
}
}
int main ()
{
cout<< "Create two fork tree:" <<endl;
Sqbitree SBT;
Create a sequential binary tree
Creat_tree (SBT);
Sequence traversal
cout<< "sequence traversal:" <<endl;
Level_order (SBT);
cout<<endl;
First Order traversal
cout<< "Pre-sequence traversal:" <<endl;
Pre_order (sbt,1);
cout<<endl;
Middle Sequence traversal
cout<< "Middle sequence Traversal:" <<endl;
Mid_order (sbt,1);
cout<<endl;
Subsequent traversal
cout<< "post-post traversal:" <<endl;
Back_order (sbt,1);
cout<<endl;


Cout<<leftchild_locate (sbt,3);
System ("pause");
return 0;
}


Chain-Type structure:

#include "stdafx.h"
#include "stdio.h"
#include "io.h"
#include "string.h"
#include "stdlib.h"//except for the memory allocation of malloc Free new Delete These are also the commands for system pause.


typedef struct BINODE
{
char data;
struct Binode *lchild,*rchild;
}binode,*linkbitree;


Recursive construction of chain binary tree
void Creattree (Linkbitree &t)
{
Char ch;
printf ("Create chain binary tree");
Ch=getchar ();
if (ch== ' # ')
{t=null;
return;}
Else
{
T=new Binode;
t->data=ch;
Creattree (T->lchild);
Creattree (t->rchild);//Always build the left subtree until the left dial hand tree is established, #表示建立完了, establish the right subtree, which is established by the way of the former sequence traversal
}

}


void preorder (Linkbitree &t)
{
if (t==null)
Return
Else
{printf ("%c", t->data);
Preorder (T->lchild);
Preorder (t->rchild);}
}


void Midorder (Linkbitree &t)
{
if (t==null)
Return
Else
{Midorder (t->lchild);

printf ("%c", t->data);
Midorder (t->rchild);}
}


void Backorder (Linkbitree &t)
{
if (t==null)
Return
Else
{backorder (t->lchild);

Backorder (T->rchild);

printf ("%c", T->data);}
}
int main ()
{
Linkbitree T;
Creattree (T);
Pre-sequence traversal
Preorder (T);
printf ("\ n");
Middle Sequence traversal
Midorder (T);
printf ("\ n");
Subsequent traversal
Backorder (T);
printf ("\ n");


System ("pause");
return 0;
}

The areas to note are:

The chained structure is created recursively, and The Terminator is #.

If the initial creation is a 1233456########### #这种的话, it is actually a left-leaning tree. The final sequence traversal and subsequent traversal results are the same, and are the reverse of the pre-order traversal.


Because I can't tell how many # endings there should be, so I simply hit a lot of endings. But this will get the right and intuitive traversal.

Implementation of binary tree sequential structure/chain-structured 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.