顺序二叉树链表实现<模版>
来源:互联网 发布:python 网络爬虫代码 编辑:程序博客网 时间:2024/06/05 01:12
花了一整天的时间琢磨,算是明白一点指针的使用了
输入样例:
21
20 10 30 5 15 25 35 3 6 11 28 2 7 27 29 8 9 40 33 34 26
这是一个二叉顺序树用链表实现,然后添加与删除根据函数来。
代码:
#include <iostream>#include <stdlib.h>using namespace std;struct tree{int data;tree *left;tree *right;};typedef struct tree *b_tree;b_tree insert(b_tree root,int node){//cout << "循环" << endl;b_tree parentnode;b_tree currentnode;b_tree newnode;newnode=(b_tree)malloc(sizeof(tree));newnode->data=node;newnode->right=NULL;newnode->left=NULL;if(root==NULL)return newnode;else{currentnode=root;while(currentnode!=NULL){parentnode=currentnode;if(currentnode->data<node)currentnode=currentnode->right;elsecurrentnode=currentnode->left;}if(parentnode->data<node)parentnode->right=newnode;elseparentnode->left=newnode;}return root; //此处为root,对应上面的currentnode=root,始终从root开始对比后添加 }b_tree creat(int node[],int len){b_tree root=NULL;for(int i=1;i<=len;i++)root=insert(root,node[i]);return root;}void print(b_tree root){if(root!=NULL){ print(root->left);printf("%d ",root->data);print(root->right);}}int find(b_tree root,int node){if(root==NULL){return 0;}else if(root->data==node){return 1; }else if(root->data<node){find(root->right,node);}else{find(root->left,node);}}b_tree tree_delete(b_tree root,int node){b_tree parentnode;b_tree currentnode;//currentnode=(b_tree)malloc(sizeof(tree));currentnode=root;if(currentnode->data==node){parentnode=root;if(currentnode->right!=NULL&¤tnode->left!=NULL){//cout << "根两边都有" << endl;parentnode=root;currentnode=currentnode->right;while(currentnode->left!=NULL){ //如果不经过这个循环,则parentnode为根节点 parentnode=currentnode;currentnode=currentnode->left; //找到后继点 }if(parentnode==root&¤tnode->right!=NULL){//cout << "右边的左下无" << endl;parentnode->right=currentnode->right;}else if(currentnode->right!=NULL){//cout << "右边的左下有" << endl;parentnode->left=currentnode->right;}root->data=currentnode->data;root->right=NULL;}else if(currentnode->right==NULL||currentnode->left==NULL){ //左右结点均不存在 root=NULL;}else if(currentnode->right!=NULL){root=currentnode->right;}else if(currentnode->left!=NULL){root=currentnode->left;}}else{while(currentnode->data!=node){parentnode=currentnode;if(currentnode->data<node)currentnode=currentnode->right;elsecurrentnode=currentnode->left;}cout << currentnode->data << endl;if(currentnode->right!=NULL&¤tnode->left!=NULL){//cout << "2个" << endl;b_tree currentnode2=currentnode;parentnode=currentnode;currentnode=currentnode->right;while(currentnode->left!=NULL){//cout << "左边存在" << endl;parentnode=currentnode;currentnode=currentnode->left;}if(parentnode!=currentnode2)parentnode->left=NULL;//如果parentnode改变过,那么后继的父节点的左必定没先,除非后面加 if(parentnode==currentnode2&¤tnode2->right!=NULL){//cout << "有节点无左" << endl;parentnode->right=currentnode->right;}else if(currentnode->right!=NULL){//cout << "有结点有左" << endl;parentnode->left=currentnode->right;}currentnode2->data=currentnode->data;}else if(currentnode->right==NULL&¤tnode->left==NULL){if(parentnode->data>node)parentnode->left=NULL;elseparentnode->right=NULL;//cout << "两个都没" << endl;}else if(currentnode->right!=NULL){// cout << "右边在" << endl; parentnode->left=NULL; if(parentnode->data>node)parentnode->left=currentnode->right;elseparentnode->right=currentnode->right;}else if(currentnode->left!=NULL){//cout << "左边在" << endl;if(parentnode->data>node) //如果成立则删除的结点在他的左边 parentnode->left=currentnode->left; elseparentnode->right=currentnode->left;}}return root;}int main(){freopen("in.txt","r",stdin);int n,i;b_tree root;cin >> n;int node[n+1];for(i=1;i<=n;i++)cin >> node[i];root=creat(node,n);print(root);cout << endl;//tree_delete(root,10);//if(find(root,b))//cout << "找到" << endl;//else//cout << "找不到" << endl; return 0;}
0 0
- 顺序二叉树链表实现<模版>
- 模版实现顺序表
- 类模版实现动态顺序表
- 二叉树顺序结构实现
- 二叉树顺序结构实现
- 二叉树的顺序实现
- 【C++数据结构】顺序表的模版类实现
- 【C++】模版类实现普通类静态顺序表
- 二叉树顺序表示的实现
- [数据结构]顺序二叉树的实现
- 【数据结构】二叉树顺序结构实现_BiTreeArray
- 二叉树顺序存储(java实现)
- 二叉树的顺序表示和实现
- 二叉树顺序存储的实现
- 二叉搜索树的顺序实现
- 二叉树模版
- 二叉查找树模版
- 二叉树用顺序表实现 C++代码实现
- linux学习-装机分区
- python 统计excel 字数
- 使用matlab生成简单的模式类
- 富爸爸穷爸爸
- C中volatile关键字
- 顺序二叉树链表实现<模版>
- 知识点:HTML5
- 用shell编程实现三个数的排序
- LeetCode461 Hamming Distance java The Hamming distance between two integers is the number osoluotion
- R语言简介
- Verilog HDL DDS设计(作业3)
- 开源项目源码阅读方法
- visual studio 2015 异常错误:HRSULT:0x8CE0000B
- Eslint 规则说明