btcrate
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;
}