当前位置:首页 » 比特币问答 » btcrate

btcrate

发布时间: 2021-10-17 06:05:23

1. 关于创建二叉树的问题

贴代码要贴完整

2. 二叉树中序遍历问题

你建二叉树的时候没有把左右孩子地址返回给父节点,遍历时找不到左右孩子,当然就崩溃了。而且你的程序在不停地对一个野指针进行操作,这样是很危险的。

create函数改成:


BiTreeCreate()
{
intx;
BiTreebt;
scanf("%d",&x);
if(x==0)bt=NULL;
else
{
bt=(BiTree)malloc(sizeof(BiTNode));
bt->data=x;
bt->LChild=Create();
bt->RChild=Create();
}
returnbt;
}

main函数改成:

intmain()
{
BiTreebt;
bt=Create();
InOrder(bt);
getch();
return0;
}

就可以了。

3. 在C语言中createbt()是什么意思

createbt是一个函数,并且有一个返回值给bt

4. 关于二叉树的建立与输入

输入时按中序序列输入;
可以在最后两句加判断若该节点为空,就不继续往下建树了
但是可能与原来Null重叠,所以直接把#号当作标志符号或者用其他办法吧

5. C语言建立二叉树的问题

createTree里在创建左右子树时的代码改为
L->lchild=CreateTree(L->lchild,str);
L->rchild=CreateTree(L->rchild,str);

另外一些参考建议:
主函数中这句L=(BTNode *)malloc(sizeof(BTNode));不要
CreateTree第一个参数不要,把它变成函数内部的一个变量,该函数调用和声明的地方相应删去第一个参数。

6. 请问C语言如何创建二叉树

创建二叉树的源程序如下:

#include <cstdlib>

#include <stdio.h>

typedef struct node

{ //树的结点

int data;

struct node* left;

struct node* right;

} Node;

typedef struct

{ //树根

Node* root;

} Tree;

void insert(Tree* tree, int value)//创建树

{

Node* node=(Node*)malloc(sizeof(Node));//创建一个节点

node->data = value;

node->left = NULL;

node->right = NULL;

if (tree->root == NULL)//判断树是不是空树

{

tree->root = node;

}

else

{//不是空树

Node* temp = tree->root;//从树根开始

while (temp != NULL)

{

if (value < temp->data)//小于就进左儿子

{

if (temp->left == NULL)

{

temp->left = node;

return;

}

else

{//继续判断

temp = temp->left;

}

}

else {//否则进右儿子

if (temp->right == NULL)

{

temp->right = node;

return;

}

else {//继续判断

temp = temp->right;

}

}

}

}

return;

}

void inorder(Node* node)//树的中序遍历

{

if (node != NULL)

{

inorder(node->left);

printf("%d ",node->data);

inorder(node->right);

}

}

int main()

{

Tree tree;

tree.root = NULL;//创建一个空树

int n;

scanf("%d",&n);

for (int i = 0; i < n; i++)//输入n个数并创建这个树

{

int temp;

scanf("%d",&temp);

insert(&tree, temp);

}

inorder(tree.root);//中序遍历

getchar();

getchar();

return 0;

}


(6)btcrate扩展阅读:

简单二叉树定义范例:此树的顺序结构为:ABCDE

#include <cstdlib>

#include <stdio.h>

#include <string>

int main()

{

node* p = newnode;

node* p = head;

head = p;

string str;

cin >> str;

creat(p, str, 0)//默认根结点在str下标0的位置

return 0;

}

//p为树的根结点(已开辟动态内存),str为二叉树的顺序存储数组ABCD##E或其他顺序存储数组,r当前结点所在顺序存储数组位置

void creat(node* p, string str, int r)

{

p->data = str[r];

if (str[r * 2 + 1] == '#' || r * 2 + 1 > str.size() - 1)p->lch = NULL;

else

{

p->lch = newnode;

creat(p->lch, str, r * 2 + 1);

}

if (str[r * 2 + 2] == '#' || r * 2 + 2 > str.size() - 1)p->rch = NULL;

else

{

p->rch = newnode;

creat(p->rch, str, r * 2 + 2);

}

}

