二叉树,写着玩的

本文介绍了一种使用C++实现的二叉树数据结构,包括节点定义、树的创建及先序遍历方法。通过具体的代码示例,展示了如何构造二叉树并进行遍历操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#ifndef DL_H
#define DL_H
#define null 0
#include <iostream>
using namespace std;
struct BinTreeNode
{
	char * data;
	BinTreeNode *leftchild , *rightchild;
	BinTreeNode(){ leftchild = null; rightchild = null; }
	BinTreeNode( char * str ,BinTreeNode *leftchild = null , BinTreeNode *rightchild = null)
	{
		data = (char *)calloc(strlen(str) + 1 , sizeof(char));
		strcpy(data , str);
		this->leftchild = leftchild;
		this->rightchild;
	}
};
class BinTree
{
private:
	BinTreeNode *root;
public:
	BinTree();
	BinTree(BinTreeNode *&root);
	//~BinTree();
	bool Create(BinTreeNode *&root);

	bool PreOrder(BinTreeNode *&root);
	bool PostOrder(BinTreeNode *root);
	bool InOrder(BinTreeNode *root);
	int Size();
	int Height();
	int CountOfNode();
};

#endif

#include "dl.h"

BinTree::BinTree()
{
	root = null;
}
BinTree::BinTree(BinTreeNode *&root)
{
	Create(root);
}

bool BinTree::Create(BinTreeNode *&root)
{
	char *data = (char *)calloc(128 , sizeof(char));
 	cin>>data;
	if(strcmp(data , "#") == 0) return true;
	root = new BinTreeNode();
	root->data = (char *)calloc(128 , sizeof(char));
	strcpy(root->data , data);
	free(data);
	data = null;
	Create(root->leftchild);
	Create(root->rightchild);
	return true;
}
bool BinTree::PreOrder(BinTreeNode *&root)
{
	if(root == null )
		return true;
	else{
		cout<<root->data<<endl;
		PreOrder(root->leftchild);
		PreOrder(root->rightchild);
	}	
}


#include"dl.h"

void main()
{
	BinTreeNode *treenode= new BinTreeNode();
	BinTree bt(treenode);
	bt.PreOrder(treenode);
	system("pause");
}


当然,下面是相同的问题的C++代码实现: ```cpp #include <iostream> #include <unordered_map> using namespace std; struct Node { char value; Node* left; Node* right; Node(char val) : value(val), left(nullptr), right(nullptr) {} }; Node* buildTree(const string& postorder, const string& inorder, int post_start, int post_end, int in_start, int in_end, unordered_map<char, int>& in_map) { if (post_start > post_end || in_start > in_end) { return nullptr; } char root_value = postorder[post_end]; Node* root = new Node(root_value); int root_index = in_map[root_value]; int left_tree_size = root_index - in_start; root->left = buildTree(postorder, inorder, post_start, post_start + left_tree_size - 1, in_start, root_index - 1, in_map); root->right = buildTree(postorder, inorder, post_start + left_tree_size, post_end - 1, root_index + 1, in_end, in_map); return root; } void preorderTraversal(Node* root) { if (root == nullptr) { return; } cout << root->value; preorderTraversal(root->left); preorderTraversal(root->right); } void constructTree(const string& postorder, const string& inorder) { unordered_map<char, int> in_map; for (int i = 0; i < inorder.length(); ++i) { in_map[inorder[i]] = i; } Node* root = buildTree(postorder, inorder, 0, postorder.length() - 1, 0, inorder.length() - 1, in_map); preorderTraversal(root); cout << endl; // 清理内存 // ... } int main() { string postorder, inorder; while (cin >> postorder >> inorder) { constructTree(postorder, inorder); } return 0; } ``` 这段代码使用递归的方式实现了二叉树的构建和先序遍历。它首先构建了一个哈希表,用于快速查找中序遍历中节点的索引。然后,通过调用 `buildTree` 函数来递归构建二叉树,并通过 `preorderTraversal` 函数进行先序遍历。 希望对你有帮助!如果你还有其他问题,请随时问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值