当前位置:首页 » 编程语言 » c语言二叉树图形
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言二叉树图形

发布时间: 2022-06-29 16:18:11

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;

}