二叉排序树的递归与非递归建立 C++实现

	#include <iostream>

using namespace std;

typedef struct Node{
	int data;
	struct Node*lchild;
	struct Node*rchild;
	struct Node*par; 
}*BST;

//递归建立二叉排序树 
int Res_Creat(BST &root, int key)
{
	if(root == NULL)
	{
		root = new Node;
		root->data = key;
		root->lchild = NULL;
		root->rchild = NULL;
		root->par = NULL;
		return 1;
	}
	else if(root->data == key)
		return 0;
	else if(root->data > key)
		return Res_Creat(root->lchild, key);
	else 
		return Res_Creat(root->rchild, key);
} 

//非递归建立二叉排序树
int Non_Res_Creat(BST &root, int key)
{
	Node *par_node = NULL;
	Node *t = root;
	while(t)
	{
		par_node = t;
		if(key < t->data)
			t = t->lchild;
		else if(key > t->data)
			t = t->rchild;
		else
			return 0; 
	}
	Node *temp = new Node;
	temp->data = key;
	temp->lchild = NULL;
	temp->rchild = NULL;
	temp->par = par_node;
	if(par_node == NULL)
		root = temp;
	else
	{
		if(key < par_node->data)
			par_node->lchild = temp;
		else 	
			par_node->rchild = temp; 
	}
} 

//中序遍历输出 
void Inordertraversal(BST root)
{
	if(root)
	{
		Inordertraversal(root->lchild);
		cout << root->data << " ";
		Inordertraversal(root->rchild);
	}
}

int main()
{
	BST root = NULL;
	int arr[] = { 17,12,19,10,15,18,25,8,11,13,16,22};
	for(int i = 0; i < 12; i++)
		Res_Creat(root, arr[i]);
	Inordertraversal(root);
	cout << endl;
	for(int i = 0; i < 12; i++)
		Non_Res_Creat(root, arr[i]);
	Inordertraversal(root);
	
	
	
	
	return 0;
	
}
  • 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、付费专栏及课程。

余额充值