1. 程式人生 > >資料結構:樹

資料結構:樹

樹(二叉樹(建立,列印,刪除))

定義:除了根節點之外,每個結點都有一個父節點,除了葉子節點外所有的節點都有一個或者多個子節點。
二叉樹:每個節點最多有兩個葉子節點
遍歷:按照某個順序訪問樹中的所有節點。
 三種常見的遍歷:前序遍歷,中序遍歷,後續遍歷(可以用遞迴和迴圈兩種方式實現)
可實現的題目:二叉樹的深度,樹的子結構,二叉樹的後續遍歷。從上到下遍歷二叉樹(寬度優先遍歷)。
二查搜尋樹:左子結點總是小於等於根節點,而右子節點總是大於等於根節點。找到一個節點的平均時間為O(logn)
二叉樹特例:堆和紅黑二叉樹堆:最大堆和最小堆。可以解決(快速找最大值和最小值問題)
紅黑樹:將樹中的節點定義為紅、黑兩種顏色,通過規則確保從根節點到葉子節點的最長路徑不超過最短路徑的兩倍。在C++ 中的STL(Standard Template libary(標準模板庫),set,multiset,map,multimap等資料結構都是基於紅黑樹實現的。)

重建二叉樹

題目:輸入某個二叉樹的前序遍歷和中序遍歷的結果,重建該二叉樹,假設輸入前序和中序遍歷的結果中沒有重複的數字。例如:輸出得 前序遍歷為{1,2,4,7,3,5,6,8}和中序遍歷{4,7,2,1,5,3,8,6},重建二叉樹。

思路:

1.建立二叉樹,返回值為BinaryTreeNode *,BinaryTreeNode* construct(int* preorder,int* inorder,int length)(如果前序遍歷和中序遍歷的頭指標都不空,及length大於0,則呼叫函式BinaryTreeNode *ConstructCore(int*startPreorder,int*endPreorder,int*startInorder,int*endInorder)(首先將前序遍歷的第一個值賦值給rootValue,初始化根節點,if(startPreorder==endPreorder)
{if(startInorder==endInorder&&*startPreorder==*startInorder) return root;else 拋空異常。) 據中序遍歷找到根節點。定義一個行動指標指p向中序遍歷的頭結點。當滿足p<=endInorder時,找到根節點(root),分別求出左、右子樹的長度leftLength和rightLength,當leftLength和rightLength不為零時,分別建立左右子樹。root->left=呼叫ConstructCore函式,root->right=呼叫ConstructCore函式。
2.列印二叉樹。無返回值。首先建立要列印一個節點的函式void printTreeNode(BinaryTreeNode* pNode)(當pNode不為空時,列印pNode的值,如果pNode->left為空,列印pNode->left->value,右指標同左指標一樣操作。else 拋空。),然後再建立一個函式,返回值為空。void PrintTree(BinaryTreeNode *root){如果根節點不 為空列印根節點的值,左指標不為空,遞迴列印左子樹。右指標同理。}。

 3.刪除二叉樹。無返回值。函式void DeleteNode(BinaryTreeNode* pRoot){如果pRoot不為空,則將左右指標分別賦值給一個新的指標,然後刪除pRoot,並且置空。遞迴刪除左右子樹。}

#include <stdio.h>  
#include <iostream>
using namespace std;
struct BinaryTreeNode
{
	int m_nValue;
	BinaryTreeNode* m_pLeft;
	BinaryTreeNode* m_pRight;

};
BinaryTreeNode* ConstructCore(int* startPreorder,int* endPreorder,int* startInorder,int* endInorder)
{
	int rootValue=startPreorder[0];
	BinaryTreeNode* root=new BinaryTreeNode();
	root->m_nValue=rootValue;
	root->m_pLeft=root->m_pRight=NULL;
	if(startPreorder==endPreorder)
	{
		if(startInorder==endInorder&&*startPreorder==*startInorder)
		{
			return root;
		}
	else
		throw std::exception("Invalid put!"); 
	}
	//通過中序遍歷序列找到根節點
	int* rootInorder=startInorder;
	while(rootInorder<=endInorder&&*rootInorder!=rootValue)
	{
		++rootInorder;
	}
         if(rootInorder==endInorder&&*rootInorder!=rootValue)
	{
		throw std::exception("Invalid put");
	}
	int leftLength=rootInorder-startInorder;
	int rightLength=endInorder-rootInorder;
	int* leftPreorderEnd=startPreorder+leftLength;
	if(leftLength>0)
	{
	    //遞迴構建左子樹
		root->m_pLeft=ConstructCore(startPreorder+1,leftPreorderEnd,startInorder,rootInorder-1);
	}
	if(rightLength>0)
	{
	    //遞迴構建右子樹
		root->m_pRight=ConstructCore(leftPreorderEnd+1,endPreorder,rootInorder+1,endInorder);
	}
	return root;


}
BinaryTreeNode* Construct(int* preorder,int* inorder,int length)
{
	if(preorder==NULL||inorder==NULL||length<=0)
	{
	throw std::exception("Invalid put!");
	}
	return ConstructCore(preorder,preorder+length-1,inorder,inorder+length-1);
}

void PrintTreeNode(BinaryTreeNode* pNode)  
{  
    if(pNode != NULL)  
    {  
        printf("value of this node is: %d\n", pNode->m_nValue);  
  
        if(pNode->m_pLeft != NULL)  
            printf("value of its left child is: %d.\n", pNode->m_pLeft->m_nValue);  
        else  
            printf("left child is null.\n");  
  
        if(pNode->m_pRight != NULL)  
            printf("value of its right child is: %d.\n", pNode->m_pRight->m_nValue);  
        else  
            printf("right child is null.\n");  
    }  
    else  
    {  
        printf("this node is null.\n");  
    }  
  
    printf("\n");  
}  
  //遞迴列印左右子樹
void PrintTree(BinaryTreeNode* pRoot)  
{  
    PrintTreeNode(pRoot);  
  
    if(pRoot != NULL)  
    {  
        if(pRoot->m_pLeft != NULL)  
            PrintTree(pRoot->m_pLeft);  
  
        if(pRoot->m_pRight != NULL)  
            PrintTree(pRoot->m_pRight);  
    }  
}  
  //遞迴刪除左右子樹
void DestroyTree(BinaryTreeNode* pRoot)  
{  
    if(pRoot != NULL)  
    {  
        BinaryTreeNode* pLeft = pRoot->m_pLeft;  
        BinaryTreeNode* pRight = pRoot->m_pRight;  
  
        delete pRoot;  
        pRoot = NULL;  
  
        DestroyTree(pLeft);  
        DestroyTree(pRight);  
    }  
}  
  
void main()  
{  
    const int length = 8;  
    int preorder[length] = {1, 2, 4, 7, 3, 5, 6, 8};
    int inorder[length] = {4, 7, 2, 1, 5, 3, 8, 6};
  
    BinaryTreeNode *root = Construct(preorder, inorder, length);
    PrintTree(root);  
    DestroyTree(root);  
}