
今天我也遇到这道题了,经过我的研究,我觉得应该是如下的解答:
首先画出该树 :如下图左边所示。然后根据树的二叉链表表示法表示存储结构如图右边所示:
注意这里的指针域为左边表示第一个孩子firstchild,右边表示兄弟nextsibling
紧接着就涉及到了树与二叉树的转换:
核心思想:左子树放孩子,右子树放兄弟,则有如图所示的二叉树:
算法思想:层次遍历目前最普遍用的就是队列的那种方式,不是递归,但是用到while循环,既然题目要求用递归,可以用递归实现该while循环功能。算法如下:
void TransLevele(Tree r)
{
if (r==NULL)
{
return ;
}
printf("%c",r->ch);
if (r->left != NULL)
{
InsertQueue(r->left);
}
if (r->right != NULL)
{
InsertQueue(r->right);
}
Tree t = DeleteQueue();
TransLevele(t);
}
//测试程序,创建树输入例如ABD##E##C##,根左右创建的方式。
如下代码是测试通过的。
#include "stdlibh"
#define MAX 100
typedef int Element;
typedef struct tree
{
Element ch;
struct tree left;
struct tree right;
}Tree;
typedef struct queue
{
Tree a[MAX];
int front;
int rear;
}Queue;
Queue Qu;
void Init();
int InsertQueue(Element ch);
Tree DeleteQueue();
void CreateTree(Tree r);
void TransLevele(Tree r);
void PrintTree(Tree r);
int main()
{
Tree r=NULL;
CreateTree(&r);
PrintTree(r);
printf("\n");
TransLevele(r);
return 0;
}
void Init()
{
int i=0;
for (i=0; i<MAX; i++)
{
Qua[i] = NULL;
}
Qufront = 0;
Qurear = 0;
}
int InsertQueue(Tree r)
{
if ( (Qurear+1)%MAX == Qufront)
{
printf("Queue full!");
return 0;
}
Qua[Qurear] = r;
Qurear = (Qurear+1)%MAX;
return 1;
}
Tree DeleteQueue()
{
if (Qufront == Qurear)
{
printf("Queue empty");
return NULL;
}
Tree t=NULL;
t = Qua[Qufront];
Qufront = (Qufront+1)%MAX;
return t;
}
void CreateTree(Tree r)
{
Element ch;
ch=getchar();
if (ch=='#')
{
(r)=NULL;
return ;
}
r = (Tree )malloc(sizeof(Tree));
(r)->ch = ch;
CreateTree(&((r)->left));
CreateTree(&((r)->right));
}
void PrintTree(Tree r)
{
if (r==NULL)
{
return ;
}
printf("%c",r->ch);
PrintTree(r->left);
PrintTree(r->right);
}
void TransLevele(Tree r)
{
if (r==NULL)
{
return ;
}
printf("%c",r->ch);
if (r->left != NULL)
{
InsertQueue(r->left);
}
if (r->right != NULL)
{
InsertQueue(r->right);
}
Tree t = DeleteQueue();
TransLevele(t);
}
#include"stdioh"//二叉树
#include"stdlibh"
typedef
struct
node
{
char
data;
struct
node
lchild,rchild;
}BinTNode;
typedef
BinTNode
BinTree;
void
GreateBinTree(BinTree
T)//以先序遍历为依据构造二叉树,T为指向根指针的指针
{
//空结点以空格代替
char
ch;
if((ch=getchar())=='
')
T=NULL;
else
{
T=(BinTree)malloc(sizeof(BinTNode));
(T)->data=ch;
GreateBinTree(&((T)->lchild));
GreateBinTree(&((T)->rchild));
}
}
void
Lorder(BinTree
T)//先序遍历二叉树
{
if(T)
{
printf("%c
",T->data);
Lorder(T->lchild);
Lorder(T->rchild);
}
}
void
Morder(BinTree
T)//中序遍历二叉树
{
if(T)
{
Morder(T->lchild);
printf("%c
",T->data);
Morder(T->rchild);
}
}
void
Rorder(BinTree
T)//后序遍历二叉树
{
if(T)
{
Rorder(T->lchild);
Rorder(T->rchild);
printf("%c
",T->data);
}
}
指针解遍历数组例题:
#include <stdioh>
int main(){
int arr[] = { 99, 15, 100, 888, 252 };
int i, p = arr, len = sizeof(arr) / sizeof(int);
for(i=0; i<len; i++){
printf("%d ", (p+i) );}
printf("\n");
return 0;
}
数组在内存中只是数组元素的简单排列,没有开始和结束标志,在求数组的长度时不能使用sizeof(p) / sizeof(int),因为 p 只是一个指向 int 类型的指针,编译器并不知道它指向的到底是一个整数还是一系列整数(数组)。
所以 sizeof(p) 求得的是 p 这个指针变量本身所占用的字节数,而不是整个数组占用的字节数。也就是说,根据数组指针不能逆推出整个数组元素的个数,以及数组从哪里开始、到哪里结束等信息。
// Win32Project1cpp : 定义应用程序的入口点。
//
#include "stdafxh"
#include "Win32Project1h"
#define MAX_LOADSTRING 100
// 全局变量:
HINSTANCE hInst; // 当前实例
TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此放置代码。
MSG msg;
HACCEL hAccelTable;
// 初始化全局字符串
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_WIN32PROJECT1, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32PROJECT1));
// 主消息循环:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msghwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msgwParam;
}
//
// 函数: MyRegisterClass()
//
// 目的: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcexcbSize = sizeof(WNDCLASSEX);
wcexstyle = CS_HREDRAW | CS_VREDRAW;
wcexlpfnWndProc = WndProc;
wcexcbClsExtra = 0;
wcexcbWndExtra = 0;
wcexhInstance = hInstance;
wcexhIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32PROJECT1));
wcexhCursor = LoadCursor(NULL, IDC_ARROW);
wcexhbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcexlpszMenuName = MAKEINTRESOURCE(IDC_WIN32PROJECT1);
wcexlpszClassName = szWindowClass;
wcexhIconSm = LoadIcon(wcexhInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
//
// 函数: InitInstance(HINSTANCE, int)
//
// 目的: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // 将实例句柄存储在全局变量中
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此添加任意绘图代码
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
不同系统 使用的接口函数可能不同
Linux要用Linux接口
windows要用win api
基本思路就是用opendir打开目录
然后循环readdir 直到null
如果要递归,那么对于每个read到的文件夹 都要再调用一次遍历函数。
以上就是关于c语言如何实现一棵二叉树的遍历全部的内容,包括:c语言如何实现一棵二叉树的遍历、求用C语言实现二叉树层次遍历的递归算法,谢谢!!!、急急急!求C语言的数据结构二叉树递归遍历程序!等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)