练习1 - 小球

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<sstream>
using namespace std;
#define L 101
typedef struct node;
typedef node *tree;	//tree => &node;tree本身就代表地址了 
struct node{
	bool flag = false;
	int data;
	tree left,right;
	int depth=1;
};
const int D=4,I=2;
tree a[L];	//定义line 
int head=0,tail=1;
tree bt;
tree root;
//int i=1;	//确定data 
int id;
int main()
{	
	//1.建树 
	//满二叉树可以通过 父节点N -> 子节点N 
	root = new node;
	root->data=1;
	a[head] = root;	 
	while(head < tail){
		head++;
		if(a[head-1]->depth<4){
			//定义左子树 
			bt=new node;	//bt仅保存一段地址 
			bt->depth = a[head-1]->depth+1;
			bt->data = a[head-1]->data * 2; 
			a[head-1]->left = bt;
			a[tail++] = bt;
			//定义左子树 
			bt=new node;	//开辟一段新地址,不会影响到以前的 
			bt->depth = a[head-1]->depth+1;
			bt->data = a[head-1]->data * 2+1; 
			a[head-1]->right = bt;
			a[tail++] = bt;
		}else{
			head = tail;	//停止了	
		}
	}
	//2.遍历树
	for(int i=1;i<=I;i++){
		bt=root;	//指向根节点 
		while(bt->depth<D){
			if(bt->flag){	//向右走 
				bt->flag = !bt->flag;
				bt = bt->right;
			}else{	//向左走 
				bt->flag = !bt->flag;
				bt = bt->left;
			}
		}
		id = bt->data;	//可覆盖; 
	} 
	cout << id;
	return 0;
}
//cout << "输入:"

结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值