7. c语言二叉树基本操作实现,代码有错误,求指点 [Error] 'createTree' was not declared in this scope

void creatTree(BT &B)//创建二叉树

应该改成:
void createTree(BT &B)//创建二叉树 递归调用时写的是:createTree(B->lchild);函数名差个e

8. 关于二叉树树的建立过程中的参数传递问题

传递地址主要是考虑如下:
防止没有初始化的空指针的传递,虽然传递指针相当于传递地址,但是NULL指针就不起作用了.
比如,你第一次传递指针,它没有初始化,也就没有一个地址对应,你传递了它,其实没有什么用,当然如果你要它有作用也可以,就是返回值为它本身,用它来接收这个返回的指针.

9. 如何实现android蓝牙自动配对连接

之前做一个Android版的蓝牙 与血压计通讯的项目,遇到最大的难题就是自动配对.
上网查资料说是用反射createBond()和setPin(),但测试时进行配对还是会出现提示,但配对是成功了
我就开始查找怎么关闭这个蓝牙配对提示框,后面还是伟大的android源码帮助了我。
在源码 BluetoothDevice 类中还有两个隐藏方法
cancelBondProcess()和cancelPairingUserInput()
这两个方法一个是取消配对进程一个是取消用户输入
下面是自动配对的代码
Mainfest,xml注册

1 <receiver android:name="." >
2 <intent-filter>
3 <action android:name="android.bluetooth.device.action.PAIRING_REQUEST" />
4 </intent-filter>
5 </receiver>
自己在收到广播时处理并将预先输入的密码设置进去

01 public class extends BroadcastReceiver
02 {
03
04 String strPsw = "0";
05
06 @Override
07 public void onReceive(Context context, Intent intent)
08 {
09 // TODO Auto-generated method stub
10 if (intent.getAction().equals(
11 "android.bluetooth.device.action.PAIRING_REQUEST"))
12 {
13 BluetoothDevice btDevice = intent
14 .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
15
16 // byte[] pinBytes = BluetoothDevice.convertPinToBytes("1234");
17 // device.setPin(pinBytes);
18 Log.i("tag11111", "ddd");
19 try
20 {
21 ClsUtils.setPin(btDevice.getClass(), btDevice, strPsw); // 手机和蓝牙采集器配对
22 ClsUtils.createBond(btDevice.getClass(), btDevice);
23 ClsUtils.cancelPairingUserInput(btDevice.getClass(), btDevice);
24 }
25 catch (Exception e)
26 {
27 // TODO Auto-generated catch block
28 e.printStackTrace();
29 }
30 }
31
32
33 }
34 }

001 <b>/************************************ 蓝牙配对函数 * **************/
002 import java.lang.reflect.Field;
003 import java.lang.reflect.Method;
004
005 import android.bluetooth.BluetoothDevice;
006 import android.util.Log;
007 public class ClsUtils
008 {
009
010 /**
011 * 与设备配对 参考源码:platform/packages/apps/Settings.git
012 * /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
013 */
014 static public boolean createBond(Class btClass, BluetoothDevice btDevice)
015 throws Exception
016 {
017 Method createBondMethod = btClass.getMethod("createBond");
018 Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);
019 return returnValue.booleanValue();
020 }
021
022 /**
023 * 与设备解除配对 参考源码:platform/packages/apps/Settings.git
024 * /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
025 */
026 static public boolean removeBond(Class btClass, BluetoothDevice btDevice)
027 throws Exception
028 {
029 Method removeBondMethod = btClass.getMethod("removeBond");
030 Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice);
031 return returnValue.booleanValue();
032 }
033
034 static public boolean setPin(Class btClass, BluetoothDevice btDevice,
035 String str) throws Exception
036 {
037 try
038 {
039 Method removeBondMethod = btClass.getDeclaredMethod("setPin",
040 new Class[]
041 {byte[].class});
042 Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice,
043 new Object[]
044 {str.getBytes()});
045 Log.e("returnValue", "" + returnValue);
046 }
047 catch (SecurityException e)
048 {
049 // throw new RuntimeException(e.getMessage());
050 e.printStackTrace();
051 }
052 catch (IllegalArgumentException e)
053 {
054 // throw new RuntimeException(e.getMessage());
055 e.printStackTrace();
056 }
057 catch (Exception e)
058 {
059 // TODO Auto-generated catch block
060 e.printStackTrace();
061 }
062 return true;
063
064 }
065
066 // 取消用户输入
067 static public boolean cancelPairingUserInput(Class btClass,
068 BluetoothDevice device)
069
070 throws Exception
071 {
072 Method createBondMethod = btClass.getMethod("cancelPairingUserInput");
073 // cancelBondProcess()
074 Boolean returnValue = (Boolean) createBondMethod.invoke(device);
075 return returnValue.booleanValue();
076 }
077
078 // 取消配对
079 static public boolean cancelBondProcess(Class btClass,
080 BluetoothDevice device)
081
082 throws Exception
083 {
084 Method createBondMethod = btClass.getMethod("cancelBondProcess");
085 Boolean returnValue = (Boolean) createBondMethod.invoke(device);
086 return returnValue.booleanValue();
087 }
088
089 /**
090 *
091 * @param clsShow
092 */
093 static public void printAllInform(Class clsShow)
094 {
095 try
096 {
097 // 取得所有方法
098 Method[] hideMethod = clsShow.getMethods();
099 int i = 0;
100 for (; i < hideMethod.length; i++)
101 {
102 Log.e("method name", hideMethod[i].getName() + ";and the i is:"
103 + i);
104 }
105 // 取得所有常量
106 Field[] allFields = clsShow.getFields();
107 for (i = 0; i < allFields.length; i++)
108 {
109 Log.e("Field name", allFields[i].getName());
110 }
111 }
112 catch (SecurityException e)
113 {
114 // throw new RuntimeException(e.getMessage());
115 e.printStackTrace();
116 }
117 catch (IllegalArgumentException e)
118 {
119 // throw new RuntimeException(e.getMessage());
120 e.printStackTrace();
121 }
122 catch (Exception e)
123 {
124 // TODO Auto-generated catch block
125 e.printStackTrace();
126 }
127 }
128 }</b>
执行时直接使用:

view sourceprint?
01 <b>public static boolean pair(String strAddr, String strPsw)
02 {
03 boolean result = false;
04 BluetoothAdapter bluetoothAdapter = BluetoothAdapter
05 .getDefaultAdapter();
06
07 bluetoothAdapter.cancelDiscovery();
08
09 if (!bluetoothAdapter.isEnabled())
10 {
11 bluetoothAdapter.enable();
12 }
13
14 if (!BluetoothAdapter.checkBluetoothAddress(strAddr))
15 { // 检查蓝牙地址是否有效
16
17 Log.d("mylog", "devAdd un effient!");
18 }
19
20 BluetoothDevice device = bluetoothAdapter.getRemoteDevice(strAddr);
21
22 if (device.getBondState() != BluetoothDevice.BOND_BONDED)
23 {
24 try
25 {
26 Log.d("mylog", "NOT BOND_BONDED");
27 ClsUtils.setPin(device.getClass(), device, strPsw); // 手机和蓝牙采集器配对
28 ClsUtils.createBond(device.getClass(), device);
29 remoteDevice = device; // 配对完毕就把这个设备对象传给全局的remoteDevice
30 result = true;
31 }
32 catch (Exception e)
33 {
34 // TODO Auto-generated catch block
35
36 Log.d("mylog", "setPiN failed!");
37 e.printStackTrace();
38 } //
39
40 }
41 else
42 {
43 Log.d("mylog", "HAS BOND_BONDED");
44 try
45 {
46 ClsUtils.createBond(device.getClass(), device);
47 ClsUtils.setPin(device.getClass(), device, strPsw); // 手机和蓝牙采集器配对
48 ClsUtils.createBond(device.getClass(), device);
49 remoteDevice = device; // 如果绑定成功,就直接把这个设备对象传给全局的remoteDevice
50 result = true;
51 }
52 catch (Exception e)
53 {
54 // TODO Auto-generated catch block
55 Log.d("mylog", "setPiN failed!");
56 e.printStackTrace();
57 }
58 }
59 return result;
60 }</b>

10. 为什么我建立的二叉树输出不起个位大佬求解!!!

有两种修改方案:
方案1:函数CreateTree()没有输入参数,但有返回值.
方案2:函数CreateTree()的输入参数是指针的指针,没有返回值.

测试结果:
请输入二叉树的结点值(输入0表示结点指针为空):
1015250030002005000[这是前序扩展序列,也称先序扩展序列]
建立二叉树
251530102050[这是中序遍历序列]

二叉树示意图:

10
/
1520
/\
253050


//方案1:函数CreateTree()没有输入参数,但有返回值
#include<stdio.h>
#include<stdlib.h>
typedefstructnode
{
intdate;
structnode*lchild,*rchild;
}BTNode;

//原代码voidCreateTree(BTNode*T)//型参
BTNode*CreateTree()
{
inta;
BTNode*T;
scanf("%d",&a);
if(a==0)
{
T=NULL;//结点的孩子指针赋予空指针表示结束
}
else
{
T=(BTNode*)malloc(sizeof(BTNode));//建立结点
T->date=a;
//原代码CreateTree(T->lchild);
//原代码CreateTree(T->rchild);
T->lchild=CreateTree();
T->rchild=CreateTree();
}//建立值为a的结点
//原代码return;
returnT;
}

voidInOrder(BTNode*T)//形参
{
if(T!=NULL)//中序递归算法
{
InOrder(T->lchild);
printf("%d",T->date);
InOrder(T->rchild);
}
return;
}

intmain()//原代码voidmain()
{
//BTNode*root;
//原代码root=(BTNode*)malloc(sizeof(BTNode));
BTNode*root=NULL;
printf("请输入二叉树的结点值(输入0表示结点指针为空): ");
//原代码CreateTree(root);
root=CreateTree();
printf("建立二叉树 ");
InOrder(root);

return0;
}

/////////////////////////////////////////////////////

//方案2:函数CreateTree()的输入参数是指针的指针,没有返回值
#include<stdio.h>
#include<stdlib.h>
typedefstructnode
{
intdate;
structnode*lchild,*rchild;
}BTNode;

//voidCreateTree(BTNode*T)//型参
voidCreateTree(BTNode**T)
{
inta;
scanf("%d",&a);
if(a==0)
{
*T=NULL;//结点的孩子指针赋予空指针表示结束
}
else
{
*T=(BTNode*)malloc(sizeof(BTNode));//建立结点
(*T)->date=a;
CreateTree(&((*T)->lchild));
CreateTree(&((*T)->rchild));
}//建立值为a的结点
}

voidInOrder(BTNode*T)//形参
{
if(T!=NULL)//中序递归算法
{
InOrder(T->lchild);
printf("%d",T->date);
InOrder(T->rchild);
}
return;
}

intmain()//原代码voidmain()
{
//原代码BTNode*root;
//原代码root=(BTNode*)malloc(sizeof(BTNode));
BTNode*root=NULL;
printf("请输入二叉树的结点值(输入0表示结点指针为空): ");
//原代码CreateTree(root);
CreateTree(&root);
printf("建立二叉树 ");
InOrder(root);

return0;
}

热点内容
用电脑怎么挖eth 发布:2025-10-08 16:27:10 浏览:778
抢劫买比特币出来成千万富翁 发布:2025-10-08 16:27:05 浏览:114
惠普8300usdt安系统 发布:2025-10-08 15:53:24 浏览:51
doge算法 发布:2025-10-08 14:41:19 浏览:456
水之美是比特币吗 发布:2025-10-08 14:25:07 浏览:861
币圈0撸快讯 发布:2025-10-08 13:25:25 浏览:683
2010年到2020年比特币 发布:2025-10-08 13:05:49 浏览:1000
usdt是哪个国家的虚拟币 发布:2025-10-08 13:05:47 浏览:368
泰坦币今日行情币圈 发布:2025-10-08 12:23:25 浏览:393
btc矿机种类 发布:2025-10-08 11:42:57 浏览:414