【二叉树】二叉树的遍历(先序遍历,中序遍历,后序遍历)和创建(结尾附代码)

二叉树的遍历分为三种:

1.先序遍历:VLR

2.中序遍历:LVR

3.后序遍历:LRV

这三种都是先左(L)后右(R),区别在于根节点(V)的次序。

而层次遍历依次为从上到下,从左到右。

举例:

二叉树的初始化和创建:

总体思路:节点包含数据域和左右两个子树,通过对左右子树递归的方法对二叉树进行延伸,直到叶子结点全为空。

注意:调试时只能用一种创建方式,需要把另一种方式屏蔽掉

方式1:传地址的方式创建

例如,创建上图的:ABC##DE##F##G#H##

方式2:传递返回值的方式创建

 方式3:直接用字符串,避免了重复性的复制粘贴

实现的功能:

 

运行截图:

寻找节点(p)及其父节点(pr):

代码:

头文件common.h

#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

#ifndef _BIN_TREE_H_
#define _BIN_TREE_H_
#define Tree_ElemType char
typedef struct BinTreeNode
{
	Tree_ElemType data;
	struct BinTreeNode* leftChild;
	struct BinTreeNode* rightChild;
}BinTreeNode;

typedef BinTreeNode* BinTree;

void BinTreeInit(BinTree* t);//二叉树的初始化
void BinTreeCreate_1(BinTree *t);//二叉树的创建(第一种方式)
BinTree BinTreeCreate_2();//二叉树的创建(第二种方式,通过返回值创建)
BinTree BinTreeCreate_3(const char *str);//二叉树的创建(第三种,避免了老是复制粘贴)
void PreOrder(BinTree t);//先序遍历
void InOrder(BinTree t);//中序遍历
void PostOrder(BinTree t);//后序遍历
size_t size(BinTree t);//统计树中节点个数
size_t Height(BinTree t);//求树的高度
BinTreeNode* Find(BinTree t, Tree_ElemType key);//寻找节点
BinTreeNode* Parent(BinTree t, BinTreeNode* p);//寻找父节点
size_t LeafSize(BinTree t);//求叶子结点个数
size_t LevelSize(BinTree t, int k);//求第k层的节点个数

void BinTreeInit(BinTree* t)
{
	*t = NULL;
}

//ABC##DE##F##G#H##
void BinTreeCreate_1(BinTree* t)
{
	Tree_ElemType item;
	scanf("%c", &item);

	if (item == '#')
	{
		*t = NULL;
	}
	else
	{
		*t = (BinTreeNode*)malloc(sizeof(BinTreeNode));
		assert(*t != NULL);

		(*t)->data = item;

		BinTreeCreate_1(&((*t)->leftChild));
		BinTreeCreate_1(&((*t)->rightChild));
	}
}

BinTree BinTreeCreate_2()
{
	Tree_ElemType item;
	scanf("%c", &item);

	if (item == '#')
		return NULL;
	
	BinTreeNode* t = (BinTreeNode*)malloc(sizeof(BinTreeNode));
	t->data = item;
	t->leftChild = BinTreeCreate_2();
	t->rightChild = BinTreeCreate_2();
	return t;

}

void PreOrder(BinTree t)
{
	if (t != NULL)
	{
		printf("%c ", t->data);
		PreOrder(t->leftChild);
		PreOrder(t->rightChild);
	}
}

void InOrder(BinTree t)
{
	if (t != NULL)
	{
		PreOrder(t->leftChild);
		printf("%c ", t->data);
		PreOrder(t->rightChild);
	}
}

void PostOrder(BinTree t)
{
	if (t != NULL)
	{
		PreOrder(t->leftChild);
		PreOrder(t->rightChild);
		printf("%c ", t->data);
	}
}

size_t size(BinTree t)
{
	
	if (t != NULL)
	{
		
		return 1+size(t->leftChild)+size(t->rightChild);
	}
	else
	{
		return 0;
	}
	
}


size_t Height(BinTree t)
{
	if (t == NULL)
		return 0;
	if (Height(t->leftChild) > Height(t->rightChild))
		return 1 + Height(t->leftChild);
	else
		return 1 + Height(t->rightChild);
}

BinTreeNode* Find(BinTree t, Tree_ElemType key)
{
	if (t == NULL || t->data == key)
		return t;
	BinTreeNode* tmp = Find(t->leftChild, key);
	if (tmp != NULL)
		return tmp;
	return tmp = Find(t->rightChild, key);
}

BinTreeNode* Parent(BinTree t, BinTreeNode* p)
{
	if (p == t || p == NULL)
		return NULL;
	else if (t->leftChild == p || t->rightChild == p)
		return t;
	BinTreeNode *tmp = Parent(t->leftChild, p);
	if (tmp != NULL)
		return tmp;
	return tmp = Parent(t->rightChild, p);
}

size_t LeafSize(BinTree t)
{
	if (t == NULL)
		return 0;
	if (t->leftChild == NULL && t->rightChild == NULL)
		return 1;

	return LeafSize(t->leftChild) + LeafSize(t->rightChild);
}

size_t LevelSize(BinTree t, int k)
{
	if (t == NULL || k == 0)
		return 0;
	if (k == 1)
		return 1;
	return LevelSize(t->leftChild, (k - 1)) + LevelSize(t->rightChild, (k - 1));
}


BinTree BinTreeCreate_3(const char* str)
{
	static const char* tmp = str;
	if (*tmp == '#' || *tmp=='\0')
		return NULL;

	BinTreeNode* t = (BinTreeNode*)malloc(sizeof(BinTreeNode));
	t->data = *tmp;
	t->leftChild = BinTreeCreate_3(++tmp);
	t->rightChild = BinTreeCreate_3(++tmp);
	return t;
}

#endif

源文件testmain.cpp

#define _CRT_SECURE_NO_WARNINGS
#include"seqlist.h"
#include"slist.h"
#include"dclist.h"
#include"stack.h"

#include "common.h"
//#include <iostream>
//using namespace std;

int main()
{
	const char *str = "ABC##DE##F##G#H##";
	BinTree bt;
	BinTreeInit(&bt);

	//BinTreeCreate_1(&bt);
	//bt = BinTreeCreate_2();
	bt = BinTreeCreate_3(str);

	printf("VLR:");
	PreOrder(bt);
	printf("\n");

	printf("LVR:");
	InOrder(bt);
	printf("\n");

	printf("LRV:");
	PostOrder(bt);
	printf("\n");

	printf("size=%d",size(bt));
	printf("\n");

	printf("height=%d", Height(bt));
	printf("\n");

	BinTreeNode *p=Find(bt, 'D');
	BinTreeNode* pr = Parent(bt, p);
	printf("LeafSize=%d\n", LeafSize(bt));
	printf("LevelSize=%d\n", LevelSize(bt, 4));
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值