c++ 创建二叉树
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <time.h>
struct node
{
long data; //存放数据的一个或者多个项
long count;
struct node *pLeft; //左孩子 指向一个二叉树
struct node *pRight; //右孩子 指向一个二叉树
}; struct node * CreateNode(long value)
{
struct node *p=malloc(sizeof(struct node));
p->pLeft=p->pRight=NULL;
p->data=value;
p->count=1;
}
struct node * AddNode(struct node * pNode,long v)
{
//情况一 pNode==NULL
if (pNode==NULL)
{
return CreateNode(v);
}
// pNode->data=v
if (pNode->data==v)
{
pNode->count++;
return pNode;
}
//v大于结点数据
if (v>pNode->data)
{
if (pNode->pRight==NULL)
{
pNode->pRight=CreateNode(v);
return pNode->pRight;
}else return AddNode(pNode->pRight,v); //递归调用
}
//v小于 结点数据
if (v<pNode->data)
{
if (pNode->pLeft==NULL)
{
pNode->pLeft=CreateNode(v);
return pNode->pLeft;
}else return AddNode(pNode->pLeft,v); //递归调用
} return NULL;
}
int main(void)
{
struct node*root;
long v,i;
printf("请输入二叉树根结点数值:");
scanf("%d",&v);
root=CreateNode(v);
for (i=0;i<=10;i++)
{
AddNode(root,i);
} getchar();
return 0;
}
c++ 创建二叉树的更多相关文章
- 非递归创建二叉树( C++队列 )
非递归按照 层序 创建二叉树,利用 队列(即可先进先出特点)存放已访问的结点元素的地址. 初始化:front=rear= -1: 每储存一个结点元素 rear+1 ,利用 rear%2==0 来使 f ...
- YTU 3025: 创建二叉树
原文链接:https://www.dreamwings.cn/ytu3025/2628.html 3025: 创建二叉树 时间限制: 1 Sec 内存限制: 128 MB 提交: 3 解决: 3 ...
- 创建二叉树,C语言实现
一.前序遍历创建二叉树,使用递归,头文件 BiTree.h /*槽点一:创建树时用scanf输入不成功*/ #ifndef BITREE_H #define BITREE_H #include< ...
- c/c++ 用前序和中序,或者中序和后序,创建二叉树
c/c++ 用前序和中序,或者中序和后序,创建二叉树 用前序和中序创建二叉树 //用没有结束标记的char*, clr为前序,lcr为中序来创建树 //前序的第一个字符一定是root节点,然后去中序字 ...
- c++实现二叉树层序、前序创建二叉树,递归非递归实现二叉树遍历
#include <iostream> #include <cstdio> #include <stdio.h> #include <string> # ...
- java创建二叉树并实现非递归中序遍历二叉树
java创建二叉树并递归遍历二叉树前面已有讲解:http://www.cnblogs.com/lixiaolun/p/4658659.html. 在此基础上添加了非递归中序遍历二叉树: 二叉树类的代码 ...
- 递归------python实现列表创建二叉树
# -*- coding:utf-8 -*- '二叉树结点类' class TreeNode: def __init__(self, x): self.val = x self.left = None ...
- 先序遍历创建二叉树,对二叉树统计叶子节点个数和统计深度(创建二叉树时#代表空树,序列不能有误)c语言
#include "stdio.h" #include "string.h" #include "malloc.h" #define NUL ...
- c使用二叉链表创建二叉树遇到的一些疑问和思考
二叉链表存储二叉树 学习的时候参考的是<大话数据结构>,书中是这样定义的 typedef char TElemType; typedef struct BiTNode { TElemTyp ...
- 用c语言实现前序创建二叉树(递归),分别用前序,中序,后序遍历,以及分别输出节点个数和叶子节点个数
本人c语言小白一枚,近期在学习数据结构(c语言版),特写此随笔,做一些总结和分享,如有不当之处,请各位技术大牛斧正 首先我们用一个结构体来抽象树的结点,代码如下(这里我们存放的数据为char型,大家可 ...
随机推荐
- c# 多线程之-- System.Threading Timer的使用
作用:每隔多久去执行线程里的方法. class ThreadTimerDemo { static void Main(string[] args) { // Create an AutoResetEv ...
- [翻译] RDVTabBarController
RDVTabBarController https://github.com/robbdimitrov/RDVTabBarController 效果: Supports iPad and iPhone ...
- spider-抓取页面内容
# -*- coding: UTF-8 -*- from HTMLParser import HTMLParser import sys,urllib2,string,re,json reload(s ...
- 第八章 计时器(DIGCLOCK)
/*-------------------------------------- DIGCLOCK.C -- Digital Clock (c) Charles Petzold, 1998 ----- ...
- November 21st 2016 Week 48th Monday
A bird is known by its note, and a man by his talk. 闻其声而知鸟,听其言而知人. Listen to what a man talks, watch ...
- DNS Brand
1) You must add glue records (child nameservers) to your-domain.com from your domain's registrar con ...
- Python简单实现多级菜单
# -*- coding: utf-8 -*- # @Time : 2018-06-01 13:40 # @Author : 超人 # @Email : huxiaojiu111@gmail.com ...
- python里的splitlines具体解释
Python的split方法函数能够切割字符串成列表,默认是以空格作为分隔符sep来切割字符串. In [1]: s = "www jeapedu com" In [2]: ...
- numpy的array数据类型(创建)
import numpy as np # 创建 # 创建一维数组 a = np.array([1, 2, 3]) print(a) ''' [1 2 3] ''' # 创建多维数组 b = np.ar ...
- Linux - 版本控制系统SVN
0. 摘要 本文通过搭建SVN多版本库为例,介绍SVN的使用. SVN是一个集中式版本控制系统,在服务端部署中央版本库,所有开发人员客户端连接到中央版本库进行代码的提交和更新. Apache Subv ...