数据结构之树的操作

话不多少 q:690217293  欢迎交流

//_____________________________demo.cpp

#include <cstdio>
#include "Tree.h"
#include <iostream>
using namespace std;
int main()
{
    Node *node1 =new Node();
    node1->index=1;
    node1->data=5;
    Node *node2=new Node();
    node2->index=2;
    node2->data=8;
    Node *node3=new Node();
    node3->index=3;
    node3->data=2;
    Node *node4=new Node();
    node4->index=4;
    node4->data=6;
    Node *node5=new Node();
    node5->index=5;
    node5->data=9;
    Node *node6=new Node();
    node5->index=6;
    node5->data=7;
    Tree *tree=new Tree();
    tree->AddNode(0,0,node1);
    tree->AddNode(0,1,node2);
    tree->AddNode(1,0,node3);
    tree->AddNode(1,1,node4);
    tree->AddNode(2,0,node5);
    tree->AddNode(2,1,node6);
    tree->PreorderTraversal();
    delete tree;
}
//=================================Node.h

class Node
{
public :
    Node();
    Node *SearchNode(int nodeIndex);
    void DeleteNode();
    void PreorderTraversal();
    void InorderTraversal();
    void PostorderTraversal();
    int index;//节点下标
    int data;//节点的值
    Node *pLChild;
    Node *pRChild;
    Node *pParent;
};


//______________________________________Node.cpp

#include "Node.h"
#include <iostream>
using namespace std;
Node::Node()
{
    index=0;
    data=0;
    pLChild=0;
    pRChild=0;
    pParent=0;
}
Node * Node::SearchNode(int nodeIndex)
{
    if(this->index==nodeIndex)
    {
        return this;
    }
    Node *temp=0;
    if(this->pLChild!=0)
    {
        if(this->pLChild->index==nodeIndex)
        {
            return this->pLChild;
        }
        else
        {
            temp=this->pLChild->SearchNode(nodeIndex);//更换节点进行递归搜索,this指针指向变换
            if(temp!=0)
            {
                return temp;
            }
        }
    }
    if(this->pRChild!=0)
    {
        if(this->pRChild->index==nodeIndex)
            return this->pRChild;
        else
        {
            temp=this->pRChild->SearchNode(nodeIndex);
                 if(temp!=0)
                     return temp;
        }
    }
    return 0;
}
void Node::DeleteNode()//递归操作,反复调用,删除节点,跟树无关,单纯节点操作,一直到最先面的节点
{
    if(this->pLChild!=0)
    {
        this->pLChild->DeleteNode();
    }
    if(this->pRChild!=0)
    {
        this->pRChild->DeleteNode();
    }
    if(this->pParent!=0)
    {
        if(this->pParent->pLChild==this)
        {
            this->pParent->pLChild=0;
        }
        if(this->pParent->pRChild==this)
        {
            this->pParent->pRChild=0;
        }
    }
    delete this;
}
void Node::PreorderTraversal()//前序遍历
{
    cout<<this->index<<" "<<this ->data<<endl;
    if(this->pLChild!=0)
    {
        this->pLChild->PreorderTraversal();
    }
    if(this->pRChild!=0)
    {
        this->pRChild->PreorderTraversal();
    }
}
void Node::InorderTraversal()//中序遍历
{
    if(this->pLChild!=0)
    {
        this->pLChild->InorderTraversal();
    }
    cout<<this->index<<" "<<this->data<<endl;
    if(this->pRChild!=0)
    {
        this->pRChild->InorderTraversal();
    }
}
void Node::PostorderTraversal()//后续遍历
{
    if(this->pLChild!=0)
    {
        this->pLChild->PostorderTraversal();
    }
    if(this->pRChild!=0)
    {
        this->pRChild->PostorderTraversal();
    }
    cout<<this->index<<" "<<this->data<<endl;
}
//________________________________________Tree.h

#include "Node.h"
class Tree
{
public:
    Tree();
    ~Tree();
    Node *SearchNode(int nodeIndex);
    bool AddNode(int nodeIndex,int direction,Node *pNode);
    bool DeleteNode(int nodeIndex,Node *pNode);
    void PreorderTraversal();
    void InorderTraversal();
    void PostorderTraversal();
private:
    Node * m_pRoot;
};


//=====================================Tree.cpp

