構(gòu)建樹:
typedef struct Node
{int data;
Node *lchild;
Node *rchild;
}Node;
class Tree
{ public:
Node *root;
Tree(int data);
Node *maketree(Node *father,int data,int side);
};
Tree::Tree(int data)
{Node *newnode=new Node;
root=newnode;
root->data=data;
root->lchild=NULL;
root->rchild=NULL;
}
Node *Tree::maketree(Node *father,int data,int side)
{Node *newnode=new Node;
newnode->data=data;
newnode->lchild=NULL;
newnode->rchild=NULL;
if(side==0)
{father->lchild=newnode;
}
else
{father->rchild=newnode;
}
return newnode;
}
前序遍歷:
void front_display(Node *start)
{cout<data<<' ';
if(start->lchild)
{front_display(start->lchild);
}
if(start->rchild)
{front_display(start->rchild);
}
}
中序遍歷:
void middle_display(Node *start)
{ if(start->lchild)
{middle_display(start->lchild);
}
cout<data<<' ';
if(start->rchild)
{middle_display(start->rchild);
}
}
后序遍歷:
void last_display(Node *start)
{if(start->lchild)
{last_display(start->lchild);
}
if(start->rchild)
{last_display(start->rchild);
}
cout<data<<' ';
}
未經(jīng)允許,不得轉(zhuǎn)載!
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
當(dāng)前文章:C++樹模板-創(chuàng)新互聯(lián)
網(wǎng)站地址:http://www.rwnh.cn/article20/dopeco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、網(wǎng)站設(shè)計公司、外貿(mào)網(wǎng)站建設(shè)、域名注冊、搜索引擎優(yōu)化、移動網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容