tree.cpp

来自「本程序在visual c++环境下实现二叉树的层次遍历.」· C++ 代码 · 共 60 行

CPP
60
字号
// Tree.cpp: implementation of the Tree class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Tree.h"
#include "queue.h"
#include "stdlib.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Tree::Tree()
{

}

Tree::~Tree()
{

}



//中序输入创建树
binode * Tree::createTree()
{
	int x;
	binode * p;
	scanf("%d",&x);
	if(x==0)
		return NULL;
	p = (binode *) malloc (sizeof(binode));
	p->data = x ;
	printf("%d -> left:",x);
	p->lchild = createTree ();
	printf("%d -> right:",x);
	p->rchild = createTree ();
	return p;
}

//树的层次遍历
void Tree::Travelsal(binode * q)
{
	queue queue1;
	struct binode * temp;
	temp = (struct binode *) malloc (sizeof(struct binode));
	queue1.Add_Queue(q);
	while(queue1.head!=NULL)
	{
	temp=queue1.Del_Queue();
	printf("%d",temp->data);
	if(temp->lchild!=NULL)
	queue1.Add_Queue(temp->lchild);//QueueHead=
	if(temp->rchild!=NULL)
	queue1.Add_Queue(temp->rchild);//QueueHead=
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?