⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tree.cpp

📁 本程序在visual c++环境下实现二叉树的层次遍历.
💻 CPP
字号:
// 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -