樹 二叉樹 多叉樹

本文先介紹了樹的概念,然後給出了二叉樹和多叉樹的實現源碼實例。

一、樹的概念

樹(本質上就使用了遞歸來定義的,遞歸就是堆棧應用,因此樹離不開遞歸和堆棧):樹是n個點的有限結合。n=0時是空樹,n=1時有且僅有一個結點叫做根,n>1,其餘的結點被分成m個互不相交的子集,每一個子集又是一棵樹。


森林

二叉樹

滿二叉樹 深度爲k,結點個數是2的k次方-1的二叉樹。
完全二叉樹 深度爲k,結點爲n,當且僅當每一個結點的編號與對應的滿二叉樹完全一致。

順序存儲:訪問方便
鏈式存儲:刪除方便

二叉排序樹:對每一個結點的值都大於其左子結點,都小於其右子結點。

遍歷:將非線性結構通過線性的方式表訪問。應用於不同需求。
典型的有先生成二叉排序樹,然後中序遍歷,就實現了從小到達的輸出。

前序遍歷:先打印,然後左遞歸,最後右遞歸。
中序遍歷:先左遞歸,然後打印,最後右遞歸。
後序遍歷:先左遞歸,然後右遞歸,最後打印。
層次遍歷:利用隊列。左,右依次放入隊列。先左子出棧打印然後訪問其子,如果存在左右依次放入棧。然後右子出棧打印如果存在子,類推……

 

二、二叉樹

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
//構造二叉樹:左子樹不大於根,右子樹大於根。
int constructTree(int data[],int num,int tree[])
{
	tree[1] = data[1];
	for (int i=2;i<9;i++)
	{
		int j=1;
		while(tree[j]!=0)
		{
			if (data[i]<=tree[j])//下一級2的k次方-1(從1開始)
			{
				j=j*2;
			}
			else
			{
				j=j*2+1;
			}
		}
		tree[j] = data[i];
	}
	for (int i=0;i<20;i++)
	{
		printf("%d ",tree[i]);
	}
	printf("\n");
	return 0;
}
//遍歷二叉樹:遞歸
//相對與根的位置,分爲中序遍歷,前序遍歷,後序遍歷。

//後序遍歷
void ergodic1(int data[],int pos)
{
	//不等於0也就是存在,那麼就要尋找左右
	if (data[pos*2]!=0)//左存在則遍歷
	{
		printf("%d ",data[pos*2]);
		ergodic1(data,pos*2);
	}
	if (data[pos*2+1]!=0)//右存在則遍歷
	{
		printf("%d ",data[pos*2+1]);
		ergodic1(data,pos*2+1);
	}	
}

//中序遍歷
void ergodic2(int data[],int pos)
{
	
	//不等於0也就是存在,那麼就要尋找左右
	if (data[pos*2]!=0)//左存在則遍歷
	{
		ergodic2(data,pos*2);
	}
	if (data[pos]!=0)
	{
		printf("%d ",data[pos]);
	}
	if (data[pos*2+1]!=0)//右存在則遍歷
	{
		ergodic2(data,pos*2+1);
	}
}
//後序遍歷
void ergodic3(int data[],int pos)
{
	//不等於0也就是存在,那麼就要尋找左右
	if (data[pos*2]!=0)//左存在則遍歷
	{
		ergodic3(data,pos*2);
		printf("%d ",data[pos*2]);
	}
	if (data[pos*2+1]!=0)//右存在則遍歷
	{
		ergodic3(data,pos*2+1);
		printf("%d ",data[pos*2+1]);
	}	
}


int main()
{
	int data[100] = {0,6,3,9,2,5,7,8,4,2};
	int num = 9;
	int tree[100] = {0};
	constructTree(data,num,tree);

	ergodic1(tree,0);
	printf("\n");
	ergodic2(tree,0);
	printf("\n");
	ergodic3(tree,0);
	printf("\n");
}


 

三、多叉樹-尋找共同的祖先

//尋找共同的祖先

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct ST_TREE TREE;
struct ST_TREE {
	int deep;
	char name[200];
	int nextNum;
	int next[200];
	TREE *father;
};
void tree_init(TREE *tree)
{
	tree->deep = 0;//頭結點
	memset(tree->name,0,200);
	tree->nextNum = 0;
	memset(tree->next,0,200);
	tree->father = NULL;
}

void tree_insert(TREE *tree0,TREE *tree)
{
	tree->father = tree0;
	tree->deep = tree0->deep+1;
	tree0->next[tree0->nextNum++]=(int)tree;
}
// TREE *tree_getChild(TREE *tree0,int no)
// {
// 	return (TREE *)tree0->next[no];
// }


//單獨設立頭結點
TREE header;

TREE *getChild(TREE *father,char *name)
{
	
	TREE *current = father;
	if (strcmp(name,"")==0)
	{
		return current;
	}
	//特別注意在遞歸的過程中,不能混淆不同層變量的值。此處不管current怎麼變,每一次循環都要來源於原來的current,也就是後來的f
	int num = current->nextNum;
	TREE *f = current;
	for (int i=0;i<num;i++)
	{
		current = (TREE *)f->next[i];
		if (strcmp(name,current->name)==0)
		{
			return current;
		}
		else
		{
			TREE *c = getChild(current,name);
			if (c)
			{
				return c;
			}
		}
	}
	return NULL;
}

void insertPC(char *fatherName,char *sonName)
{
	TREE *f = getChild(&header,fatherName);
	if(f)
	{
		TREE *son = (TREE *)malloc(sizeof(TREE));
		tree_init(son);
		strcat(son->name,sonName);
		tree_insert(f,son);
	}
	else
	{
		insertPC("",fatherName);//插入頭結點
		insertPC(fatherName,sonName);
	}
}

//尋找共同的祖先:比較深度
TREE *getAncestor(char *a,char *b)
{
	TREE *aTree = getChild(&header,a);
	TREE *bTree = getChild(&header,b);
	while(true)
	{
		if (aTree->father==NULL || bTree->father==NULL)
		{
			break;
		}
// 		if (strcmp(aTree->name,"")==0 || strcmp(aTree->name,"")==0)
// 		{
// 			break;
// 		}
		if(aTree->deep>bTree->deep)
		{
			aTree = aTree->father;
		}
		else if(bTree->deep>aTree->deep)
		{
			bTree = bTree->father;
		}
		else
		{
			if (strcmp(aTree->father->name,bTree->father->name)!=0)
			{
				aTree = aTree->father;
				bTree = bTree->father;
			}
			else
			{
				return aTree->father;
			}
		}
	}
	return NULL;
}


int main(void) 
{ 
	tree_init(&header);
	insertPC("a","b");
	insertPC("b","d");
 	insertPC("a","c");
	insertPC("c","e");
	insertPC("e","f");

// 	TREE *t = getChild(&header,"f");
// 	printf("%s",t->name);

	TREE *ancestor = getAncestor("d","f");
	printf("%s",ancestor->name);

	return 0;
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章