第一次作业(3.数据结构)

1.链表

链表是一种物理存储单元上非连续、非顺序的存储结构数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。 相比于线性表顺序结构,操作复杂。由于不必须按顺序存储,链表在插入的时候可以达到O(1)的复杂度,比另一种线性表顺序表快得多,但是查找一个节点或者访问特定编号的节点则需要O(n)的时间,而线性表和顺序表相应的时间复杂度分别是O(logn)和O(1)。

2.单链表的实现(c++)

/**************linked.h***********************

template <class A>

struct Node

{

    A data;

    Node<A>* next;

}


template <class A>

class LinkList

{

public :

    LinkList();

    LinkList(A a[] ,int i);

    ~LinkList();

    int Length();

    A Get(int i);

    int Lacate(A x);

    void Insert(int i,A x);

    A Delete(int i);

    void PrintList();

private:

    Node<A> * first;

};

/**************************linked.cpp****************************

template<class A>

Void LinkList<A>::PrintList()

{

    p=first->next;

    while(p!=NULL)

    {

        cout<<p->data;

        p=p->next;

    }

}


template <class A>

int LinkList<A>::length()

{

    p=first->next;

    count=0;

    while(p!=NULL)

    {

        p=p->next;

        count++;

    }

    return count;

}


template<class A>

A LinlList <A>::Get(int i)

{

    p=first->next;

    count=1;

    while(p!=NULL&&count<i)

    {

        p=p->next;

        count++;

    }

    if(p==NULL)

        throw "位置";

    else

        return p->data;

}


template<class A>

int LinkList<A>::Locate(A x)

{

    p=first->next;

    count=1;

    while (p!=NULL)

    {

        if (p->data==x)

            return count;

        p=p->next;

        count++;

}

return 0;

}


template <class A>

void ListList< A> ::Insert(int i ,A x)

{

    p=first;

    count =0;

    while (p!=NULL&&count<i-1)

    {

        p=p->next;

        count++;

    }

    if(p==NULL)

        throw "位置";

    else

    {

        s=new Node;

        s->data=x;

        s->next=p->next;

        p->next=s;

    }

}


template <class A>

LinkList<A>::LinkList()

{

    first=new Node;

    first->next=NULL;

}


template <class A>

LinkList<A>::LinkList(A a[],int n)

{

    first=new Node;

    first->next=NULL;

    for(int i=0;i<n;i++)

    {

        s=new Node;

        s->data=a[i];

        s->next=first->next;

        first->next=s;

    }

}


template<class A>

A LinkList<A>::Delete(int i)

{

    p=first;

    count=0;

    while (p!=NULL&&count<i-1)

    {

        p=p->next;

        count++;

    }

    if(p==NULL||p->next==NULL)

        throw"位置";

    else

    {

        q=p->next;

        x=q->data;

        p->next=q->next;

       delete q;

       return  x;

   }

}


template<class A>

LinkList<A>::~LinkList

{

    while (first!=NULL)

    {

        q=first;

        first=first->next;

        delete q;

    }

}

3.树和二叉树

树是由n(n>=1)个有限节点组成一个具有层次关系的集合。它具有以下的特点:每个节点(node)有零个或多个子节点;没有父节点的节点称为根节点;每一个非根节点有且只有一个父节点;除了根节点外,每个子节点可以分为多个不相交的子树;

二叉树的每个结点至多只有二棵子树(不存在度大于2的结点),二叉树的子树有左右之分,次序不能颠倒。

二叉树较重要的应用实例是哈夫曼树和哈夫曼编码。


4.平衡二叉树的实现

二叉排序树又称二叉查找树,他的左子树不空,则左子树上的所有结点的值均小于根结点的值;若他的右子树不空,则右子树上的所有结点的值均大于根结点的值;他的左右子树也都是二叉排序树。


/*****************************BiSortTree.h****************************

class BiSortTree

{

public :

    BiSortTree(int a[],int n);

    ~BiSortTree();

    void InsertBST(BiNode<int >* root,BiNode<int> * s);

    void DeleteBST((BiNode<int >* p,BiNode<int> * f);

    BiNode<int >*SearchBST(BiNode<int > *root,int k);

private:

    BiNode<int >* root;

};

/**************************BiSortTree.cpp********************************

void BiSortTree::InsertBST(BiNode<int> *root,BiNode<int > *s)

{

    if(root==NULL)

        root=s;

    else if(s->data<root->data)

        InsertBST(root->lchild,s);

    else

        InsertBST(root->rchild,s);

}


BiSortTree::BiSortTree(int r[],int n)

{

    for(int i=0;i<n;i++)

    {

        s=new BiNode;

        s->data=r[i];

        s->lchild=s->rchild=NULL;

        InsertBST(root,s);

    }

}


void BiSortTree::DeleteBST(BiNode<int>* p,BiNode<int>* f)

{

    if(p->lchild==NULL)&&(p->rchild==NULL)

    {

        f->lchild=NULL;

        delete p;

    }

    else if(p->rchild==NULL)

   {

        f->lchild=p->lchild;

        delete p;

    }

    else if(p->lchild==NULL)

    {

        f->lchild=p->rchild;

        delete p;

    }

    else

    {

        par=p;

        s=p->rchild;

        while(s->lchild!=NULL)

        {

            par=s;

            s=s->lchild;

        }

        p->data=s->data;

        if(par==p)

             par->rchild=s->rchild;

        else

            par->lchild=s->rchild;

        delete s;

    }

}

 

BiNode<int>*  BiSortTree::SearchtBST(BiNode<int> *root,BiNode<int > *k)

{

    if(root==NULL)

        return NULL;

    else if(root->data==k)

        return root;

    else if (root->data<k)

        return SearchBST(root->lchild,k);

    else

        return SearchBST(root->rchild,k);

}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值