BST的创建及分层打印

这篇博客详细介绍了BST(二叉搜索树)的创建和分层打印方法,包括C语言实现的代码示例。此外,还提到了Markdown编辑器的新功能,如图片拖拽、代码高亮、KaTeX数学公式、甘特图、UML图表和Flowchart流程图等,这些新特性旨在提升写作体验和内容展示效果。
摘要由CSDN通过智能技术生成

BST的创建及分层打印
#include <stdio.h>
#include <stdlib.h>
typedef struct tree
{
int nVal;
struct tree *pLeft;
struct tree *pRight;
}Tree;

typedef struct queue
{
Tree *nVal;
struct queue *pNext;
}MyQueue;

typedef struct node
{
int nCount;
MyQueue *pHead;
MyQueue *pEnd;
}Queue;

void Init(Queue **pQueue)
{
*pQueue=(Queue *)malloc(sizeof(Queue));
(*pQueue)->nCount=0;
(*pQueue)->pHead=NULL;
(*pQueue)->pEnd=NULL;
}

void Push(Queue **pQueue,Tree * nNum)
{
MyQueue pNode=(MyQueue)malloc(sizeof(MyQueue));
pNode->nVal=nNum;
pNode->pNext=NULL;
if((*pQueue)->pHead==NULL)
{
(*pQueue)->pHead=pNode;
}
//尾添加
else
{
(*pQueue)->pEnd->pNext=pNode;
}
(*pQueue)->pEnd=pNode;
(*pQueue)->nCount++;
}

Tree * Pop(Queue *pQueue)
{
Tree * nNum;
MyQueue *pDel=NULL;
//头删除
if(pQueue=NULL || (pQueue)->nCount==0) return NULL;
pDel=(pQueue)->pHead;
nNum=pDel->nVal;
(pQueue)->pHead=(pQueue)->pHead->pNext;
free(pDel);
pDel=NULL;
(pQueue)->nCount–;
return nNum;

}
void AddNode(Tree **pTree,int nNum)
{
Tree *pMark=NULL;
Tree pNode=(Tree)malloc(sizeof(Tree));
pNode->nVal=nNum;
pNode->pLeft=NULL;
pNode->pRight=NULL;
pMark=*pTree;
if(*pTree==NULL)
{
*pTree=pNode;
return;
}

while(pMark)
{
	//去左边
	if(pMark->nVal>nNum)
	{
		if(pMark->pLeft==NULL)
		{
			pMark->pLeft=pNode;
			return;
			
		}
		pMark=pMark->pLeft;
	}
	//去右边
	else if(pMark->nVal<nNum)
	{
		if(pMark->pRight==NULL)
		{
			pMark->pRight=pNode;
			return;
			
		}
		pMark=pMark->pRight;
	}
	else
	{
		printf("data error\n");
		free(pNode);
		pNode=NULL;
		return;
	}
}

return;

}
Tree *CreateTree(Tree **pTree,int arr[],int nLength)
{
int i;
for(i=0;i<nLength;i++)
{
AddNode(pTree,arr[i]);
}
return *pTree;

}
void Show1(Tree *pTree)
{
//构建一个辅助队列

Queue *pQueue1=NULL;
Queue *pQueue2=NULL;
Init(&pQueue1);
Init(&pQueue2);
if(pTree==NULL)return;

Push(&pQueue1,pTree);

while(pQueue1->nCount!=0 || pQueue2->nCount!=0 )
{
	while(pQueue1->nCount!=0)
	{
		pTree=Pop(pQueue1);
		printf("%d ",pTree->nVal);
		if(pQueue1->nCount==0)
		{
			printf("\n");
		}
	
		if(pTree->pLeft!=NULL)
		{
			Push(&pQueue2,pTree->pLeft);
		}
		if(pTree->pRight!=NULL)
		{
			Push(&pQueue2,pTree->pRight);
		}
	}
	while(pQueue2->nCount!=0)
	{
		pTree=Pop(pQueue2);
		printf("%d ",pTree->nVal);
		if(pQueue2->nCount==0)
		{
			printf("\n");
		}
	
		if(pTree->pLeft!=NULL)
		{
			Push(&pQueue1,pTree->pLeft);
		}
		if(pTree->pRight!=NULL)
		{
			Push(&pQueue1,pTree->pRight);
		}
	}
}

}

