一个二叉树程序
来源:互联网 发布:华为 移动数据 4g开关 编辑:程序博客网 时间:2024/06/05 12:11
暂时只实现了插入和清空操作,后续其他操作会继续实现
#include<iostream>using namespace std;#include<iomanip>template <class T>class bTree{public: struct node{ T data; node* left; node* right; node(const T data = T()):data(data),left(NULL),right(NULL){} }; typedef node* Node; bTree():tree(NULL),size(0){} ~bTree(); void update(const T& oldData,const T& newData) { if(remove(oldData)) { insert(newData); } } void insert(const T&); void insert(node* &temp,const T&); void insert(node* &temp,node* &temp1) { if(temp==NULL) { temp = new node(); temp = temp1; ++size; }else{ if(temp1->data > (temp->data)) { insert(temp->right,temp1); }else{ insert(temp->left,temp1); } } } //template <class T> bool remove(const T& data) { Node &nodp = find(data); if(nodp==NULL) { return false; } node* temp = nodp; //cout<<temp->data<<endl; temp = nodp; if(nodp->left!=NULL) { //cout<<"Insert"<<endl; insert(nodp->right,nodp->left); } //cout<<"Insert complete!!"<<endl; nodp = nodp->right; delete temp; //cout<<temp->data<<endl; --size; return true; } void show(); void show(node* &temp); void clear(); void clear(node* &temp); int getSize()const { return size;} T getData(const T&); T getData(node* &temp,const T&); //template <class T> Node& find(node* &temp,const T& data) { if(temp!=NULL) { if(temp->data > data) { return find(temp->left,data); } if(temp->data < data){ return find(temp->right,data); } return temp; } } //template <class T> Node& find(const T& data) { return find(tree,data); } void print(node* &temp, int space, char sign){if(temp==NULL) return;print(temp->right,space+3,'/');cout << setw(space+1) << sign << temp->data << endl;print(temp->left,space+3,'\\');}void print(){ print(tree,0,'*');cout<<"---------"<<endl; }private: node* tree; int size;};template <class T>bTree<T>::~bTree(){ clear();}template <class T>void bTree<T>::insert(const T& data){ insert(tree,data);}template <class T>void bTree<T>::insert(node* &temp,const T& data){ if(temp==NULL) { temp = new node(); temp->data = data; ++size; }else{ if(data > (temp->data)) { insert(temp->right,data); }else{ insert(temp->left,data); } }}template <class T>void bTree<T>::show(){ //cout<<"show"<<endl; show(tree);}template <class T>void bTree<T>::show(node* &temp){ if(temp==NULL) { //cout<<"show null"<<endl; return; }else{ show(temp->left); cout<<temp->data<<endl; show(temp->right); }}template <class T>void bTree<T>::clear(){ clear(tree);}template <class T>void bTree<T>::clear(node* &temp){ if(temp!=NULL) { clear(temp->left); delete temp; clear(temp->right); --size; }}template <class T>T bTree<T>::getData(const T& data){ return find(data)->data;}template <class T>T bTree<T>::getData(node* &temp,const T& data){ return find(temp,data)->data;}int main(int argc,char** argv){ bTree<int> test; test.insert(30); test.insert(10); test.insert(15); test.insert(5); test.insert(40); test.insert(45); test.insert(1); test.insert(3); test.insert(6); test.insert(7); test.print(); cout<<"get data="<<test.getData(40)<<endl; test.remove(10); test.print(); test.update(40,55); test.print(); return 0;}
0 0
- 一个二叉树程序
- 一个关于二叉树的小程序
- 一个应用二叉树基本算法的程序
- 一个应用二叉树基本算法的程序 zz
- 编写一个程序,实现二叉树的各种运算
- 数据结构 《4》---- 一个漂亮的打印二叉树的程序
- 一个漂亮的打印二叉树的程序
- 怎样编写一个程序,把一个有序整数数组放到二叉树中
- 怎样编写一个程序,把一个有序整数数组放到二叉树中?
- 怎样编写一个程序,把一个有序整数数组放到二叉树中(按行输出)
- 编写一个程序,把一个有序整数数组放到二叉树中。
- 怎样编写一个程序,把一个有序整数数组放到二叉树中
- 5、怎样编写一个程序,把一个有序整数数组放到二叉树中
- 给定一个数组,写个程序构造一个最矮的二叉树
- 编写一个程序,把一个有序整数数组放到二叉树中
- 怎样编写一个程序,把一个有序整数数组放到二叉树中
- 怎样编写一个程序,把一个有序整数数组放到二叉树中?
- 86 怎样编写一个程序,把一个有序整数数组放到二叉树中
- Eclipse使用Maven创建Web时错误:Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webap
- 64bit mysql安装(win7系统)
- 博客转移到此
- JAVA获取图片大小和尺寸
- 解决Opencv高低版本不兼容问题
- 一个二叉树程序
- 正则表达式说明
- 关于WAMP的Apache服务不能启动的解决方法
- 按键扫描
- 黑子的篮球
- ActionSupport.getText()方法 .
- 2012 蓝桥杯 星期几 【决赛试题】
- PHP session
- 巧解银联8583数据域