c++实现一个二叉排序树以及树的递归遍历的实现

二叉排序树的理论介绍:

https://baike.baidu.com/item/%E4%BA%8C%E5%8F%89%E6%8E%92%E5%BA%8F%E6%A0%91/10905079?fr=aladdin

一棵空树,或者是具有下列性质的二叉树

(1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值;

(2)若右子树不空,则右子树上所有结点的值均大于它的根结点的值;

(3)左、右子树也分别为二叉排序树;

(4)没有键值相等的结点。

二叉排序树的节点类定义:

class node_data{
public:
	int data;//数据域
	node_data *parent, *left, *right;//每个节点有3个指针,分别指向父节点,左节点和右节点
	node_data() :data(0), parent(NULL), left(NULL), right(NULL){};//默认构造函数
	node_data(int in_data) :data(in_data), parent(NULL), left(NULL), right(NULL) {};//产生一个节点
};

二叉排序树的类定义:

class binary_tree{
public:
	binary_tree(int num[], int len);//调用构造函数的时候,\
			就可以生成相对应大小的二叉树,构造方法中调用的是递归或者非递归的方式生成二叉树
	void insert_node(int data);//非递归的方式插入一个节点
	void insert_node_recursive(int data);//递归的方式插入一个节点,\
			递归的时候是通过调用下面private成员中的方法insert_node_recu
	void delete_node(int data);//递归的方式删除一个节点及其之下的所有左右子树的节点,\
			递归的时候是通过调用下面private成员中的方法delete_node_recu
	node_data *search_node(int data);//递归的方式查找一个节点,\
			递归的时候是通过调用下面private成员中的方法search_node_recu
	void print_tree(void);
private:
	node_data *root; //根节点
	void insert_node_recu(node_data *current, int data);//具体实现递归插入方法
	void delete_node_recu(node_data *current);//递归实现删除的方法
	node_data *search_node_recu(node_data *current, int data);//data递归实现查找的方法
	void print_tree_ldr_recu(node_data *current);//使用递归的方法中序遍历
};

二叉排序树的插入:

对二叉排序树的插入分为递归和非递归的方法

binary_tree::binary_tree(int num[], int len)
{
	root = new node_data();
	for (int i =
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是二叉排序树的先序、中序和后序非递归遍历C++代码: ```c++ #include <iostream> #include <stack> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; // 先序遍历 void preOrder(TreeNode* root) { if (root == NULL) { return; } stack<TreeNode*> s; s.push(root); while (!s.empty()) { TreeNode* node = s.top(); s.pop(); cout << node->val << " "; if (node->right != NULL) { s.push(node->right); } if (node->left != NULL) { s.push(node->left); } } } // 中序遍历 void inOrder(TreeNode* root) { if (root == NULL) { return; } stack<TreeNode*> s; TreeNode* node = root; while (node != NULL || !s.empty()) { while (node != NULL) { s.push(node); node = node->left; } node = s.top(); s.pop(); cout << node->val << " "; node = node->right; } } // 后序遍历 void postOrder(TreeNode* root) { if (root == NULL) { return; } stack<TreeNode*> s1, s2; s1.push(root); while (!s1.empty()) { TreeNode* node = s1.top(); s1.pop(); s2.push(node); if (node->left != NULL) { s1.push(node->left); } if (node->right != NULL) { s1.push(node->right); } } while (!s2.empty()) { cout << s2.top()->val << " "; s2.pop(); } } int main() { TreeNode* root = new TreeNode(5); root->left = new TreeNode(3); root->right = new TreeNode(7); root->left->left = new TreeNode(2); root->left->right = new TreeNode(4); root->right->left = new TreeNode(6); root->right->right = new TreeNode(8); cout << "Preorder traversal: "; preOrder(root); cout << endl; cout << "Inorder traversal: "; inOrder(root); cout << endl; cout << "Postorder traversal: "; postOrder(root); cout << endl; return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值