(12)数据结构-二叉树基本操作

1、简介

二叉树有很多性质,书上都有本文不作赘述,主要介绍代码实现部分而。

2、数据结构

typedef char ElemType;

typedef struct node{
    
    
    ElemType data;
    int layer;
    struct node* lchild;
    struct node* rchild;
}BiNode,*BiTree;

3、函数实现

//先序创建二叉树
BiTree CreateBinTree();
void DLR(BiTree T);
void LDR(BiTree T);
void LRD(BiTree T);
//层序遍历的同时,求出每个节点的层数
void Level(BiTree T);
//修改二叉树中所有data为x的节点至newdata
void search(BiTree &T, ElemType x, ElemType newdata);
//交换左右子树
void Exchange(BiTree &T);
//求node节点的高度
int height(BiTree node);
//求node节点的深度
int depth(BiTree T, BiTree node);

3.1 创建二叉树

创建二叉树的方式有很多,本文使用的是先序方式创建。

BiTree CreateBinTree(){
    
    

    BiTree T = new BiNode ;
    ElemType data;
    cin >> data;
	
    if(data == null){
    
    
        return NULL;
    }else{
    
    
        T->data = data;
        T->lchild = CreateBinTree();
        T->rchild = CreateBinTree();
    }
    return T;
}

3.2 三种简单遍历方式

void DLR(BiTree T){
    
    
    if(T){
    
    
        cout << T->data << " ";
        DLR(T->lchild);
        DLR(T->rchild);
    }
}

void LDR(BiTree T){
    
    
    if(T){
    
    
        LDR(T->lchild);
        cout << T->data << " ";
        LDR(T->rchild);
    }
}

void LRD(BiTree T){
    
    
    if(T){
    
    
        LRD(T->lchild);
        LRD(T->rchild);
        cout << T->data << " ";
    }
}

3.3 层序遍历方式

层序遍历使用了队列结构帮助实现。
(1)让root入队
(2)取出队首节点并访问
(3)如果存在左子树则左子树入队
(4)如果存在右子树则右子树入队
(5)如果队列非空回到步骤二

void Level(BiTree T){
    
    
    queue<BiTree> q;
    if(T == NULL){
    
    
        return;
    }else{
    
    
        T->layer = 1;
        q.push(T);
        while(!q.empty()){
    
    
            BiTree temp = q.front();
            q.pop();
            cout << temp->data << " ";
            if(temp->lchild){
    
    
                temp->lchild->layer = temp->layer+1;
                q.push(temp->lchild);
            }
            if(temp->rchild){
    
    
                temp->rchild->layer = temp->layer+1;
                q.push(temp->rchild);
            }
        }
    }
}

3.4 修改二叉树节点

void search(BiTree &T, ElemType x, ElemType newdata){
    
    
    if(T == NULL){
    
    
        return;
    }
    if(T->data == x){
    
    
        T->data = newdata;
    }else{
    
    
        search(T->lchild,x,newdata);
        search(T->rchild,x,newdata);
    }

}

3.5 交换二叉树中左右子树

void Exchange(BiTree &T){
    
    
    if(T == NULL){
    
    
        return;
    }else{
    
    
        BiTree temp = T->lchild;
        T->lchild = T->rchild;
        T->rchild = temp;
        Exchange(T->lchild);
        Exchange(T->rchild);
    }
}

3.6 计算节点高度

int height(BiTree node){
    
    
    int a,b;
    if(node){
    
    
        a = height(node->lchild);
        b = height(node->rchild);
        return (a > b ? a : b)+1;
    }else{
    
    
        return 0;
    }
}

3.7 计算节点深度

int depth(BiTree T, BiTree node){
    
    
    //某一个节点的深度,等于根节点的高度,减去该节点的高度
    int a = height(T);
    int b = height(node);
    return a-b;
}

4、完整代码

#include<iostream>
#include<queue>
using namespace std;
#define null '#'
typedef char ElemType;

typedef struct node{
    
    
    ElemType data;
    int layer;
    struct node* lchild;
    struct node* rchild;
}BiNode,*BiTree;

//先序创建二叉树
BiTree CreateBinTree();
void DLR(BiTree T);
void LDR(BiTree T);
void LRD(BiTree T);
//层序遍历的同时,求出每个节点的层数
void Level(BiTree T);
//修改二叉树中所有data为x的节点至newdata
void search(BiTree &T, ElemType x, ElemType newdata);
//交换左右子树
void Exchange(BiTree &T);
//求node节点的高度
int height(BiTree node);
//求node节点的深度
int depth(BiTree T, BiTree node);

int main(){
    
    

    BiTree T = CreateBinTree();
    Level(T);
    cout << endl  << "Change each F to Z" << endl;
    search(T,'F','Z');
    Level(T);
    cout << endl;

    cout << "Get the height of the tree: "<< height(T) << endl;

    cout << "Get the depth of T->lchild: " << depth(T,T->lchild) << endl;

    cout << "Exchange Left and Right" << endl;
    Exchange(T);
    Level(T);
    cout << endl;

    return 0;
}

BiTree CreateBinTree(){
    
    

    BiTree T = new BiNode ;
    ElemType data;
    cin >> data;

    if(data == null){
    
    
        return NULL;
    }else{
    
    
        T->data = data;
        T->lchild = CreateBinTree();
        T->rchild = CreateBinTree();
    }
    return T;
}

void DLR(BiTree T){
    
    
    if(T){
    
    
        cout << T->data << " ";
        DLR(T->lchild);
        DLR(T->rchild);
    }
}

void LDR(BiTree T){
    
    
    if(T){
    
    
        LDR(T->lchild);
        cout << T->data << " ";
        LDR(T->rchild);
    }
}

void LRD(BiTree T){
    
    
    if(T){
    
    
        LRD(T->lchild);
        LRD(T->rchild);
        cout << T->data << " ";
    }
}

void Level(BiTree T){
    
    
    queue<BiTree> q;
    if(T == NULL){
    
    
        return;
    }else{
    
    
        T->layer = 1;
        q.push(T);
        while(!q.empty()){
    
    
            BiTree temp = q.front();
            q.pop();
            cout << temp->data << " ";
            if(temp->lchild){
    
    
                temp->lchild->layer = temp->layer+1;
                q.push(temp->lchild);
            }
            if(temp->rchild){
    
    
                temp->rchild->layer = temp->layer+1;
                q.push(temp->rchild);
            }
        }
    }
}

void search(BiTree &T, ElemType x, ElemType newdata){
    
    
    if(T == NULL){
    
    
        return;
    }
    if(T->data == x){
    
    
        T->data = newdata;
    }else{
    
    
        search(T->lchild,x,newdata);
        search(T->rchild,x,newdata);
    }

}

int height(BiTree node){
    
    
    int a,b;
    if(node){
    
    
        a = height(node->lchild);
        b = height(node->rchild);
        return (a > b ? a : b)+1;
    }else{
    
    
        return 0;
    }
}

int depth(BiTree T, BiTree node){
    
    
    //某一个节点的深度,等于根节点的高度,减去该节点的高度
    int a = height(T);
    int b = height(node);
    return a-b;
}

void Exchange(BiTree &T){
    
    
    if(T == NULL){
    
    
        return;
    }else{
    
    
        BiTree temp = T->lchild;
        T->lchild = T->rchild;
        T->rchild = temp;
        Exchange(T->lchild);
        Exchange(T->rchild);
    }
}

おすすめ

転載: blog.csdn.net/xdg15294969271/article/details/120483435