데이터 구조 - 코드 구현 - 이 진 트 리

#include
using namespace std;
typedef struct bitnode {
	char data;
	bitnode *lchild,*rchild;
} bitnode;
class bitt {
	private:
		bitnode *pt;
		void create(bitnode *&t);
		void intra(bitnode *t);
		int countLeaf(bitnode *t);
		int count(bitnode *t);
		int countone(bitnode *t);
		int countx(bitnode *t);

	public:
		void createbitree();
		void inordertra();

		int countbitLeaf( );
		int countbit();//   
		int countbitone();//  1 
		int countxbit( );


};
int bitt::countx(bitnode *t) {
	if(t) {
		if(t->lchild&&!t->rchild||!t->lchild&&t->rchild)return 1+countx(t->lchild)+countx(t->rchild);
		else return countx(t->lchild)+countx(t->rchild);
	} else return 0;
}
int bitt::countone(bitnode *t) {

	if(t) if(t->lchild&&t->rchild==NULL||t->rchild&&t->lchild==NULL)  return 1+countone(t->lchild)+countone(t->rchild);
		else return countone(t->lchild)+countone(t->rchild);
	return 0;
}
int bitt:: countxbit( ) {
	bitnode *p=pt;
	return countx(p);
}
void bitt:: create(bitnode *&t) {
	char ch;
	cin>>ch;
	if(ch=='.')t=NULL;
	else {
		t=new bitnode;
		t->data=ch;
		create(t->lchild);
		create(t->rchild);
	}
}

void bitt::intra(bitnode *t) {
	if(t) {
		intra(t->lchild);
		cout<data;
		intra(t->rchild);
	}
}
void bitt::createbitree() {
	bitnode *p;
	create(p);
	pt=p;
}
void bitt::inordertra() {
	bitnode *p=pt;
	intra(p);
}
int bitt::countLeaf(bitnode *t) {
	if(t) {
		int m=countLeaf(t->lchild);
		int n=countLeaf(t->rchild);
		if(m+n==0)return 1;
		else return m+n;
	} else return 0;
}
int bitt::countbitLeaf() {
	bitnode *p=pt;
	return countLeaf(p);
}
int bitt::count(bitnode *t) {
	if(t) if(t->lchild||t->rchild)
			return 1+count(t->lchild)+count(t->rchild);
	return 0;
}
int bitt::countbit() {
	bitnode *p=pt;
	return count(p);
}


int bitt::countbitone() {
	bitnode *p=pt;
	return countone(p);
}

int main() {
	bitt pt;
	pt.createbitree();
	pt.inordertra();
	cout<

좋은 웹페이지 즐겨찾기