【数据结构】回顾二叉树
来源:互联网 发布:软件实施项目管理制度 编辑:程序博客网 时间:2024/05/20 05:22
1.为什么会有树?因为当有大量的输入数据时,链表的线性访问时间就显得略长了。而树结构,其大部分操作的运行时间平均为O(logN)。
2.树的实现并不难,几行代码就搞定了。
struct TreeNode{ Object element; TreeNode *firstChild; TreeNode *nextSibling;}
3.遍历形式:
// 中序遍历二叉树void inorder(tree_pointer ptr){ if(ptr) { inorder(ptr->left_child); printf("%d",ptr->data); inorder(ptr->right_child); }}// 前序遍历二叉树void preorder(tree_pointer ptr){ if(ptr) { printf("%d",ptr->data); preorder(ptr->left_child); preorder(ptr->right_child); }}// 后序遍历二叉树void postorder(tree_pointer ptr){ if(ptr) { postorder(ptr->left_child); postorder(ptr->right_child); printf("%d",ptr->data); }}
4.迭代的中序遍历
void iter_inorder(tree_pointer node){ int top=-1; tree_pointer stack[MAX_STACK_SIZE]; for(;;) { for(;node;node=node->left_child) add(&top,node); node=delete(&top); if(!node) break; printf("%d",node->data); node=node->right_child; }}
5.二叉树的层序遍历
void level_order(tree_pointer ptr){ int front=rear=0; tree_pointer queue[MAX_QUEUE_SIZE]; if(!ptr) return; addq(front,&rear,ptr); for(;;) { ptr=deleteq(&front,rear); if(ptr) { printf("%d",ptr->data); if(ptr->left_child) addq(front,&rear,ptr->left_child); if(ptr->right_child) addq(front,&rear,ptr->right_child); } else break; }}
6.二叉树的复制
tree_pointer copy(tree_pointer original){ tree_pointer temp; if(original) { temp=(tree_pointer) malloc(sizeof(node)); if(IS_FULL(temp)) { fprintf(stderr,"The memory is full\n"); exit(1); } temp->left_child=copy(original->left_child); tmep->right_child=copy(original->right_child); temp->data=original->data; return temp; } return NULL;}
7.判断二叉树的等价性
int equal(tree_pointer first,tree_pointer second){ return ((!first&&!second)||(first && second && (first->data == second->data) && equal(first->left_child,second->left_child) && equal(first->right_child,second->right_child));}
8.寻找结点的中序后继
threaded_pointer insucc(threaded_pointer tree){ threaded_pointer temp; temp=tree->right_child; if(!tree->right_thread) while(!temp->left_thread) temp=temp->left_child; return temp;}
9.线索二叉树的中序遍历
void tinorder(threaded_pointer tree){ threaded_pointer temp=tree; for(;;) { temp=insucc(temp); if(temp==tree) break; printf("%3c",temp->data); }}
10.线索二叉树的右插入操作
void insert_right(threaded_pointer parent,threaded_pointer child){ threaded_pointer temp; child->right_child=parent->right_child; child->right_thread=parent->right_thread; child->left_child=parent; child->left_thread=TRUE; parent->right_child=child; parent->right_thread=FALSE; if(!child->right_thread) { temp=insucc(child); temp->left_child=child; }}
感谢您的访问,希望对您有所帮助。
欢迎大家关注或收藏、评论或点赞。
为使本文得到斧正和提问,转载请注明出处:
http://blog.csdn.net/nomasp
5 0
- 【数据结构】回顾二叉树
- 数据结构快速回顾——二叉树
- 数据结构快速回顾——二叉查找树
- 二叉树回顾
- 数据结构-树的回顾
- 数据结构快速回顾——二叉树 解幂子集问题
- 数据结构回顾和总结(二叉搜索树(BST)的基本操作)
- 数据结构系统回顾与总结(3)(判断是否为同一棵二叉搜索树)
- 数据结构回顾
- 数据结构-树-二叉树
- 数据结构::树,二叉树
- 数据结构-二叉树
- 二叉树的数据结构
- 数据结构-二叉树算法
- java数据结构:二叉树
- 数据结构---二叉树
- 数据结构(C++)--二叉树
- JAVA 数据结构 二叉树
- IP、ICMP、UDP、TCP校验和算法
- [Leetcode 96, Medium] Unique Binary Search Trees
- 并发编程笔记
- android开发之应用Crash自动抓取Log_自动保存崩溃日志到本地
- Android多线程断点续传下载的实现
- 【数据结构】回顾二叉树
- 技术要点
- 07-图5. 旅游规划(25)
- Xcode插件
- Linux下编写简单的动态链接库
- 不用临时的变量 交换两个数的两种方法
- iOS系类教程之用instruments来检验你的app
- 黑马程序员-----基础加强-注解,类加载器
- 5分钟搞定内存字节对齐