二叉树的存储结构及其实现


声明:内容及图片基于https://www.bilibili.com/video/BV1Gz411b7Rq?from=articleDetail

二叉树的存储结构

二叉树的顺序存储结构

二叉树的链式存储结构

struct BiNode{ DataType data; BiNode *lchild,*rchild; };

具有n个节点的二叉链表中具有n+1个空指针

三叉链表

二叉树的遍历

前序遍历

DLR(根,左,右)

 

void BiTree::PreOrder(BiNode *bt){ if(bt==NULL)             //递归终止条件 
        return ; else{ visit(bt->data);       //访问根节点bt数据域 
        PreOrder(bt->lchild);  //前序递归遍历bt左子树 
        PreOrder(bt->rchild);  //前序递归遍历bt右子树 
 } }

中序遍历

void BiTree::InOrder(BiNode *bt){ if(bt==NULL)             //递归终止条件 
        return ; else{ InOrder(bt->lchild);  //中序递归遍历bt左子树
        visit(bt->data);       //访问根节点bt数据域 
        InOrder(bt->rchild);  //中序递归遍历bt右子树 
 } }

后序遍历

void BiTree::PostOrder(BiNode *bt){ if(bt==NULL)             //递归终止条件 
        return ; else{ PostOrder(bt->lchild);  //后序递归遍历bt左子树
        PostOrder(bt->rchild);  //后序递归遍历bt右子树 
        visit(bt->data);       //访问根节点bt数据域 
 } } 

层序遍历

void BiTree::LeverOrder(){ if(root==NULL)          //二叉树为空,结束 
        return; Queue.enQueue(root); //根指针入队 
    while(!Queue.isEmpty()){    //队列非空时 
        q=Queue.deQueue();      //出
        visit(q->data); if(q->lchild!=NULL){          //左孩子非空 
            Queue.enQueue(q->lchild); //左孩子入队 
 } if(q->rchild!=NULL){          //右孩子非空 
            Queue.enQueue(q->rchild); //右孩子入队 
 } } } 

二叉树的创建

建立二叉树类

class BiTree{ private: BiNode *root; public: BiTree(){ root=Create(root); //构造函数,建立一颗二叉树 
 } ~BiTree(){ Release(root); //析构函数 
 } void PreOrder(){ PreOrder(root); //前序遍历 
 } void InOrder(){ InOrder(root); //中序遍历 
 } void PostOrder(){ PostOrder(root); //后序遍历 
 } void LeverOrder();   //层序遍历 
    private: BiNode* Create(BiNode *bt); //构造函数调用
        void Release(BiNode *bt);   //析构函数调用
        void PreOrder(BiNode *bt);  //前序遍历函数调用
        void InOrder(BiNode *bt);   //中序遍历函数调用
        void PostOrder(BiNode *bt); //后序遍历函数调用 
};

构造函数方法一(返回指针)

BiTree::BiTree(){ root=Create(root); } BiNode* BiTree::Create(BiNode *bt){ input(ch); //输入节点数据 
    if(ch=='#') bt=NULL;          //建立一颗空树 
    else{ bt=new BiNode; bt->data=ch;               //生成一个节点,数据域为ch 
        bt->lchild=Create(bt->lchild); //前序遍历方式创建二叉树 bt->rchild=Create(bt->rchild); } return bt; }

构造函数方法二(引用)

BiTree::BiTree(){ Create(root); } void BiTree::Create(BiNode * &bt){ input(ch); //输入节点数据 
    if(ch=='#') bt=NULL;          //建立一颗空树 
    else{ bt=new BiNode; bt->data=ch;               //生成一个节点,数据域为ch 
        Create(bt->lchild); //前序遍历方式创建二叉树
        Create(bt->rchild); } return ; }

 二叉树的销毁

void BiTree::release(BiNode* &bt)  // 此处的参数使用引用形式,仅是为了防止出现打印异常。
{ if (bt != NULL) { release(bt->lchild); //后序遍历销毁二叉树 release(bt->rchild); delete bt; bt = NULL; } }

实例

#include<iostream> #include<queue>
using namespace std; struct BiNode{ char data; struct BiNode *lchild,*rchild; }; void Create(BiNode* &root){ char c; cin>>c; if(c=='#') root=NULL; else{ root=new BiNode; root->data=c; Create(root->lchild); Create(root->rchild); } return ; } void PreOrder(BiNode* root){ if(root==NULL) return ; else{ cout<<root->data<<" "; PreOrder(root->lchild); PreOrder(root->rchild); } return ; } void InOrder(BiNode* root){ if(root==NULL) return ; else{ InOrder(root->lchild); cout<<root->data<<" "; InOrder(root->rchild); } return; } void PostOrder(BiNode* root){ if(root==NULL) return ; else{ PostOrder(root->lchild); PostOrder(root->rchild); cout<<root->data<<" "; } } void LeverOrder(BiNode* root){ if(root==NULL) return ; queue<BiNode*> q; q.push(root); while(!q.empty()){ BiNode* tmp=q.front(); cout<<tmp->data<<" "; q.pop(); if(tmp->lchild!=NULL){ q.push(tmp->lchild); } if(tmp->rchild!=NULL){ q.push(tmp->rchild); } } return ; } void deleteBiTree(BiNode* &root){ if(root!=NULL){ deleteBiTree(root->lchild); deleteBiTree(root->rchild); cout<<root->data<<" delete "; delete root; root=NULL; } } int main(){ BiNode* root; Create(root); PreOrder(root); cout<<endl; InOrder(root); cout<<endl; PostOrder(root); cout<<endl; LeverOrder(root); cout<<endl; deleteBiTree(root); }

输入:

AB#D##C##

输出:

A B D C
B D A C
D B C A
A B C D
D delete B delete C delete A delete


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM