編程代碼樹
『壹』 C語言,判斷樹的編程
一般構樹的題都用最小生成樹演算法,但鑒於你的這題的特殊性,直接深搜,O(n)的時間復雜度就可以過了
#include<cstdio>
#include<cstring>
using namespace std;
bool bo[1100];
int n,m,a[1100][1100];
void search(int x){
for(int i=1;i<=a[x][0];i++){//詢問與x相連的每一個點是否染了色
int y=a[x][i];
if(!bo[y]){//如果y沒有被染色,就把y染色,並用y去更新別的節點
bo[y]=1;
search(y);
}
}
}
int main(){
scanf("%d%d",&n,&m);//這里需要你輸入結點個數和邊數,比如你的那組數據就輸入10 9表示10個點,9條邊
if(m<n-1){printf("NO\n");return 0;}//如果你的邊數比節點數-1還要小的話,那根本不可能聯成一個整體
for(int i=1;i<=m;i++){
int x,y;scanf("%d%d",&x,&y);
a[x][++a[x][0]]=y;//記錄下x這個點可以連到y這個點,下同
a[y][++a[y][0]]=x;
}
memset(bo,0,sizeof(bo));//剛開始時除節點1外沒有一個節點是在這個圖中的,然後從1開始往別的點染色
bo[1]=1;
search(1);
for(int i=1;i<=n;i++)
if(!bo[i]){//如果有點沒有被染色就說明不能連成一棵樹
printf("NO\n");
return 0;
}
printf("YES\n");
return 0;
}
『貳』 求java圖形界面樹類編程源碼舉例。類似windows資源管理器那樣的。如附圖,2層2項即可。
publicvoidinit(){
ContainercontentPane=null;
=newDefaultMutableTreeNode("我的電腦");
1=newDefaultMutableTreeNode("網路");
2=newDefaultMutableTreeNode("硬碟");
1_1=newDefaultMutableTreeNode("無限");
1_2=newDefaultMutableTreeNode("有限");
2_1=newDefaultMutableTreeNode("A");
2_2=newDefaultMutableTreeNode("B");
treeNode.add(treeNode1);
treeNode.add(treeNode2);
treeNode1.add(treeNode1_1);
treeNode1.add(treeNode1_2);
treeNode2.add(treeNode2_1);
treeNode2.add(treeNode2_2);
JTreetree=newJTree(treeNode);
contentPane=getContentPane();
JPaneljp=newJPanel();
jp.add(tree);
contentPane.add(jp);
this.setSize(300,250);
this.setLocation(400,300);
this.setVisible(true);
}
『叄』 編程實現以上二叉樹中序遍歷操作,輸出遍歷序列,求寫代碼~~
#include<stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define OK 1
#define ERROR 0
#define OVERFLOW 0
typedef char TElemType;
typedef int Status;
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
typedef enum {Link,Thread} PointerTag;
typedef struct BiThrNode
{
TElemType data;
struct BiThrNode *lchild,*rchild;
PointerTag LTag,RTag;
}BiThrNode;
typedef BiThrNode *BiThrTree;
BiTree CreateBiTree(BiTree T) //先序遍歷構造二叉樹
{
char ch;
scanf("%c",&ch);
if(ch=='#') //#代表空指針
T=NULL;
else
{
T=(BiTNode *)malloc(sizeof(BiTNode)); //申請結點
if(!T)
exit(OVERFLOW);
T->data=ch; //生成根結點
T->lchild=CreateBiTree(T->lchild); //構造左子樹
T->rchild=CreateBiTree(T->rchild); //構造右子樹
}
return T;
}
Status InOrderTraverse(BiTree T,Status(*visit)(TElemType))
{//中序遍歷二叉樹
if(T)
{
if(InOrderTraverse(T->lchild,visit))
if(visit(T->data))
if(InOrderTraverse(T->rchild,visit))
return OK;
return ERROR;
}
else
return OK;
}
Status PrintElement(TElemType e)
{
printf("%-2c",e);
return OK;
}
void main()
{
BiTree T=NULL;
printf("請輸入構造二叉樹的字元序列:");
T=CreateBiTree(T);
if(T)
printf("二叉樹建立成功!
");
else
printf("二叉樹構造失敗!!!
");
printf("中序遍歷二叉樹:");
InOrderTraverse(T,PrintElement);
printf("
");
}
『肆』 求利用c\c++將字元串集構建成一棵樹的程序代碼
表達式樹是樹結構的一個經典應用。
常見的表達式:3+5*7+2為中綴表達式。
這里,我實現一種轉換演算法,那就是先把中綴式子轉化為一棵樹,然後使用不同的遍歷遍歷方式得到不同的表達式
【注】(僅僅給出沒有括弧時的代碼,為了簡化字元串處理,我沒有使用字元串,而是使用了字元,因此不支持兩位的數字算式)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct string_node {
char tag; //定義字元
struct string_node *left;
struct string_node *right;
struct string_node *parent;
};
struct string_node *gen_sub_tree(char*str, int len)
{
struct string_node *root;
int i = 0;
for (i = 0; i < len; i++) {
struct string_node *node = (struct string_node*)calloc(1, sizeof(structstring_node));
node->tag =str[i];
if(isdigit(str[i])) {
if (i != 0) {//運算由左至右,除了第一個操作數,其它全部為右操作數,高優先順序的會處在樹的旁支
root->right = node;
node->parent = root;
}
root = node;
} else if((str[i] == 'x') || (str[i] == '/')) {
//處理乘除運算符
struct string_node *temp_root = NULL, *temp_parent = NULL;
if (root->parent &&(root->parent->tag == '+' || root->parent->tag == '-')) {
temp_parent = root->parent;
temp_root = root;
} else if(root->parent) {
temp_parent =root->parent->parent;
temp_root = root->parent;
} else {
temp_root = root;
}
node->parent = temp_parent;
if (temp_parent)
temp_parent->right = node;
node->left = temp_root;
temp_root->parent = node;
root = node;
} else if (str[i] == '+' || str[i] == '-') {
//處理加減運算符
struct string_node *temp_root =NULL, *temp_parent = NULL;
if (root->parent &&root->parent->parent &&(root->parent->tag == 'x' || root->parent->tag == '/')) {
temp_root =root->parent->parent;
} else if (root->parent&& !root->parent->parent){
temp_root = root->parent;
} else {
temp_root = root;
}
node->left = temp_root;
temp_root->parent = node;
root = node;
}
}
//找到樹根,返回調用者
while (root->parent) {
root = root->parent;
}
return root;
}
int main(int argc, char **argv)
{
struct string_node *root;
char str[40] ={'1','+','0','+','2','x','5','+','3','x','4','x','2', '-', '7'};
root = NULL;
root = gen_sub_tree(str, 100);
print_result(root);
return 0;
}
//輸出結果
void print_result(struct string_node*p)
{
if(p != NULL) {
//此為後綴表達式,根據printf的位置不同可以得到不同的X綴表達式
print_result(p->left);
print_result(p->right);
printf("%c\n", p->tag);
}
}
【】下面考慮加上括弧時的情況,由於號優先順序最高,而且還不是像運算符那樣結合操作數的字元,因此把括弧括住的成分單獨作為一個操作數比較好,這樣就可以遞歸的實現了。,只需要加入對』(『和』)』的解析即可嘍,遞歸調用gen_sub_tree即可。以下為加入括弧處理的代碼:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct string_node {
char tag; //定義字元
struct string_node *left;
struct string_node *right;
struct string_node *parent;
};
struct string_node *gen_sub_tree(char*str, int len)
{
struct string_node *root;
int i = 0;
for (i = 0; i < len; i++) {
struct string_node *node = (struct string_node*)calloc(1, sizeof(structstring_node));
node->tag =str[i];
if(isdigit(str[i])) {
if (i != 0) {//運算由左至右,除了第一個操作數,其它全部為右操作數,高優先順序的會處在樹的旁支
root->right = node;
node->parent = root;
}
root = node;
} else if((str[i] == 'x') || (str[i] == '/')) {
//處理乘除運算符
struct string_node *temp_root = NULL, *temp_parent = NULL;
if (root->parent &&(root->parent->tag == '+' || root->parent->tag == '-')) {
temp_parent = root->parent;
temp_root = root;
} else if(root->parent) {
temp_parent =root->parent->parent;
temp_root = root->parent;
} else {
temp_root = root;
}
node->parent = temp_parent;
if (temp_parent)
temp_parent->right = node;
node->left = temp_root;
temp_root->parent = node;
root = node;
} else if (str[i] == '+' || str[i] == '-') {
//處理加減運算符
struct string_node *temp_root =NULL, *temp_parent = NULL;
if (root->parent &&root->parent->parent &&(root->parent->tag == 'x' || root->parent->tag == '/')) {
temp_root =root->parent->parent;
} else if (root->parent&& !root->parent->parent){
temp_root = root->parent;
} else {
temp_root = root;
}
node->left = temp_root;
temp_root->parent = node;
root = node;
}
}
//找到樹根,返回調用者
while (root->parent) {
root = root->parent;
}
return root;
}
int main(int argc, char **argv)
{
struct string_node *root;
char str[40] ={'1','+','0','+','2','x','5','+','3','x','4','x','2', '-', '7'};
root = NULL;
root = gen_sub_tree(str, 100);
print_result(root);
return 0;
}
//輸出結果
void print_result(struct string_node*p)
{
if(p != NULL) {
//此為後綴表達式,根據printf的位置不同可以得到不同的X綴表達式
print_result(p->left);
print_result(p->right);
printf("%c\n", p->tag);
}
}
『伍』 有關樹的編程,用C語言描述
看數據結構的書去,書上都有具體的演算法,稍微修改一下就可以了,我給你看一個我現在正在做的一個二分查找代碼填空習題:核心代碼如下
intinsert_key(BSTree*root,intkey)
{
BiTnode*father=NULL,*p=root,*s;
while(p!=NULL&&key!=p->key_value)
{
father=p;
if(key<p->key_value)p=p->left;
elsep=p->right;
}
/*這個是二分查找的代碼,至於怎麼輸出這個范圍內的,不用我教你了吧?
做個循環就可以了
*/
……
}
『陸』 編程關於樹的問題
這個是c++程序,呵呵
顯示的是後續遍歷
#include<iostream.h>
typedef struct TreeNode
{
int key;
struct TreeNode *left;
struct TreeNode *right;
}treeNode;
class BiSortTree
{
public:
BiSortTree(void);
void desplayTree(void);//顯示這個樹
void insertTree(int key);//在樹中插入一個值
deleteTree(int key);//在樹中刪除一個值
treeNode* searchTree(int key);//在樹中查找一個值
~BiSortTree();
private:
treeNode* buildTree(treeNode* head,int number);//建立一個樹
treeNode* search(treeNode* head ,int key);//查找
treeNode* BiSortTree::searchParent(treeNode* head,treeNode* p);//查找出p的父親節點的指針
treeNode* BiSortTree::searchMinRight(treeNode* head);//找到右子樹中最小的節點
void showTree(treeNode* head);//顯示
void destroyTree(treeNode* head);//刪除
treeNode *Head;
};
/**************以下是建立一個二叉排序樹****************/
BiSortTree::BiSortTree()
{
cout<<"建立一棵二叉排序樹,請輸入你要建樹的所有數(以-1 作為結束標志!): "<<endl;
Head=NULL;
int number;
cin>>number;
while(number!=-1)
{
Head=buildTree(Head,number);
cin>>number;
}
}
treeNode* BiSortTree::buildTree(treeNode* head,int number)
{
treeNode *p;
p=new treeNode;
p->key=number;
p->left =p->right=NULL;
if(head==NULL)
{
return p;
}
else
{
if(p->key <head->key)
head->left=buildTree(head->left,number);
else
head->right=buildTree(head->right,number);
return head;
}
}
/*****************以下是在一棵二叉排序樹插入一個數***********************************/
void BiSortTree::insertTree(int key)
{
Head=buildTree(Head,key);
}
/*****************以下是在一個二叉排序樹查找一個數是否存在*************************/
treeNode* BiSortTree::searchTree(int key)
{
return search(Head,key);
}
treeNode* BiSortTree::search(treeNode* head ,int key)
{
if(head==NULL)
return NULL;
if(head->key==key)
return head;
else
{
if(key<head->key )
return search( head->left,key);
else
return search(head->right,key);
}
}
/************以下是在一個二叉排序樹刪除一個給定的值*********************************/
BiSortTree::deleteTree(int key)
{
treeNode *p;
p=NULL;
p=search(Head,key);
if(p==NULL)
{
cout<<"Don't find the key";
}
if(p==Head)
{
Head=NULL;
}
else
{
treeNode* parent;
parent=searchParent(Head,p);
if(p->left==NULL&&p->right==NULL)//葉子節點
{
if(parent->left==p)
{
parent->left=NULL;
}
else
{
parent->right=NULL;
}
}
else//非葉子節點
{
if(p->right==NULL)//該節點沒有右孩子
{
if(parent->left==p)
{
parent->left=p->left ;
}
else
{
parent->right=p->left;
}
}
else//該點有左右孩子
{
treeNode * rightMinSon,* secondParent;//secondParent為右子樹中的最小節點的父親
rightMinSon=searchMinRight(p->right);
secondParent=searchParent(p->right ,rightMinSon);
secondParent->left=rightMinSon->right;
if(p->right==rightMinSon)//右子樹中的最小節點的父親為p
{
p->right=rightMinSon->right ;
}
p->key=rightMinSon->key ;
}
}
}
}
treeNode* BiSortTree::searchParent(treeNode* head,treeNode* p)
{
if(head->left==p||head->right==p||head==p||head==NULL)
return head;
else
{
if(p->key<head->key)
return searchParent(head->left ,p);
else
return searchParent(head->right ,p);
}
}
treeNode* BiSortTree::searchMinRight(treeNode* head)//找到右子樹中最小的節點
{
if(head->left ==NULL||head==NULL)
{
return head;
}
else
{
return searchMinRight(head->left);
}
}
/*****************以下是顯示一個二叉排序樹****************************************/
void BiSortTree::desplayTree(void)
{
showTree(Head);
cout<<endl;
}
void BiSortTree::showTree(treeNode* Head)
{
if(Head!=NULL)
{
showTree(Head->left ) ;
cout<<Head->key<<' ' ;
showTree(Head->right) ;
}
}
/*****************以下是刪除一棵整二叉排序樹************************************/
BiSortTree::~BiSortTree()
{
cout<<"已經消除了一棵樹!!!!"<<endl;
destroyTree(Head);
}
void BiSortTree::destroyTree(treeNode* head )
{
if(head!=NULL)
{
destroyTree(head->left );
destroyTree(head->right );
delete head;
}
}
/*********************/
void print()
{
cout<<endl<<endl<<"以下是對二叉排序樹的基本操作:"<<endl
<<"1.顯示樹的中序遍歷"<<endl
<<"2.插入一個節點"<<endl
<<"3.尋找一個節點"<<endl
<<"4.刪除一個節點"<<endl;
}
void main()
{
BiSortTree tree;
int number;
int choiceNumber;
char flag;
while(1)
{
print() ;
cout<<"請選擇你要進行的操作(1~4)"<<endl;
cin>>choiceNumber;
switch(choiceNumber)
{
case 1:
tree.desplayTree();break;
case 2:
cout<<"請插入一個數: "<<endl;
cin>>number;
tree.insertTree(number);
tree.desplayTree();
break;
case 3:
cout<<"請插入你要找數: "<<endl;
cin>>number;
if(tree.searchTree(number)==NULL)
{
cout<<"沒有發現"<<endl;
}
else
{
cout<<"發現"<<endl;
}
break;
case 4:
cout<<"請輸入你要刪除的數: "<<endl;
cin>>number;
tree.deleteTree(number);
tree.desplayTree();
break;
default: break;
}
cout<<"你是否要繼續(Y/N)?"<<endl;
cin>>flag;
if(flag=='N'||flag=='n')
break;
}
}
『柒』 用processing編程如何實現畢達哥拉斯樹 求具體代碼
void setup()
{
size(600,600);
background(0);
noFill();
stroke(255,200);
frameRate(1);
}
boolean zmianaK = true;
int ii = 1;
void draw()
{
if(ii < 18)
{ background(1);
dPitagorejskie(100,height/2-50, 55, 0.25, 0.4, ii);
ii++;
}
}
void dPitagorejskie(float X, float Y, float D, float wspP, float wspH, int ilRek)
{
pushMatrix();
translate(X,Y);
rectMode(CENTER);
dPitagorejskieR(D, wspP, wspH, ilRek);
rectMode(CORNER);
popMatrix();
}
void dPitagorejskieR(float D, float wspP, float wspH, int ilRek)
{
if(ilRek > 0 && (wspP <= 1))
{
ilRek--;
rect(0,0,D,D);
pushMatrix();
translate(D/2,0);
float H = wspH*D;
float rA = wspP*D;
float rB = (1-wspP)*D;
float A = dist(0, -D/2, H, rA-D/2); //przeciwprostokatna A
float B = dist(0, D/2, H, D/2-rB); //przeciwprostokatna A
float alfa = atan(H/rA);
float beta = atan(H/rB);
translate(H/2, rA/2-D/2);
rotate(-alfa);
translate(A/2, 0);
dPitagorejskieR(A, zmianaK ? 1-wspP : wspP, wspH, ilRek);
popMatrix();
translate(D/2,D/2);
translate(H/2, -rB/2);
rotate(beta);
translate(B/2, 0);
dPitagorejskieR(B, zmianaK ? 1-wspP : wspP, wspH, ilRek);
}
}
『捌』 C++編程實現二叉樹的構建及其相關應用,求代碼
#include<stdio.h>
#include<malloc.h>
typedefchardatatype;
typedefstructBinNode{
datatypedata;
structBinNode*lchild;
structBinNode*rchild;
}BinNode;
typedefBinNode*bintree;//bintree本身是個指向結點的指針
//前序遍歷生成二叉樹
voidcreatetree(bintree*t){
datatypec;
c=getchar();
if(c=='#')
*t=NULL;
else{
*t=(bintree)malloc(sizeof(BinNode));
(*t)->data=c;
createtree(&(*t)->lchild);
createtree(&(*t)->rchild);
}
}
//中序遍歷
voidmidorder(bintreet){
if(t){
midorder(t->lchild);
printf("%c",t->data);
midorder(t->rchild);
}
}
//查找
boolsearch_tree(bintreet,datatypex){
if(!t){
returnfalse;
}
if(t->data==x){
returnt;
}else{
if(!search_tree(t->lchild,x)){
returnsearch_tree(t->rchild,x);
}
returntrue;
}
}
//求深度
intheight_tree(bintreet){
inth,left,right;
if(!t){
return0;
}
left=height_tree(t->lchild);
right=height_tree(t->rchild);
h=(left>right?left:right)+1;
returnh;
}
voidmain()
{
bintreeTree;
printf("Createtreeinpreorder: ");
createtree(&Tree);
printf("Displaytreeinmidorder: ");
midorder(Tree);
printf(" ");
intheight=height_tree(Tree);
printf("height:%d ",height);
search_tree(Tree,'e')?printf("Found! "):printf("NotFound! ");
return;
}
運行結果:
『玖』 各種編程語言抽象語法樹分別是什麼樣子的
e),是源代碼的抽象語法結構的樹狀表現形式,這里特指編程語言的源代碼。樹上的每個節點都表
『拾』 各種編程語言抽象語法樹分別是什麼樣子的
LISP的AST極像代碼是有原因的,LISP用的S-expression本身就是一個中間語言,相當於抽象語法樹生成的中間代碼,用來生成目標代碼的。
本來McCarthy是想用和我們現在使用的語言比較像的M-expression的,但是當時LISP程序員更喜歡用那個中間形式的S-expression,於是就保留下來了。