二叉树的相关操作

二叉树的相关操作

BintreeNode.h
#ifndef BINTREENODE_H
#define BINTREENODE_H
typedef char Datatype;

typedef struct BinTreeNode
{
	Datatype data;
	struct BinTreeNode *lchild;
	struct BinTreeNode *rchild;
}BinTreeNode;

BinTreeNode *CreateNode(Datatype elem);

#endif

BinTree.h
#ifndef BINTREE_H
#define BINTREE_H
#include "BinTreeNode.h"

typedef struct BinTree
{
	BinTreeNode *root;
	int count;
}BinTree;

void Init(BinTree *r);
void BinTreeCreate(BinTree *r);
int BinTreeDepth(BinTreeNode *r);
int BinTreeWidth(BinTreeNode *r);
int BinTreeNodes(BinTree *r);
int BinTreeLeaves(BinTreeNode *r);
void BinTreeDestroy(BinTreeNode *r);
BinTreeNode *FindNode(BinTreeNode *r,Datatype elem);
void DisplayBinTree(BinTreeNode *r);
void PreOrder(BinTreeNode *r);
void PostOrder(BinTreeNode *r);
void InOrder(BinTreeNode *r);
void AllPath(BinTreeNode *r,char path[],int pathlen);
#endif

BinTreeNode.c
#include<stdio.h>
#include<stdlib.h>
#include "BinTreeNode.h"

BinTreeNode *CreateNode(Datatype elem)
{
	BinTreeNode *p=(BinTreeNode *)malloc(sizeof(BinTreeNode));
	if(!p)
	{
		printf("No enough memory!");
		exit(-1);
	}
	p->data=elem;
	p->lchild =p->rchild =NULL;
	return p;
}


BinTree.c
#include<stdio.h>
#include<stdlib.h>
#include"BinTree.h"

int Leavescount=0;

void Init(BinTree *r)
{
	r->count=0;
	r->root=NULL;
}

void BinTreeCreate(BinTree *r)
{
	char str[100];
	int i=0,top=-1;
	char flag;
	BinTreeNode *p=NULL;
	BinTreeNode *Stack[50];
	printf("Please input a string: ");
	scanf("%s",str);
	getchar();
	while(str[i]!=‘\0‘)
	{
		switch(str[i])
		{
		    case ‘(‘:
				top++;
				Stack[top]=p;
				flag=‘l‘;
				break;
		    case ‘,‘:
				flag=‘r‘;
				break;
		    case ‘)‘:
				top--;
				break;
		   default:
			   p=CreateNode(str[i]);
			   if(r->root==NULL)
			   {
				   r->root =p;
				   r->count =1;
			   }
			   else
			   {
				   if(flag==‘l‘)
				   {
					   Stack[top]->lchild =p;
					   r->count++;
				   }
				   else
				   {
					   Stack[top]->rchild =p;
				       r->count++;
				   }
			   }
			   break;
		}//end of switch
		i++;
	}//end of while
}

int BinTreeDepth(BinTreeNode *r)
{
	int ldepth=0,rdepth=0;
	if(!r)
		return 0;
	else
	{
		ldepth=BinTreeDepth(r->lchild);
		rdepth=BinTreeDepth(r->rchild);
		return (ldepth>rdepth?(ldepth+1):(rdepth+1));
	}
}

void DisplayBinTree(BinTreeNode *p)
{
	if(p)
	{
		printf("%c",p->data);
		if(p->lchild ||p->rchild)
		{
			printf("(");
			DisplayBinTree(p->lchild );
			printf(",");
            DisplayBinTree(p->rchild );
			printf(")");
		}
	}
}

int BinTreeNodes(BinTree *r)
{
	return r->count;
}

int BinTreeLeaves(BinTreeNode *r)
{
	if(r)
	{
		BinTreeLeaves(r->lchild);
		BinTreeLeaves(r->rchild);
	    if(!r->lchild&&!r->rchild)
			Leavescount++;
	}
	return Leavescount;
}

void BinTreeDestroy(BinTreeNode *r)
{
	BinTreeNode *p=r;
	if(!p)
		return;
	BinTreeDestroy(p->lchild);
	BinTreeDestroy(p->rchild);
	free(p);
	p=NULL;
}


BinTreeNode *FindNode(BinTreeNode *r,Datatype elem)
{
	BinTreeNode *p=NULL;
	if(r==NULL)
		return NULL;
	else if(r->data==elem)
		return r;
	else
	{
		p=FindNode(r->lchild,elem);
		if(p)
			return p;
		else
			return FindNode(r->rchild,elem);
	}
}

int BinTreeWidth(BinTreeNode *r)
{
	struct  Queue
	{
		int lnode;
		BinTreeNode *p;
	}Queue[50];
	int front,rear;
	int lnum,max,i,n;
	front=rear=0;
	if(r!=NULL)
	{
		rear++;
		Queue[rear].p=r;
		Queue[rear].lnode=1;
		while(rear!=front)
		{
			front++;
			r=Queue[front].p;
			lnum=Queue[front].lnode;
			if(r->lchild!=NULL)
			{
				rear++;
				Queue[rear].p=r->lchild;
				Queue[rear].lnode=lnum+1;
			}
			if(r->rchild!=NULL)
			{
				rear++;
				Queue[rear].p=r->lchild;
				Queue[rear].lnode=lnum+1;
			}
		}
		max=0;
		lnum=1;
		i=1;
		while(i<=rear)
		{
			n=0;
			while(i<=rear&&Queue[i].lnode==lnum)
			{
				n++;
				i++;
			}
			lnum=Queue[i].lnode;
			if(max<n)
				max=n;
		}
		return max;
	}
	else
		return 0;
}

