计蒜客A1004-非递归树的中序遍历(数组模拟二叉树)

在这里插入图片描述
本题,先用getline获取一行数据,然后对每一组数据进行处理,用两个数组分别保存某个节点的左孩子以及右孩子,那么保存的是ascii码值,这样在中序遍历的时候,可以采用递归的方式,代码如下:

//@author:hairu,wu
#include<iostream>
using namespace std;

const int max_n=1e4;
int lchild[max_n],rchild[max_n];	//保存ASCII码值
int cnt=0,tol=0;

//中序打印 
void printNode(int root){
	//左边 
	if(lchild[root]!=-1){
		printNode(lchild[root]);
	}
	//打印 
	cnt++;	//当前第n个节点,用于打印最后一个节点时不打印空格
	cout<<(char)root;
	if(cnt!=tol) cout<<" "; 
	
	//右边 
	if(rchild[root]!=-1){
		printNode(rchild[root]);
	}
} 

int main(){
	string str;
	getline(cin,str);
	int root;
	for(int i=0;i<=str.length()-5;i+=7){
		int x=str[i];
		int y=str[i+2];
		int num=str[i+4]-'0';
		//根据num值进行判断
		if(num == 0){
			//根节点
			root =x;	//ascii值 
		} else if(num == 1){
			//左孩子
			lchild[y] = x;	//y的左孩子是x 
		}else if(num == 2){
			rchild[y]=x;
		} 
		//因为x这是一个新增的节点,所以将它的左右两个孩子都只为-1
		lchild[x]=-1;
		rchild[x]=-1;
		tol++;//节点总个数 
	}
	printNode(root);//打印
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
#include #include //#define error 0 //#define OVERFLOW -1 //#define ok 1 #define MAXSIZE 100 typedef char TElemType; typedef int Status; typedef struct BiTNode{ //的结点 TElemType data; struct BiTNode *lchild,*rchild; }BiTNode,*BiTree; typedef BiTree datatype; typedef struct { datatype data[MAXSIZE]; int top; }sqstack; typedef sqstack *STK; Status CreateBiTree(BiTree *T) { //先序建立二叉树 char ch; ch=getchar(); if(ch=='#') (*T)=NULL; //#代表空 else { (*T)=(BiTree)malloc(sizeof(BiTNode)); (*T)->data=ch; CreateBiTree(&(*T)->lchild); //先序建立左子 CreateBiTree(&(*T)->rchild); //先序建立右子 } return 1; } STK initstack() //栈的初始化 { STK s; s=(STK)malloc(MAXSIZE*sizeof(sqstack)); s->top=0; return s; //返回指向栈地址的指针 } Status stackempty(STK s) //判断栈是否为空 { return(s->top==0); } Status push(STK s,datatype *e) //压栈函数 { if(s->top==MAXSIZE) //栈满,则返回错误 return 0; else { s->data[s->top]=*e; (s->top)++; return 1; } } Status pop(STK s,datatype *e) //出栈函数 { if(stackempty(s)) //判断栈是否为空 return 0; else { s->top--; *e=s->data[s->top]; //用e接受栈顶元素 return 1; } } Status inordertraverse(BiTree T) //中序非递归遍历二叉树 { STK s; s=initstack(); // BiTree T; BiTree p; p=T; while (p||!stackempty(s)) { if(p) { push(s,&p); p=p->lchild; } else { pop(s,&p); printf("%2c",p->data); p=p->rchild; }//else }//while return 1; }//inordertraverse void main() { BiTree T=NULL; printf("\n Creat a Binary Tree .\n"); //建立一棵二叉树T* CreateBiTree( &T ); printf ("\nThe preorder is:\n"); inordertraverse(T); }
你可以这样解析出 map 中的 fullDocument 中的 OwnerID、name、QuotaSize: ``` package main import ( "fmt" "reflect" ) func main() { m := map[string]interface{}{ "_id": map[string]interface{}{ "_data": "8263B4EB63000000062B022C0100296E5A1004C3E4524B77B64630AC204C7469FAED7F46645F6964006463B4EB636EBBDB249F2ED2770004", }, "clusterTime": map[string]interface{}{ "1672801123": 6, }, "documentKey": map[string]interface{}{ "_id": "ObjectID(\"63b4eb636ebbdb249f2ed277\")", }, "fullDocument": map[string]interface{}{ "OwnerID": 123, "QuotaSize": 104857600, "_id": "ObjectID(\"63b4eb636ebbdb249f2ed277\")", "directory": "/buckets", "name": "123_test1", }, "ns": map[string]interface{}{ "coll": "UserQuotaConfig", "db": "filer3", }, "operationType": "insert", "wallTime": 1672801123879, } // 取出 fullDocument fullDocument, ok := m["fullDocument"].(map[string]interface{}) if !ok { fmt.Println("fullDocument not found") return } // 取出 OwnerID OwnerID, ok := fullDocument["OwnerID"].(int) if !ok { fmt.Println("OwnerID not found") return } fmt.Println("OwnerID:", OwnerID) // 取出 name name, ok := fullDocument["name"].(string) if !ok { fmt.Println("name not found") return } fmt.Println("name:", name) // 取出 QuotaSize QuotaSize, ok := fullDocument["QuotaSize"].(int) if !ok { fmt.Println("QuotaSize not found") return } fmt.Println("QuotaSize:", QuotaSize) } ``` 在这个例子中,我们首先将 map 赋值给了变量 m。然后使用类型断言取出了 fullDocument 并将其赋

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小吴同学GOGOGO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值