C++链表实现二叉树

来源:互联网 发布:义隆单片机c语言教程 编辑:程序博客网 时间:2024/05/17 23:06

我们分别在node.h和tree.h中定义了树中的节点类和树类,同时在node.cpp和tree.cpp中实现了在头文件中申明的函数,然后在demo.cpp中创建一棵树,看看效果。
PS:注意searchIndex(pos)函数,深刻理解其内在递归性,要不超容易犯错,博主初学的时候就是没有仔细理解其递归性,导致遍历一直出错,花了好长时间才找到问题出在哪里,非常耗时。

node.h

#ifndef NODE_H#define NODE_Hclass Node{public:    Node();    int pos;    int data;    Node* p_lchild;    Node* p_rchild;    Node* p_parent;    bool addNode(int pos, int direction, Node* pNode);    void delNode();    void preOrderTraverse();    void inOrderTraverse();    void postOrderTraverse();    Node* searchIndex(int pos);};#endif

tree.h

#ifndef TREE_H#define TREE_H#include "node.h"class Tree{public:    Tree();    ~Tree();    Node* searchIndex(int index);    bool addNode(int pos,int direction,Node* pNode);    void delNode(int pos, Node* pNode);    void preOrderTraverse();    void inOrderTraverse();    void postOrderTraverse();private:    Node* m_proot;};#endif

node.cpp

#include "node.h"#include <iostream>using namespace std;Node::Node(){    this->pos = 0;    this->data = 0;    this->p_lchild = NULL;    this->p_rchild = NULL;    this->p_parent = NULL;}Node* Node::searchIndex(int pos){    if (this->pos==pos){        cout << "搜索到了:" << this->pos << endl;        return this;    }    Node* temp = NULL;    if (this->p_lchild != NULL){        if (this->p_lchild->pos == pos){            cout << "搜索到了:" << this->p_lchild->pos << endl;            return this->p_lchild;        }        else{            temp=this->p_lchild->searchIndex(pos);            if (temp != NULL){                return temp;            }        }    }    if (this->p_rchild != NULL){        if (this->p_rchild->pos == pos){            cout << "搜索到了:" << this->p_rchild->pos<<endl;            return this->p_rchild;        }        else{            return this->p_rchild->searchIndex(pos);        }    }    return NULL;}bool Node::addNode(int pos, int direction, Node* pNode){    Node *p_parent = searchIndex(pos);    if (p_parent == NULL){        return false;    }    Node *cur = new Node();    if (cur == NULL){        return false;    }    cur->pos = pNode->pos;    cur->data = pNode->data;    if (direction == 0){        if (p_parent->p_lchild != NULL){            return false;        }        p_parent->p_lchild = cur;        cur->p_parent = p_parent;        cout << p_parent->pos << "的左孩子是:" << cur->pos << endl;    }     if (direction == 1){        if (p_parent->p_rchild != NULL){            return false;        }        p_parent->p_rchild = cur;        cur->p_parent = p_parent;        cout << p_parent->pos << "的右孩子是:" << cur->pos << endl;    }    return true;}void Node::delNode(){    if (this->p_lchild != NULL){        this->p_lchild->delNode();    }    if (this->p_rchild != NULL){        this->p_rchild->delNode();    }    if (this->p_parent != NULL){        if (this->p_parent->p_lchild == this){            this->p_parent->p_lchild = NULL;        }        if (this->p_parent->p_rchild == this){            this->p_parent->p_rchild = NULL;        }    }}void Node::preOrderTraverse(){    cout << this->pos << ":" << this->data << endl;    if (this->p_lchild != NULL){        this->p_lchild->preOrderTraverse();    }    if (this->p_rchild != NULL){        this->p_rchild->preOrderTraverse();    }}void Node::inOrderTraverse(){    if (this->p_lchild != NULL){        this->p_lchild->inOrderTraverse();    }    cout << this->pos << ":" << this->data << endl;    if (this->p_rchild != NULL){        this->p_rchild->inOrderTraverse();    }}void Node::postOrderTraverse(){    if (this->p_lchild != NULL){        this->p_lchild->postOrderTraverse();    }    if (this->p_rchild != NULL){        this->p_rchild->postOrderTraverse();    }    cout << this->pos << ":" << this->data << endl;}

tree.cpp

#include "tree.h";#include <iostream>using namespace std;Tree::Tree(){    m_proot = new Node();}Tree::~Tree(){    delNode(0, NULL);}Node* Tree::searchIndex(int index){    return m_proot->searchIndex(index);}bool Tree::addNode(int pos, int direction, Node* pNode){    return m_proot->addNode(pos, direction, pNode);}void Tree::delNode(int pos, Node* pNode){    Node* cur = searchIndex(pos);    cur->delNode();}void Tree::preOrderTraverse(){    m_proot->preOrderTraverse();}void Tree::inOrderTraverse(){    m_proot->inOrderTraverse();}void Tree::postOrderTraverse(){    m_proot->postOrderTraverse();}

demo.cpp

#include <iostream>#include "tree.h"using namespace std;int main(){    Node* node1 = new Node();    node1->pos = 1;    node1->data = 5;    Node* node2 = new Node();    node2->pos = 2;    node2->data = 8;    Node* node3 = new Node();    node3->pos = 3;    node3->data = 2;    Node* node4 = new Node();    node4->pos = 4;    node4->data = 6;    Node* node5 = new Node();    node5->pos = 5;    node5->data = 9;    Node* node6 = new Node();    node6->pos = 6;    node6->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);    cout << "前序遍历:" << endl;    tree->preOrderTraverse();    cout << "中序遍历:" << endl;    tree->inOrderTraverse();    cout << "后序遍历:" << endl;    tree->postOrderTraverse();    tree->delNode(2, NULL);    tree->preOrderTraverse();    return 0;}

PS:其实可以不用编写tree类的,使用node足够实现二叉树的所有操作,大家也可以试试只用node类创建一棵树,加上tree类之后会便利理解逻辑关系,各有利弊。
参考:http://www.imooc.com/video/12138

原创粉丝点击