#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
using namespace std;
//头文件
#define VALUE int
//定义数据类型
//-----------------------------------------------
typedef struct BITREE{
VALUE value;
int unicode;
struct BITREE *leftson;
struct BITREE *rightson;
}BITREE;
//二叉树的定义
//定义根
BITREE root;
//定义树的高度
long high=0;
//定义叶子数
long leaves=0;
//树的队列模型
VALUE treeline[100000];
//二叉树操作
//----------------------------------------------
//创建左子树,返回左子树地址
BITREE* createleftson(BITREE *father,VALUE value){
BITREE *leftson;
leftson=(BITREE *)malloc(sizeof(BITREE));
leftson->value=value;
father->leftson=leftson;
leftson->leftson=NULL;
leftson->rightson=NULL;
leftson->unicode=father->unicode*2;
treeline[leftson->unicode]=leftson->value;
return leftson;
}
//创建右子树,返回右子树地址
BITREE* createrightson(BITREE *father,VALUE value){
BITREE *rightson;
rightson=(BITREE *)malloc(sizeof(BITREE));
rightson->value=value;
father->rightson=rightson;
rightson->leftson=NULL;
rightson->rightson=NULL;
rightson->unicode=father->unicode*2+1;
treeline[rightson->unicode]=rightson->value;
return rightson;
}
//遍历二叉树(为了插节点)
int traversal_node(BITREE *root){
if(root==NULL)
return 1;
if(root->unicode%2==0){
root->unicode*=2;
treeline[root->unicode]=root->value;
}
else
{
root->unicode*=2;
root->unicode--;
treeline[root->unicode]=root->value;
}
traversal_node(root->leftson);
traversal_node(root->rightson);
}
//插入左节点,返回左结点地址
BITREE* createleftnode(BITREE *father,VALUE value){
BITREE *newnode;
newnode=(BITREE *)malloc(sizeof(BITREE));
newnode->leftson=father->leftson;
newnode->rightson=NULL;
father->leftson=newnode;
newnode->value=value;
newnode->unicode=father->unicode*2;
treeline[newnode->unicode]=newnode->value;
traversal_node(newnode->leftson);
return newnode;
}
//插入右结点,返回右结点地址
BITREE* createrightnode(BITREE *father,VALUE value){
BITREE *newnode;
newnode=(BITREE *)malloc(sizeof(BITREE));
newnode->rightson=father->rightson;
newnode->leftson=NULL;
father->rightson=newnode;
newnode->value=value;
newnode->unicode=father->unicode*2+1;
treeline[newnode->unicode]=newnode->value;
traversal_node(newnode->rightson);
return newnode;
}
//遍历二叉树(先序)
int traversal_first(BITREE *root){
if(root==NULL)
return 1;
//to do
traversal_first(root->leftson);
traversal_first(root->rightson);
}
//遍历二叉树(中序)
int traversal_middle(BITREE *root){
if(root==NULL)
return 1;
traversal_middle(root->leftson);
//to do
traversal_middle(root->rightson);
}
//遍历二叉树(后序)
int traversal_last(BITREE *root){
if(root==NULL)
return 1;
traversal_last(root->leftson);
traversal_last(root->rightson);
//to do
}
//树的高度
int highoftree(BITREE *root){
int l,r;
if(root){
l=highoftree(root->leftson);
r=highoftree(root->rightson);
if(l>r)
return l+1;
else
return r+1;
}
else
return 0;
}
/*
if(root->leftson==NULL&&root->rightson==NULL)
return 1;
if(root->leftson==NULL)
return highoftree(root->rightson)+1;
if(root->rightson==NULL)
return highoftree(root->leftson)+1;
else
return highoftree(root->leftson)>highoftree(root->rightson)?highoftree(root->leftson):highoftree(root->rightson)+1;
*/
//已知unicode探求树的拓扑路径
int* findpath(int unicode){
int *path;
path=(int *)malloc(sizeof(int)*1000);
int k,point=0;
k=unicode;
while(k!=1){
if(k%2==0){
*(path+point)=2;
k/=2;
point++;
}
else{
*(path+point)=1;
k=(k-1)/2;
point++;
}
}
*(path+point)=0;
return path;
}
//注:1代表取左上,2代表取右上
相关文章
- c++学习笔记—二叉树基本操作的实现
- 栈和队列的基本操作(c++实现)
- 实现二叉树各种基本操作的算法
- 码云的远程仓库与本地文件的一些基本操作
- Windows环境安装Hadoop与一些简单的操作测试
- Java 数据结构-特点: 代表一个队列,通常按照先进先出(FIFO)的顺序操作元素。 实现类: LinkedList, PriorityQueue, ArrayDeque。 堆(Heap) 堆(Heap)优先队列的基础,可以实现最大堆和最小堆。 PriorityQueue<Integer minHeap = new PriorityQueue<>; PriorityQueue<Integer maxHeap = new PriorityQueue<>(Collections.reverseOrder); 树(Trees) Java 提供了 TreeNode 类型,可以用于构建二叉树等数据结构。 class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } 图(Graphs) 图的表示通常需要自定义数据结构或使用图库,Java 没有内建的图类。 以上介绍的只是 Java 中一些常见的数据结构,实际上还有很多其他的数据结构和算法可以根据具体问题选择使用。 其他一些说明 以下这些类是传统遗留的,在 Java2 中引入了一种新的框架-集合框架(Collection),我们后面再讨论。 枚举(Enumeration) 枚举(Enumeration)接口虽然它本身不属于数据结构,但它在其他数据结构的范畴里应用很广。 枚举(The Enumeration)接口定义了一种从数据结构中取回连续元素的方式。 例如,枚举定义了一个叫nextElement 的方法,该方法用来得到一个包含多元素的数据结构的下一个元素。 关于枚举接口的更多信息,请参见枚举(Enumeration)。 位集合(BitSet) 位集合类实现了一组可以单独设置和清除的位或标志。 该类在处理一组布尔值的时候非常有用,你只需要给每个值赋值一"位",然后对位进行适当的设置或清除,就可以对布尔值进行操作了。 关于该类的更多信息,请参见位集合(BitSet)。 向量(Vector) 向量(Vector)类和传统数组非常相似,但是Vector的大小能根据需要动态的变化。 和数组一样,Vector对象的元素也能通过索引访问。 使用Vector类最主要的好处就是在创建对象的时候不必给对象指定大小,它的大小会根据需要动态的变化。 关于该类的更多信息,请参见向量(Vector) 栈(Stack) 栈(Stack)实现了一个后进先出(LIFO)的数据结构。 你可以把栈理解为对象的垂直分布的栈,当你添加一个新元素时,就将新元素放在其他元素的顶部。 当你从栈中取元素的时候,就从栈顶取一个元素。换句话说,最后进栈的元素最先被取出。 关于该类的更多信息,请参见栈(Stack)。 字典(Dictionary) 字典(Dictionary) 类是一个抽象类,它定义了键映射到值的数据结构。 当你想要通过特定的键而不是整数索引来访问数据的时候,这时候应该使用 Dictionary。 由于 Dictionary 类是抽象类,所以它只提供了键映射到值的数据结构,而没有提供特定的实现。 关于该类的更多信息,请参见字典( Dictionary)。 Dictionary 类在较新的 Java 版本中已经被弃用(deprecated),推荐使用 Map 接口及其实现类,如 HashMap、TreeMap 等,来代替 Dictionary。
- php与html实现交互的基本操作
- 检查一个二叉树是否平衡的算法分析与C++实现
- 写给java程序员的c++与java实现的一些重要细微差别
- 第4章第1节练习题1 二叉树的基本操作(递归实现)