树的存储及遍历方法

本文探讨了树的三种基本表示法:父亲表示法用于快速定位根节点,孩子表示法适用于单链表结构,而父亲孩子表示法则结合了两者。还介绍了先序、后序和层次遍历方法,以及非递归实现。重点在于理解它们的优缺点及应用场景。
摘要由CSDN通过智能技术生成

一、有根树的父亲表示法
除根节点外其他结点有且只有一个父节点,因此,我们可以把每一条边存储在其子结点上。
记录形式为i结点的父亲是j结点
如下图:
在这里插入图片描述
father[a]=-1(表示不存在)
father[b]=a
father[c]=a
father[d]=a
father[e]=c

父亲表示法存储结构代码如下:
int fa[MAXN]
void link(int x,int y)
{
fa[x]=y;
}
也可以用结构体来存储
struct node{
int data,parent
}
node tree[MAXN];
优缺点:利用了树中除根结点外每个结点都有唯一的父节点这个性质,很容易找到树根,但是找孩子需要遍历整个线性表。

二、数型单链表结构,称为“孩子表示法”
每个结点包含一个数据域和一个指针域(指向若干个结点)
typedef struct node
typedef struct *tree;
struct node
{
char data;
tree child[m]//m个孩子
};
tree t;
//把边的信息存储在父结点上
缺陷:只能从父结点遍历到子结点,不能从某个子结点返回到
父结点。
三、树型双链表结构,称为“父亲孩子表示法”
typedef struct node
typedef node *tree;
struct node{
char data;
tree child[m];
tree father;
};

四、二叉树型表示法:“孩子兄弟表示法”
typedef struct node;
typedef node *tree;
struct node{
char data;
tree firstchild,next;
}

树的遍历
先序遍历:先根结点,再从左到右遍历各棵子树
后序遍历:先从左到右遍历各子树,再访问根结点。
层次遍历:按层次从小到大逐个访问,同一层次按从左到右访问。

前两种方法是递归的,通常所说的“深度优先”,最后一种是广度优先

void tral(tree t ,int m)
{
if(t)
{
cout<data<<endl;
for(int i=0;i<m;i++)
tral(t->child[i],m) //m为孩子的个数
}
}
深度优先如下图(图片来自互联网,非本人所画)
在这里插入图片描述


广度优先代码:

const int n=100;
int head,tail,i;
tree q[n];
tree p;
void work()
{
tail=head=1;
q[tail]=t;
tail++;
while(head<tail)
{
p=q[head];
head++;
cout<data<<’ ';
for(int i=0;i<m;i++)
if(p->child[i])
{
q[tail]=p->child[i];
tail++;
}
}
}

完整代码如下:

#include<iostream>
#include<stdlib.h>
#include<stack>
using namespace std;
#define len 15						//定义一个长度

typedef int ElemType;

typedef struct BiTNode
{
	ElemType data;
	struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;

typedef struct Node
{
	BiTree btnode;
	bool isfirst;
}Node,*node;

//向下遍历,找到节点s应该插入的位置,节点有重复时,忽略这个节点
void SearchTreeNode(BiTree &root, BiTree &s)	//注意:使用引用传递
{
	if (root == NULL)
		return;
	if (s->data > root->data)
	{
		if (root->rchild == NULL)
		{
			root->rchild = s;
			return;
		}
		SearchTreeNode(root->rchild, s);//s值大于根节点值,未到达叶子节点,继续向右孩子遍历
	}

	else if (s->data < root->data)
	{
		if (root->lchild == NULL)
		{
			root->lchild = s;
			return;
		}
		SearchTreeNode(root->lchild, s);//s值小于根节点值,未到达叶子节点,继续向左孩子遍历
	}
}

//插入一个节点,树为空,插入节点即为根节点,否则找合适的位置插入
void InsertNode(BiTree &tree, BiTree &s)		//注意:使用引用传递
{
	if (tree == NULL)
		tree = s;
	else
		SearchTreeNode(tree, s);
}

//二叉排序树创建,每次增加一个结点,插到现有的二叉树上去
void CreateOrderBinaryTree(BiTree &tree, int *a)
{
	for (int i = 0; i < len; i++)
	{
		BiTree s = (BiTree)malloc(sizeof(BiTNode));
		s->data = a[i];
		s->lchild = NULL;
		s->rchild = NULL;
		InsertNode(tree, s);
	}
}
//前序遍历
void ProOrderTraverse(BiTree tree)
{
	if (tree == NULL)
		return;
	cout << tree->data << " ";
	ProOrderTraverse(tree->lchild);
	ProOrderTraverse(tree->rchild);
}
//非递归前序遍历
void ProOrder(BiTree pRoot)
{
	if (pRoot == NULL)
		return;
	BiTree p = pRoot;
	stack<BiTree>s;

	while (p != NULL || !s.empty())
	{
		while (p != NULL)
		{
			s.push(p);
			cout << p->data << " ";		//第一次遇见的时候输出
			p = p->lchild;
		}
		if (!s.empty())
		{
			p = s.top();
			s.pop();
			p = p->rchild;
		}
	}
}

//中序遍历
void midOrderTraverse(BiTree tree)
{
	if (tree == NULL)
		return;
	midOrderTraverse(tree->lchild);
	cout << tree->data << " ";
	midOrderTraverse(tree->rchild);
}

//非递归中序遍历
void midOrder(BiTree pRoot)
{
	if (pRoot == NULL)
		return;
	BiTree p = pRoot;
	stack<BiTree>s;
	while (p != NULL || !s.empty())
	{
		while (p!=NULL)
		{
			s.push(p);
			p = p->lchild;
		}
		if (!s.empty())
		{
			p = s.top();
			cout << p->data << " ";		//第二次遇见的时候输出
			s.pop();
			p = p->rchild;
		}
	}
}

//后序遍历
void postOrderTraverse(BiTree pRoot)
{
	if (pRoot == NULL)
		return;
	postOrderTraverse(pRoot->lchild);
	postOrderTraverse(pRoot->rchild);
	cout << pRoot->data<<" ";
}

//非递归实现后续遍历
void postOrder(BiTree pRoot)
{
	if (pRoot == NULL)
		return;
	stack<node>s;
	BiTree p = pRoot;
	node tmp;
	while (p!=NULL || !s.empty())
	{
		while (p != NULL)		//沿左子树一直往下搜索,直至出现没有左子树的结点
		{
			node btn = (node)malloc(sizeof(Node));
			btn->btnode = p;
			btn->isfirst = true;
			s.push(btn);
			p = p->lchild;
		}
		if (!s.empty())
		{
			tmp = s.top();
			s.pop();
			if (tmp->isfirst == true)			//第一次出现在栈顶
			{
				tmp->isfirst = false;
				s.push(tmp);
				p = tmp->btnode->rchild;
			}
			else						//第二次出现在栈顶
			{
				cout << tmp->btnode->data<<" ";
				p = NULL;
			}
		}
	}
}

//非递归实现后续遍历
void postorder(BiTree pRoot)
{
	if (pRoot == NULL)
		return;
	stack<BiTree>s;
	BiTree cur = pRoot, pre = NULL;
	s.push(pRoot);
	while (!s.empty())
	{
		cur = s.top();
		if ((cur->lchild == NULL&&cur->rchild == NULL) ||
			((pre == cur->lchild || pre == cur->rchild) && pre != NULL))
		{
			cout << cur->data << " ";
			s.pop();
			pre = cur;
		}
		else
		{
			if (cur->rchild != NULL)
				s.push(cur->rchild);
			if (cur->lchild != NULL)
				s.push(cur->lchild);
		}
	}
}

int main()
{
	int a[len] = { 62, 88, 58, 47, 35, 73, 51, 99, 37, 93, 23, 27, 45, 21, 12 };

	BiTree tree = NULL;
	//创建一个二叉树,并中序遍历
	CreateOrderBinaryTree(tree, a);

	cout << "前序遍历" << endl;
	ProOrderTraverse(tree);
	cout << endl;
	ProOrder(tree);
	cout << endl<<endl;

	cout << "中序遍历" << endl;
	midOrderTraverse(tree);
	cout << endl;
	midOrder(tree);
	cout << endl<<endl;

	cout << "后序遍历" << endl;
	postOrderTraverse(tree);
	cout << endl;
	postorder(tree);
	cout << endl<<endl;

	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值