二叉树的实现与一些基本操作(C++环境)
#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++环境)的更多相关文章
- 【mybatis】1、入门CURD基本操作(环境搭建)
#1.基本环境 环境 版本 jdk 1.7.0_10 ide eclipse-jee-luna-SR2-win32-x86_64 maven 3.3.3 mybatis 3.2.7 mysql 5.1 ...
- MySql(一)mysql服务的基本操作及环境配置
MySQL服务的启动开始–>计算机–>右键选择管理–>双击打开服务和应用程序–>双击服务–>找到MySQL的服务名称(我的是MySQL56),右键选择启动即可 通过命令行 ...
- Docker环境搭建以及基本操作
Docker环境搭建以及基本操作 Docker环境基本搭建: 基础环境:Centos 7.4 IP:192.168.30.117 [root@docker ~]# cat /etc/re ...
- [LeetCode] Invert Binary Tree 翻转二叉树
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem wa ...
- 基于Java实现红黑树的基本操作
首先,在阅读文章之前,我希望读者对二叉树有一定的了解,因为红黑树的本质就是一颗二叉树.所以本篇博客中不在将二叉树的增删查的基本操作了,需要了解的同学可以到我之前写的一篇关于二叉树基本操作的博客:htt ...
- JAVA实现二叉树(简易版--实现了二叉树的各种遍历)
1,个人感觉二叉树的实现主要还是如何构造一颗二叉树.构造二叉树函数的设计方法多种多样,本例采用 addNode 方法实现.以下程序通过定义内部类来表示二叉树的结点,然后再实现了二叉树这种数据结构的一些 ...
- java数据结构之二叉树的定义和递归实现
定义最多有两棵子树的有序树,称为二叉树.二叉树是一种特殊的树.递归定义:二叉树是n(n>=0)个有限结点构成的集合.N=0称为空二叉树:n>0的二叉树由一个根结点和两互不相交的,分别称为左 ...
- Conda 配置 Python 环境
目录 前言 一.Conda 是什么 二.如何获取 三.使用 Conda 命令配置多环境 1.创建新环境 2.激活新环境 3.配置新环境 4.退出新环境 5.检查所有环境 6.检查所有安装的包 7.删除 ...
- Java提高篇(二七)-----TreeMap
TreeMap的实现是红黑树算法的实现,所以要了解TreeMap就必须对红黑树有一定的了解,其实这篇博文的名字叫做:根据红黑树的算法来分析TreeMap的实现,但是为了与Java提高篇系列博文保持一致 ...
随机推荐
- PHP之PhpDocument的使用
参考资料: 1.http://manual.phpdoc.org/HTMLSmartyConverter/HandS/ric_INSTALL.html 2.http://blog.csdn.net/s ...
- C#WebForm内置对象
内置对象: Response对象:响应请求Response.Write("<script>alert('添加成功!')</script>");Respons ...
- github改local用户名和email
github改local用户名和email 进入cd ~/.ssh 修改git config --global user.name “用户名” config --global user.email 电 ...
- linux php redis 扩展安装
安装redis服务端 1 进入软件的下载路径 cd /soft wget http://download.redis.io/redis-stable.tar.gz tar -zxvf redis-st ...
- POJ 3126 Prime Path
给定两个四位素数a b,要求把a变换到b 变换的过程要保证 每次变换出来的数都是一个 四位素数,而且当前这步的变换所得的素数 与 前一步得到的素数 只能有一个位不同,而且每步得到的素数都不能 ...
- HashMap & HashTable的区别
HashMap & HashTable的区别主要有以下: 1.HashMap是线程不安全的,HashTable是线程安全的.由这点区别可以知道,不考虑线程安全的情况下使用HashMap的效率明 ...
- Python之路,Day6 - 面向对象学习
本节内容: 面向对象编程介绍 为什么要用面向对象进行开发? 面向对象的特性:封装.继承.多态 类.方法. 引子 你现在是一家游戏公司的开发人员,现在需要你开发一款叫做<人狗大战>的游戏 ...
- Topcoder SRM570 900 CurvyonRails
题意:给定一个网格,一些格子是障碍不用管,剩余的格子是城市,你可以修建铁路,铁路的形状可以是直的或者弯的,也就是说可以以这个点为节点连接它四联通的其中两个方块.要求用一个或多个环来覆盖所有城市.对于有 ...
- A 标签的背景
a { -webkit-tap-highlight-color: transparent; -webkit-touch-callout: none; -webkit-user-select: ...
- 【图像处理】【SEED-VPM】6.文件目录结构
———————————————————————————————————————————————————————————————————————— seed-vpm6467 \ Hardware Tes ...