二叉树(链表)
来源:互联网 发布:木工画图软件 编辑:程序博客网 时间:2024/06/06 01:50
搜索 删除 前序 后续遍历都在Node 这类中实现 然后 在树中去调用(注意递归函数的使用???)
Node.h
#include<iostream>using namespace std;template<class T>class Node{public: int index; int data; Node *pLChild; Node *pRChild; Node *pParent; Node() { index = 0; data = 0; pLChild = NULL; pRChild = NULL; pParent = NULL; } Node<T> *searchNode(int nodeIndex) { if (this->index == nodeIndex) { return this; } if (this->pLChild != NULL) { if (this->pLChild->index == nodeIndex) { return this->pLChild; } } if (this->pRChild != NULL) { if (this->pRChild->index == nodeIndex) { return this->pRChild; } } return NULL; } void deleteNode() //删除结点 { if (this->pLChild != NULL) { this->pLChild->deleteNode(); } if (this->pRChild != NULL) { this->pRChild->deleteNode(); } if (this->pParent != NULL) { if (this->pParent->pRChild == this) { this->pParent->pRChild = NULL; } if (this->pParent->pLChild == this) { this->pParent->pLChild == NULL; } } delete this; } void preoderTraversal()//前序遍历 根左右 { cout << this->index << " " << this->data; if (this->pLChild != NULL) { this->pLChild->preoderTraversal(); } if (this->pRChild != NULL) { this->pRChild->preoderTraversal(); } } void inoderTraversal()//中序遍历 左根右 { if (this->pLChild != NULL) { this->pLChild->preoderTraversal(); } cout << this->index << " " << this->data; if (this->pRChild != NULL) { this->pRChild->preoderTraversal(); } } void postoderTraversal() //后序遍历左右根 { if (this->pLChild != NULL) { this->pLChild->preoderTraversal(); } if (this->pRChild != NULL) { this->pRChild->preoderTraversal(); } cout << this->index << " " << this->data; }};
阅读全文
0 0
- 二叉树(链表)
- 树:二叉树几种形态(满二叉,完全二叉,线索二叉,二叉排序,平衡二叉,哈夫曼)
- 二叉树、满二叉树、完全二叉树、二叉排序树(二叉查找树)、平衡二叉树
- 二叉树、二叉排序树(二叉查找树)、平衡二叉树
- 二叉树,排序二叉树(续)
- 二叉树5(复制二叉树)
- 二叉树的宽度(二叉树)
- 复制二叉树(二叉树)
- 判满二叉树(二叉树)
- 二叉树(四)删除二叉树
- 二叉查找树(二叉搜索树)
- 二叉树(二叉链表实现)
- 数据结构--二叉树(链表)基本操作
- 二叉树的实现(2)链表
- 数据结构(栈,队列,链表,二叉树)
- 二叉树 => 链表
- 二叉树 链表 + RSA
- day_02_链表、二叉树
- 第六届蓝桥杯决赛 C语言B组 题解 第五题_居民集会
- Cube
- 网络图片浏览器
- 网桥和交换机的区别
- M_CITY
- 二叉树(链表)
- 360天气预报接口
- 前端开发编译器 ps:少年你需要一把称手的兵器
- 机器学习:你需要多少训练数据?
- linux下 mysql5.7没有innodb引擎
- 【笔记】神经网络(一)
- poj 2373 Dividing the Path
- Gym_100971G_Repair
- 数据库SQL优化大总结