二叉树学习

一.了解二叉树

1.基本概念

根结点 左子树指针 右子树指针 孩子结点 父结点 兄弟结点 姊妹结点

2.空的二叉树:就结构体指针 tree=NULL

3.只有跟结点的二叉树

4.只有左子树或右子树的二叉树(完全没有另一边的 一条线的 不常用)

5.左右子树都存在

        5.1.完全二叉树(结点连续)

        5.2.满二叉树(左右子树都完整)

e.g:1.二叉树编号一般从1开始

      2.Lchild/2=parent

二.二叉树的遍历

1.

eg:前序  ABDGCEF

中序: DGBAECF   后续:GDBEFCA

#include<string>
#include<iostream>
#include<cstdlib>
using namespace std;

typedef struct treeNode
{
	char data;//数据域字符表示
	struct treeNode* Lchild;
	struct treeNode* Rchild;
}TREE,*LPTREE;//给 treeNode 起了一个 别名 TREE 同时给 她的指针 起一个别名 LPTEEE

//LP开头的一般是指针别名

LPTREE createNode(char data)
{
	LPTREE newNode = (LPTREE)malloc(sizeof(TREE));
	newNode->data = data;
	newNode->Lchild = NULL;
	newNode->Rchild = NULL;
	return newNode;
}
//没有规律的树
void insertNode(LPTREE parentNode, LPTREE Lchild, LPTREE Rchild)
{
	parentNode->Lchild = Lchild;
	parentNode->Rchild = Rchild;
}

//打印当前结点中的元素
void printCurNodeDate(LPTREE curDate)
{
	printf("%c\t", curDate->data);
}
//递归法:领悟
//先序:根 左 右
void preOrder(LPTREE root)
{
	if (root != NULL)
	{
		printCurNodeDate(root);
		preOrder(root->Lchild);
		preOrder(root->Rchild);
	}
}
//中序:左 根 右
void midOrder(LPTREE root)
{
	if (root != NULL)
	{
		midOrder(root->Lchild);
		printCurNodeDate(root);
		midOrder(root->Rchild);
	}
}

//后序:左 右 根
void lastOrder(LPTREE root)
{
	if (root != NULL)
	{
		lastOrder(root->Lchild);
		lastOrder(root->Rchild);
		printCurNodeDate(root);
	}
}




int main()
{
	//死板二叉树创建方法 无实际作用
	LPTREE A = createNode('A');
	LPTREE B = createNode('B');
	LPTREE C = createNode('C');
	LPTREE D = createNode('D');
	LPTREE E = createNode('E');
	LPTREE F = createNode('F');
	LPTREE G = createNode('G');

	insertNode(A, B, C);
	insertNode(B, D, NULL);
	insertNode(D, NULL, G);
	insertNode(C, E, F);
	printf("先序遍历:\n");
	preOrder(A);
	printf("\n中序遍历:\n");
	midOrder(A);
	printf("\n后序遍历:\n");
	lastOrder(A);
	return 0;

}

树 结构体:

二叉树创建函数:

 

先序:

 中序:

 后序:

 主函数测试:

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值