① c語言如何列印二叉樹,列印出二叉樹的形狀!!!!
classnode
{
public:
charch;
structnode*l,*r;
node(charc,node*lchild,node*rchild):ch(c),l(lchild),r(rchild){}
};
voidspace(intn)
{
for(inti=0;i<n;i++)
cout<<'';
}
/*以
*右子樹
*根
*左子樹
*的形式列印
*/
voidprint(node*T,intn)
{
if(!T)return;
print(T->r,n+2);
space(n);
cout<<T->ch<<endl;
print(T->l,n+2);
}
intmain()
{
node*T=newnode('A',
newnode('B',NULL,NULL),
newnode('C',
newnode('D',NULL,NULL),
newnode('E',NULL,NULL)));
print(T,0);
}
② c語言繪制二叉樹
你那裡是列印出的啥?不會是沒有存下數據列印了亂碼吧?:)
[修改]
比如,我把和你的二叉樹相關的代碼去掉,改了一下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <graphics.h>
int main()
{
char str[10];
int x = 100, y = 100;
int e = 9;
/* select a driver and mode that supports */
/* multiple drawing colors. */
int gdriver = DETECT, gmode = VGA, errorcode;
detectgraph(&gdriver, &gmode);
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "d:\\bc\\bgi");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
fillellipse(x,y,9,9);
setcolor(WHITE);
circle(x,y,10);
sprintf(str,"%d",e);
outtextxy(x-3,y-2,str);
/* clean up */
getch();
/* colse */
closegraph();
return 0;
}
就能在圈圈裡列印出"9"
③ (C語言)構造一棵二叉樹並顯現出來
#i nclude<stdio.h>
#i nclude<stdlib.h>
typedef struct BiTNode
{
char data;
struct BiTNode *lchild;
struct BiTNode *rchild;
}BiTNode,*BiTree;
int CreateBiTree(BiTree &T)
{
char c;
scanf("%s",&c);
if('*'==c)
{
T=NULL;
return 0;
}
else
{
BiTNode* T=(BiTNode*)malloc(sizeof(BiTNode*));
T->data=c;
T->lchild=NULL;
T->rchild=NULL;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
return 0;
}
void main()
{
BiTNode* T=(BiTNode*)malloc(sizeof(BiTNode*));
CreateBiTree(T);
}
④ C語言。求畫第四題的二叉樹結構圖。謝謝!
⑤ c語言:二叉樹
system("cls");//清屏
printf("input tree node elem(elem-->left-->right):\n");//屏幕上輸出--請輸入樹節點元素,(元素-->左--》右) 如何輸入,要看Pre_Create_BT函數
⑥ C語言二叉樹
就是說根據算符優先順序把運算過程表達出來,這是表達式求值的基礎。
⑦ 二叉樹c語言實現
#include<iostream.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
char data;
struct node *lchild,*rchild;//
}BiTNode,*BiTree;
void CreatBiTree(BiTree &T)
{
char ch;
ch=getchar();
if (ch == ' ')
T = 0;
else {
T=(BiTNode*)malloc(sizeof(BiTNode));
T->data=ch;//生成根節點
CreatBiTree(T->lchild);//構造左子樹
CreatBiTree(T->rchild);//構造右子樹
}
}
void preorder(BiTree T)//前序遍歷
{
if (T!=NULL){
printf ("%c",T->data);
preorder(T->lchild);
preorder(T->rchild);
}
}
void inorder(BiTree T)//中序遍歷
{
if (T!=NULL){
inorder(T->lchild);
printf ("%c",T->data);
inorder(T->rchild);
}
}
void postorder(BiTree T)//後序遍歷
{
if (T!=NULL){
postorder(T->lchild);
postorder(T->rchild);
printf ("%c",T->data);
}
}
void main ()
{
cout<<"請輸入要創建的二叉樹包括空格:"<<endl ;
BiTree T;
CreatBiTree(T);//創建二叉樹
cout<<"前序遍歷的結果為:"<<endl;
preorder(T);
cout<<endl;
cout<<"中序遍歷的結果為:"<<endl;
inorder(T);
cout<<endl;
cout<<"後序遍歷的結果為:"<<endl;
postorder(T);
}
⑧ C語言中關於二叉樹圖形解答
(1)、就圖三而言,根據圖一的二叉樹圖,序號為1 的結點a的孩子有序號為2的結點b和序號為3的結點c,指針就是先指向2後指向3,二叉樹是一般是先寫左孩後寫右孩的。接著看結點a的左孩序號為2的結點b,他的左孩和右孩分別為序號為4的d和序號為5的e,再看結點a的右孩c的孩子序號為6的結點f,,一直這樣看下去,沒有孩子的則忽略。
(2)、圖二的看法與圖三的類似,parent這一列表明父母的序號
⑨ 請問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;
}
(9)c語言二叉樹圖形擴展閱讀:
簡單二叉樹定義範例:此樹的順序結構為: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);
}
}
⑩ C語言 二叉樹
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int e;
struct Node *l, *r;
} Node;
Node *init() //先序遍歷構造二叉樹
{
char n;
Node *p;
scanf("%c", &n);
if (n=='0')
return NULL;
p = (Node*)malloc(sizeof(Node));
if (!p)
exit(0);
p->e = n-'0';
p->l = init();
p->r = init();
return p;
}
void DLR(Node *head) //先序遍歷二叉樹(遞歸演算法)
{
if (head)
{
printf("%d", head->e);
DLR(head->l);
DLR(head->r);
}
}
void destory(Node *head) //銷毀二叉樹
{
Node *l, *r;
if (!head)
return;
l = head->l;
r = head->r;
free(head);
destory(l);
destory(r);
}
int main()
{
Node *head = init();
DLR(head);
destory(head);
return 0;
}