數(shù)據(jù)結(jié)構(gòu)拓展習(xí)題:最大二叉樹(shù)
題目:?給定一個(gè)非空且無(wú)重復(fù)元素的整數(shù)數(shù)組A,它對(duì)應(yīng)的“最大二叉樹(shù)”T (A)定義為:
① T(A)的根為A中最大值元素;
② T(A)左子樹(shù)為A中最大值左側(cè)部分對(duì)應(yīng)的“最大二叉樹(shù)”;
③ T(A)右子樹(shù)為A中最大值右側(cè)部分對(duì)應(yīng)的“最大二叉樹(shù)”。
例如:A={3, 2, 1, 6, 0, 5}對(duì)應(yīng)的“最大二叉樹(shù)”T (A)如右圖所示。
設(shè)計(jì)一個(gè)“最大二叉樹(shù)”的構(gòu)建算法,并分析最好情況、最壞情況下的時(shí)間和空間復(fù)雜性。




BiTree CreatBiggestBiTree(int* A, int start, int end)
{
?????? if (start > end)
????????????? return NULL;
?????? int index = start;
?????? int max = A[start];
?????? for (int i = start; i <= end; i++)//查找數(shù)組索引范圍內(nèi)的最大元素
?????? {
????????????? if (A[index] < A[i])
????????????? {
???????????????????? index = i;
???????????????????? max = A[i];
????????????? }
?????? }
?????? BiTree T = (BiTNode*)malloc(sizeof(BiTNode));
?????? if (!T)
????????????? exit(OVERFLOW);
?????? T->data = max;
?????? T->lchild = CreatBiggestBiTree(A, start, index - 1);
?????? T->rchild = CreatBiggestBiTree(A, index + 1, end);
?????? return T;
}