ÔÚ¼ÆËã»ú¿ÆѧÖУ¬¶þ²æÊ÷ÊÇÿ¸ö½áµã×î¶àÓÐÁ½¸ö×ÓÊ÷µÄÓÐÐòÊ÷¡£Í¨³£×ÓÊ÷µÄ¸ù±»³Æ×÷¡°×ó×ÓÊ÷¡±£¨left subtree£©ºÍ¡°ÓÒ×ÓÊ÷¡±£¨right subtree£©¡£¶þ²æÊ÷³£±»ÓÃ×÷¶þ²æ²éÕÒÊ÷ºÍ¶þ²æ¶Ñ»òÊǶþ²æÅÅÐòÊ÷¡£
¶þ²æÊ÷µÄÿ¸ö½áµãÖÁ¶àÖ»Óжþ¿Ã×ÓÊ÷(²»´æÔڶȴóÓÚ2µÄ½áµã)£¬¶þ²æÊ÷µÄ×ÓÊ÷ÓÐ×óÓÒÖ®·Ö£¬´ÎÐò²»Äܵߵ¹¡£¶þ²æÊ÷µÄµÚi²ãÖÁ¶àÓÐ2µÄ i -1´Î·½¸ö½áµã£»Éî¶ÈΪkµÄ¶þ²æÊ÷ÖÁ¶àÓÐ2^(k) -1¸ö½áµã£»¶ÔÈκÎÒ»¿Ã¶þ²æÊ÷T£¬Èç¹ûÆäÖն˽áµãÊý(¼´Ò¶×Ó½áµãÊý)Ϊn0£¬¶ÈΪ2µÄ½áµãÊýΪn2£¬Ôòn0 = n2 + 1¡£
Ê÷ÊÇÓÉÒ»¸ö»ò¶à¸ö½áµã×é³ÉµÄÓÐÏÞ¼¯ºÏ£¬ÆäÖУº
¢±±ØÓÐÒ»¸öÌض¨µÄ³ÆΪ¸ù(ROOT)µÄ½áµã£»
¢²Ê£ÏµĽáµã±»·Ö³Én>=0¸ö»¥²»ÏཻµÄ¼¯ºÏT1¡¢T2¡¢......Tn£¬¶øÇÒ£¬ÕâЩ¼¯ºÏµÄÿһ¸öÓÖ¶¼ÊÇÊ÷¡£Ê÷T1¡¢T2¡¢......Tn±»³Æ×÷¸ùµÄ×ÓÊ÷(Subtree)¡£
Ê÷µÄµÝ¹é¶¨ÒåÈçÏ£º£¨1£©ÖÁÉÙÓÐÒ»¸ö½áµã£¨³ÆΪ¸ù£©£¨2£©ÆäËüÊÇ»¥²»ÏཻµÄ×ÓÊ÷
1.Ê÷µÄ¶È¡ª¡ªÒ²¼´ÊÇ¿í¶È£¬¼òµ¥µØ˵£¬¾ÍÊǽáµãµÄ·ÖÖ§Êý¡£ÒÔ×é³É¸ÃÊ÷¸÷½áµãÖÐ×î´óµÄ¶È×÷Ϊ¸ÃÊ÷µÄ¶È¡£Ê÷ÖжÈΪÁãµÄ½áµã³ÆΪҶ½áµã»òÖն˽áµã¡£Ê÷ÖжȲ»ÎªÁãµÄ½áµã³ÆΪ·ÖÖ¦½áµã»ò·ÇÖն˽áµã¡£³ý¸ù½áµãÍâµÄ·ÖÖ¦½áµãͳ³ÆΪÄÚ²¿½áµã¡£
2.Ê÷µÄÉî¶È¡ª¡ª×é³É¸ÃÊ÷¸÷½áµãµÄ×î´ó²ã´Î¡£
3.»¹ÓÐÊ÷µÄ½Úµã£¬¸ù½Úµã£¬×ó×ÓÊ÷£¬ÓÒ×ÓÊ÷¡£
ÊýµÄ×é³É£ºÓÉÊý¾Ý´æ´¢ ×ó×ÓÊ÷ ÓÒ×ÓÊ÷×é³É¡£
typedef struct Tree
{
data_t data;
struct Tree* ltree;
struct Tree* rtree;
}Tree;
¶ÔÊýµÄ²Ù×÷ÓжÔÊ÷µÄ±éÀú£¬Èç¶ÔÊ÷½øÐÐÏÈÐò±éÀú£¬ÖÐÐò±éÀú£¬ºóÐò±éÀú¡£
#ifndef _TREE_H_ #define _TREE_H_
typedef int data_t; //Ê÷µÄ·â×° typedef struct Tree { data_t data; //´æ´¢µÄÊý¾Ý struct Tree* ltree; //×ó×ÓÊ÷ struct Tree* rtree; //ÓÒ×ÓÊ÷ }Tree;
typedef enum TREE_OP { TREE_ERR=-1, TREE_OK }TREE_OP_ENUM;
//´´½¨Ê÷½Úµã Tree* CreateTreeNode(data_t tdata); //´´½¨Ò»¿ÅÊ÷ Tree* CreateTree(data_t *data,int size); //Ïú»ÙÒ»¿ÅÊ÷ void DestroyTree(Tree* ptree); //ÏÈÐò±éÀú¶þ²æÊ÷ void PreOrder(Tree* ptree); //ÖÐÐò±éÀú¶þ²æÊ÷ void InOrder(Tree* ptree); void PostOrder(Tree* ptree);
#endif /* _TREE_H_ */ |
#include<stdio.h> #include<stdlib.h> #include<string.h> #include"tree.h"
Tree* CreateTreeNode(data_t tdata) { Tree* pNode =(Tree*)malloc(sizeof(Tree)); if ( NULL != pNode ) { memset(pNode,0,sizeof(Tree)); } pNode->data = tdata; return pNode; }
Tree* CreateTree(data_t *data,int size) { if (( NULL == data )||(size <=0)) { printf("parameter err\n"); return NULL; } Tree* proot=CreateTreeNode(data[0]); int i=1; for ( ; i <size; i++ ) { Tree* pNode=CreateTreeNode(data[i]); Tree* prootTmp=proot; while ( 1 ) { if ( pNode->data > prootTmp->data) { if ( NULL == prootTmp->rtree) { prootTmp->rtree = pNode; break; } else { prootTmp= prootTmp->rtree; } } else { if ( NULL == prootTmp->ltree) { prootTmp->ltree=pNode; break; } else { prootTmp=prootTmp->ltree; } } } } return proot; }
void DestroyTree(Tree* ptree) { if ( NULL == ptree ) { return; } DestroyTree(ptree->ltree); DestroyTree(ptree->rtree); free(ptree); ptree=NULL; }
void PreOrder(Tree* ptree) { if ( NULL == ptree ) { return; } printf("%d ",ptree->data); PreOrder(ptree->ltree); PreOrder(ptree->rtree); }
void InOrder(Tree* ptree) { if ( NULL == ptree ) { return; }
InOrder(ptree->ltree); printf("%d ",ptree->data); InOrder(ptree->rtree); } void PostOrder(Tree* ptree) { if ( NULL == ptree ) { return; }
PostOrder(ptree->ltree); PostOrder(ptree->rtree); printf("%d ",ptree->data); } |
#include<stdio.h> #include"tree.h"
int main() { data_t arr[5]={120,2,100,88,93}; Tree* ptree=CreateTree(arr, 5); PreOrder(ptree); printf("\n"); InOrder(ptree); printf("\n"); PostOrder(ptree); printf("\n"); DestroyTree(ptree); ptree = NULL; return 0; } |