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
阅读全文
0 0
- 二叉树 c实现
- 二叉树 C实现
- 二叉树 c 实现
- C语言递归实现二叉链表
- 数据结构之---C语言实现二叉树的二叉链表存储表示
- c语言实现二叉树的基本操作--二叉链表存储
- 二叉树(二叉链表实现)
- 二叉树的二叉链表实现
- 二叉树的二叉链表实现
- 二叉链表实现二叉树
- 【二叉查找树】c实现
- 用c实现二叉树
- C语言实现二叉树
- 用c实现二叉树
- 二叉树递归C实现
- C语言实现二叉树
- C语言实现二叉树
- [C++] 实现二叉搜索树
- 最详尽的DrawerLayout用法
- 在windows下conda install lda
- linux面试题目(五)
- IP基础知识 主机地址和网络地址
- mysql查询每天每周每月每年的数据方法
- C++链表实现二叉树
- vs2013+open1.0环境配置
- CSDN博客多少积分可以有排名
- Dubbo(2)——Admin管理平台和Zookeeper注册中心的搭建
- uva 1276 多重背包
- Freescale i.MX6Q Android 4.3.2编译:
- redis集群搭建(centons7环境下)
- iOS支付功能篇:原生WebView调起支付宝客户端支付方案
- crond和crontab的简单概念和应用