void Show(Tree *pTree)
{
if(pTree=NULL)return;
//左
Show(pTree->pLeft);
//根
printf("%d “,pTree->nVal);
//右
Show(pTree->pRight);
}
int main()
{
Tree *pTree=NULL;
Queue *pQueue=NULL;
int arr[]={6,4,10,3,5,9,8};
pTree=CreateTree(&pTree,arr,sizeof(arr)/sizeof(arr[0]));
Show(pTree);
printf(”\n");
printf("====================================\n");
Init(&pQueue);
Show1(pTree);
return 0;
}

新的改变

我们对Markdown编辑器进行了一些功能拓展与语法支持,除了标准的Markdown编辑器功能,我们增加了如下几点新功能,帮助你用它写博客:

  1. 全新的界面设计 ,将会带来全新的写作体验;
  2. 在创作中心设置你喜爱的代码高亮样式,Markdown 将代码片显示选择的高亮样式 进行展示;
  3. 增加了 图片拖拽 功能,你可以将本地的图片直接拖拽到编辑区域直接展示;
  4. 全新的 KaTeX数学公式 语法;
  5. 增加了支持甘特图的mermaid语法1 功能;
  6. 增加了 多屏幕编辑 Markdown文章功能;
  7. 增加了 焦点写作模式、预览模式、简洁写作模式、左右区域同步滚轮设置 等功能,功能按钮位于编辑区域与预览区域中间;
  8. 增加了 检查列表 功能。

功能快捷键

撤销:Ctrl/Command + Z
重做:Ctrl/Command + Y
加粗:Ctrl/Command + B
斜体:Ctrl/Command + I
标题:Ctrl/Command + Shift + H
无序列表:Ctrl/Command + Shift + U
有序列表:Ctrl/Command + Shift + O
检查列表:Ctrl/Command + Shift + C
插入代码:Ctrl/Command + Shift + K
插入链接:Ctrl/Command + Shift + L
插入图片:Ctrl/Command + Shift + G

合理的创建标题,有助于目录的生成

直接输入1次#,并按下space后,将生成1级标题。
输入2次#,并按下space后,将生成2级标题。
以此类推,我们支持6级标题。有助于使用TOC语法后生成一个完美的目录。

如何改变文本的样式

强调文本 强调文本

加粗文本 加粗文本

标记文本

删除文本

引用文本

H2O is是液体。

210 运算结果是 1024.

插入链接与图片

链接: link.

图片: Alt

带尺寸的图片: Alt

居中的图片: Alt

居中并且带尺寸的图片: Alt

当然,我们为了让用户更加便捷,我们增加了图片拖拽功能。

如何插入一段漂亮的代码片

博客设置页面,选择一款你喜欢的代码片高亮样式,下面展示同样高亮的 代码片.

// An highlighted block
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,让我来为您解答。要使用控制台输入的方式来创建BST(二叉搜索树),您需要先定义一个BST节点的数据结构。在这个数据结构中,我们需要包含一个数据成员, 存储节点的值,以及左右子树的指针成员。例如,以下是一个简单的BST节点: ``` struct bstNode { int val; bstNode* left; bstNode* right; }; ``` 然后,您可以编写一个函数来向BST中插入新节点。这个函数将首先检查根节点是否存在,如果不存在,就创建一个新的节点并将其设置为根节点。如果根节点已经存在,则遍历BST来找到一个合适的插入位置。 以下是一个简单的函数,可以使用控制台输入来创建BST: ``` #include <iostream> using namespace std; struct bstNode { int val; bstNode* left; bstNode* right; }; bstNode* insert(bstNode* root, int value) { if (root == nullptr) { root = new bstNode; root->val = value; root->left = root->right = nullptr; } else { if (value < root->val) { root->left = insert(root->left, value); } else { root->right = insert(root->right, value); } } return root; } int main() { bstNode* root = nullptr; int n; cout << "输入要插入的数字的数量: "; cin >> n; for (int i = 0; i < n; i++) { int value; cout << "输入要插入的数字: "; cin >> value; root = insert(root, value); } cout << "BST创建成功!" << endl; return 0; } ``` 您现在可以输入要插入的数字的数量和要插入的数字,然后程序将在控制台上显示您输入的数字组成的BST树的结构。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值