#include "Tree.h"
Tree::Tree()
{
    m_pRoot=new Node();
}
Tree::~Tree()
{
    //DeleteNode(0,0);//两种方法
    m_pRoot->DeleteNode();
}
Node *Tree::SearchNode(int nodeIndex)
{
    return m_pRoot->SearchNode(nodeIndex);
}
bool Tree::AddNode(int nodeIndex,int direction,Node *pNode)
{
    Node *temp=SearchNode(nodeIndex);
    if(temp==0)
        return false;
    Node *node=new Node();
    if(node==0)
        return false;
    node->index=pNode->index;
    node->data=pNode->data;
    node->pParent=temp;
    if(direction==0)
    {
        temp->pLChild=node;
    }
    if(direction==1)
    {
        temp->pRChild=node;
    }
    return true;
}
bool Tree::DeleteNode(int nodeIndex,Node *pNode)
{
    Node *temp=SearchNode(nodeIndex);
    if(temp==0)
        return false;
    if(pNode!=0)//删除  顶点的时候
    {
        pNode->data=temp->data;
    }
    temp->DeleteNode();//调用Node里面的删除函数
    return true;
}
void Tree::PreorderTraversal()
{
    m_pRoot->PreorderTraversal();
}
void Tree::InorderTraversal()
{
    m_pRoot->InorderTraversal();
}
void Tree::PostorderTraversal()
{
    m_pRoot->PostorderTraversal();
}



一、实验目的: 理解二叉特别是完全二叉的性质,掌握二叉的存储结构(二叉链表);熟练掌握二叉的常用操作算法(初始化、插入结点、删除结点、遍历等);初步掌握二叉的应用。 二、实验内容: 要求采用二叉链表作为存储结构,完成二叉的建立,前序、中序和后序遍历的操作,求所有叶子及结点总数的操作等。 具体要求如下: ①给出基于二叉链表的二叉类的定义; ②给出二叉初始化(构造函数)的实现; ③给出二叉三种遍历算法的递归实现; ④二叉先序遍历的非递归算法实现; ⑤利用二叉的遍历算法求二叉的结点数、二叉的叶结点数、二叉的高度; ⑥二叉的撤销删除 三、实验步骤: 1、需求分析: 本演示程序用JAVA编写,完成的生成,任意位置的插入、删除,以及遍历二叉中的结点,查找和修改中元素的值。 ① 输入的形式和输入值的范围:插入元素时需要输入插入的位置和元素的值;删除元素时输入删除元素的位置;遍历时采用三种遍历方法中的一种遍历方法;修改操作时需要输入的元素的值;查找操作时,需要找到要查找元素的位置。在所有输入中,元素的值都是整数。 ② 输出的形式:在所有四种操作中都显示操作是否正确以及操作中的内容。其中删除操作后显示删除的元素的值,遍历二叉中的元素,查找操作、修改操作后显示修改的值。 ③ 程序所能达到的功能:完成的生成(通过插入操作)、插入、删除、遍历、查找、修改操作。 ④ 测试数据: A. 中已有以50,25,75,12,37,43,30,33,87,93,97为关键字的结点 B. 插入操作中依次输入10,20,30,40,50,60,70,80,90,100十个数 C. 删除操作中输入10删除值为10的元素 D. 查找操作中输入20,30,40,50返回这个元素在中的位置 2.概要设计: 1)为了实现上述程序功能,需要定义的抽象数据类型: public int iData; public double dData; public Node leftChild; public Node rightChild; private Node root;int value; private Node getSuccessor; 基本操作:{ Tree () 操作结果:构造一个空的二叉 insert () 初始条件:是否存在一个空二叉 操作结果:往二叉中插入数值 delete () 初始条件:存在一非空的二叉 操作条件:将二叉中的元素删除 displayTree () 初始条件:存在一非空的 操作条件:显示非空中的所有元素的值 getString () 初始条件:存在一非空的二叉 操作结果:返回整个字符串的数值 getChar () 初始条件:存在一非空的二叉 操作结果:返回字符型的数值 getInt () 初始条件:存在一非空的二叉 操作结果:返回整型的数值 find () 初始条件:存在一非空二叉 操作结果:从二叉中查找某一元素 traverse () 初始条件:存在一非空的二叉 操作结果:对二叉中的元素进行遍历 preorder () 初始条件:存在一非空的二叉 操作结果:对二叉中的元素进行先根遍历 inOrder () 初始条件:存在一非空的二叉 操作结果:对二叉中的元素进行中根遍历 postOrder () 初始条件:存在一非空的二叉 操作结果:对二叉中的元素进行后根遍历 DisplayNode () 初始条件:存在一非空的二叉 操作结果:显示出二叉中的整形数值和双精度浮点型数值 public static void main 操作结果:调用主函数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值