二叉树的实现与一些基本操作(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提高篇系列博文保持一致 ...
随机推荐
- 重写TextField Rect 改变显示位置
很简单很常用的一些东西,希望给需要的人帮助. 效果图如下: 自定义textField init() { super.init(frame: CGRect(x: , y: , width: yourWi ...
- js中this的使用
this是Javascript语言的一个关键字. 它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用.比如, function test(){ this.x = 1; } 随着函数使用场合的 ...
- LoadRunner ERROR: java.lang.NumberFormatException
Loadrunner中使用lr_xml_get_values()获取服务端返回的字符串LcsId,LcsId为double,需要将该值转换为 int 后传入下一次请求中. 报错如下:Error is ...
- LCS
/**LCS问题*/ #include <iostream>#include <string>#include <algorithm> using namespac ...
- Remove Duplicates from Sorted Array II [LeetCode]
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- VPS/云主机 如何试用远程连接登录主机服务器_
1.windows主机如何远程登录 点本地电脑开始>运行(或者按"window+R")>输入mstsc点确定 弹出远程连接的框输入IP连接, 如果是VPS,直接输入I ...
- Xcode5 + phoneGap2.9搭建ios开发环境-配置-测试-归档上传/phoneG...
前言: 小弟是做JAVA/Android的第一次搞这个ios,公司有mobile项目是使用phoneGap开发的,需要开发ios版本.什么都不会只能一点一点琢磨了……大神越过…… 原文链接:http: ...
- windows系统查看被占用的端口号
我们需要查看80端口被哪个进程占用了, 1. netstat -ano|findstr 80
- bzoj4213: 贪吃蛇
题意:给定一个网格,有一些格子是障碍不用管,剩余的是空地,你要用一些起点和终点在边界上的路径或环来完全覆盖掉空地,如果使用第一种,会付出1的代价,求最小代价,不能覆盖则输出-1. 现在看到网格而且数据 ...
- 查看cpu
使用系统命令top即可看到如下类似信息: Cpu(s): 0.0%us, 0.5%sy, 0.0%ni, 99.5%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st ...