You are on page 1of 3

Tính 

số nút lá của cây nhịphân T


int GetLeafNum(TREE T)
{
int n = 0;
if (T == NULL)
return 0;
if ((T->pLeft == NULL) && (T->pRight == NULL))
return 1;
if (T->pLeft)
n = GetLeafNum(T->pLeft);
if (T->pRight)
 n = n + GetLeafNum(T->pRight);
return n;
}

Tính số nút có đúng một cây con


int GetSNodeNum(TREE T)
{
int n;
if (T == NULL)
return 0;
if ((T->pLeft == NULL) && (T->pRight == NULL))
return 0;
if ((T->pLeft) && (T->pRight))
n = 0;
else
n = 1;
n= n + GetSNodeNum(T->pLeft ) + GetSNodeNum(T->pRight);
return n;
}
Tính số nút có đúng hai cây con
int GetDNodeNum(TREE T)
{
int n;
if (T == NULL)
return 0;
if ((T->pLeft == NULL) && (T->pRight == NULL))
return 0;
if ((T->pLeft) && (T->pRight))
n = 1;
else
n = 0;
n = n + GetDNodeNum(T->pLeft) +GetDNodeNum(T->pRight);
return n;
}

K i ể m   t r a   c â y   n h ị p h â n   tì m   k iế m   T c ó p h ả i l à cây cân bằng hoàn toàn


hay không (Độlệchgiữa chiều cao cây con trái vàcây con phảik h ô n g
q u á 1 v à   đ ộ l ệ c h g i ữ a s ố n ú t c ủ a c â y con trái vàcây con
phải không quá1)
int GetNodeNum(TREE T)
{
//h à m   tí n h   s ố n ú t được cài đặt 
if (T == NULL)
return 0;
return 1 + GetNodeNum(T->pLeft) +GetNodeNum(T->pRight);
}
int CheckCompletelyBal(TREET)
{
int hl, hr, nl, nr;
if (T == NULL)
return 1;
if (!CheckCompletelyBal(T->pLeft))
return 0;
if (!CheckCompletelyBal(T->pRight))
return 0;
hl = GetHeight(T->pLeft);
hr = GetHeight(T->pRight);
nl = GetNodeNum(T->pLeft);
nr = GetNodeNum(T->pRight);
if (((hl == hr + 1) || (hr == hl + 1)) &&((nl == nr + 1) || (nr == nl + 1)))
return 1;
else
return 0;
}
Tính chiều cao cây nhịphân T
int GetHeight(TREE T)
{
int hl, hr;
if (T == NULL)
return 0;
hl = GetHeight(T->pLeft);
hr = GetHeight(T->pRight);
if (hl < hr)
return hr + 1;
else
return hl + 1;
}

You might also like