void PreOrder(BinTreeNode *r)
{
	BinTreeNode *Stack[50];
	int top=-1;
	BinTreeNode *p;
	if(r!=NULL)
	{
		top++;
		Stack[top]=r;
		while(top>-1)
		{
			p=Stack[top];
			top--;
			printf("%c",p->data);
			if(p->rchild!=NULL)
			{
				top++;
				Stack[top]=p->rchild;
			}
			if(p->lchild!=NULL)
			{
				top++;
				Stack[top]=p->lchild;
			}
		}
		printf("\n");
	}
}

void InOrder(BinTreeNode *r)
{
	BinTreeNode *Stack[50];
	int top=-1;
	BinTreeNode *p=NULL;
	if(r!=NULL)
	{
		p=r;
		while(top>-1||p!=NULL)
		{
			while(p!=NULL)
			{
				top++;
				Stack[top]=p;
				p=p->lchild;
			}
			if(top>-1)
			{
				p=Stack[top];
				top--;
				printf("%c",p->data);
				p=p->rchild;
			}
		}
		printf("\n");
	}
}

void PostOrder(BinTreeNode *r)
{
	BinTreeNode *p=NULL;
	struct Stack
	{
		BinTreeNode *pt;
		int tag;
	}Stack[50];
	int top=-1;
	top++;
	Stack[top].pt=r;
	Stack[top].tag=1;
	while(top>-1)
	{
		if(Stack[top].tag==1)
		{
			p=Stack[top].pt;
			top--;
			if(p!=NULL)
			{
				top++;
				Stack[top].pt=p;
				Stack[top].tag=0;
				top++;
				Stack[top].pt=p->rchild;
				Stack[top].tag=1;
				top++;
				Stack[top].pt=p->lchild;
				Stack[top].tag=1;
			}
		}
		if(Stack[top].tag==0)
		{
			printf("%c",Stack[top].pt->data);
			top--;
		}
	}
}

void AllPath(BinTreeNode *r,char path[],int pathlen)
{
	int i;
	if(r!=NULL)
	{
		if(r->lchild==NULL&&r->rchild==NULL)
		{
			printf("The path is %c to root: %c ",r->data,r->data );
			for(i=pathlen-1;i>=0;i--)
				printf("%c ",path[i]);
			printf("\n");
		}
		else
		{
			path[pathlen]=r->data;
			pathlen++;
			AllPath(r->lchild,path,pathlen);
			AllPath(r->rchild,path,pathlen);
			pathlen--;
		}
	}
}

/*                          a
                        b        d
					c                 e
*/


main.c
#include<stdio.h>
#include<stdlib.h>
#include"BinTreeNode.h"
#include"BinTree.h"

void menu(void)
{
	printf("\n");
	printf("-----------------------------------------------------\n");
	printf("                    Menu                  Version 1.0\n");
	printf("-----------------------------------------------------\n");
	printf("1.Tree nodes                 2.Display the tree      \n");
	printf("3.Create the tree            4.Tree Depth            \n");
	printf("5.Tree width                 6.Tree leaves           \n");
	printf("7.Find node                  8.PreOrder              \n");
	printf("9.InOrder                    10.PostOrder            \n");
	printf("11.AllPath                   12.Destroy and exit     \n");
	printf("Please input your choice: ");
}


int main(void)
{
	BinTree t;
	int choice;
	char elem;
	char path[50];
	Init(&t);
	while(1)
	{
		menu();
		scanf("%d",&choice);
		getchar();
		switch(choice)
		{
		case 1:
			printf("The nodes of the tree is: %d !\n",BinTreeNodes(&t));
			break;
		case 2:
			DisplayBinTree(t.root);
			printf("\n");
			break;
		case 3:
			BinTreeCreate(&t);
			break;
		case 4:
			printf("The depth of the tree is: %d !\n",BinTreeDepth(t.root));
			break;
		case 5:
			printf("The width of the tree is: %d !\n",BinTreeWidth(t.root));
			break;
		case 6:
			printf("The leaves of the tree is: %d !\n",BinTreeLeaves(t.root));
			break;
		case 7:
			printf("Please input the node you want to check: ");
			scanf("%c",&elem);
			getchar();
			if(FindNode(t.root,elem))
				printf("Find success!\n");
			else
				printf("Not find!\n");
			break;
		case 8:
			PreOrder(t.root);
			break;
		case 9:
			InOrder(t.root);
			break;
		case 10:
			PostOrder(t.root);
			break;
		case 11:
			AllPath(t.root,path,0);
			break;
		case 12:
			BinTreeDestroy(t.root);
			exit(0);
			break;
		default:
			printf("Illegal input!\n");
			break;
		}
	}
	return 0;
}


二叉树的相关操作

上一篇:ebs fnd_lookups


下一篇:图的相关操作