樹2-二叉樹複製, 遍歷, 計算葉子結點和高度

HIK4RU44發表於2024-04-18

樹2-二叉樹複製, 遍歷, 計算葉子結點和高度


二叉樹結點

typedef struct BinaryNode{
    char ch;
    struct BinaryNode *lChild;
    struct BinaryNode *rChild;
} BinaryNode;
//葉子結點的數量
int sum;

二叉樹遍歷

前序

//遞迴遍歷(前序)
void Recursion(BinaryNode *root){
    //中左右
    if(root==NULL) return; //root為空return
    cout << root->ch;
    //左子樹
    Recursion(root->lChild);
    //右子樹
    Recursion(root->rChild);
}

中序

//遞迴遍歷(中序)
void Recursion2(BinaryNode *root){
    if(root==NULL) return; //root為空return
    //先遍歷左子樹
    Recursion2(root->lChild);
    //再訪問遍歷根節點
    cout << root->ch;
    //再遍歷右子樹 
    Recursion2(root->rChild);
}

後序

//遞迴遍歷(後序)
void Recursion3(BinaryNode *root){
    if(root==NULL) return; //root為空return
    //先遍歷左子樹
    Recursion3(root->lChild);
    //再遍歷右子樹 
    Recursion3(root->rChild);
    //再訪問遍歷根節點
    cout << root->ch;
}

求葉子節點的數量

//在成員位置定義一個sum
//求葉子節點的數目(沒有子結點)
void CalculateLeafNum(BinaryNode *root){
    if(root==NULL) return;
    if(root->lChild==NULL && root->rChild==NULL){
        sum++;
    };
    //先遍歷左子樹
    CalculateLeafNum(root->lChild);
    //再遍歷右子樹 
    CalculateLeafNum(root->rChild);    
}

二叉樹複製

BinaryNode* CopyBinaryTree(BinaryNode *root){
    if(root==NULL){
        return NULL;
    }
    //複製左子樹
    BinaryNode *lchild = CopyBinaryTree(root->lChild);
    //複製右子樹
    BinaryNode *rchild = CopyBinaryTree(root->rChild);

    //建立結點
    BinaryNode *newNode = (BinaryNode*)malloc(sizeof(BinaryNode));
    newNode->ch = root->ch;
    newNode->lChild = lchild;
    newNode->rChild = rchild;
    return newNode;
}

釋放二叉樹

void FreeSpaceBinaryTree(BinaryNode* root){
    if(root==NULL){
        return;
    }
    //釋放左子樹
    FreeSpaceBinaryTree(root->lChild);
    //釋放右子樹 
    FreeSpaceBinaryTree(root->rChild);
    //釋放當前結點
    free(root);
}

求二叉樹高度

//求二叉樹高度
int CalculateTreeDepth(BinaryNode *root){
    if(root==NULL) return 0;
    int depth = 0;
    //求左子樹高度
    int leftDepth = CalculateTreeDepth(root->lChild);
    //求右子樹高度
    int rightDepth = CalculateTreeDepth(root->rChild);
    //比較高度
    depth = leftDepth>rightDepth ? leftDepth+1 : rightDepth+1;
    return depth;
}


建立二叉樹

二叉樹結構

void CreateBinaryTree(){
    //建立結點
    BinaryNode node1 = {'A',NULL,NULL};
    BinaryNode node2 = {'B',NULL,NULL};
    BinaryNode node3 = {'C',NULL,NULL};
    BinaryNode node4 = {'D',NULL,NULL};
    BinaryNode node5 = {'E',NULL,NULL};
    BinaryNode node6 = {'F',NULL,NULL};
    BinaryNode node7 = {'G',NULL,NULL};
    BinaryNode node8 = {'H',NULL,NULL};
    //建立結點關係
    node1.lChild = &node2;
    node2.rChild = &node3;
    node3.lChild = &node4;
    node3.rChild = &node5;
    node1.rChild = &node6;
    node6.rChild = &node7;
    node7.rChild = &node8;

    //複製二叉樹
    BinaryNode *root = CopyBinaryTree(&node1);
    //前序遍歷
    cout << "前序遍歷: ";
    Recursion(root);
    cout << endl;
    //中序遍歷
    cout << "中序遍歷: ";
    Recursion2(root);
    cout << endl;
    //後序遍歷
    cout << "後序遍歷: ";
    Recursion3(root);
    cout << endl;

    //計算葉子結點數量
    CalculateLeafNum(root);
    //求二叉樹高度
    int depth = CalculateTreeDepth(root);
    cout << endl;
    cout << "樹深度為: " << depth << endl;

    FreeSpaceBinaryTree(root);
}

呼叫

int main(){
    int sum = 0;
    CreateBinaryTree();
    cout << endl;

    system("pause");
    return 0;
}

相關文章