二叉树的遍历_C++实现

(1)二叉树的建立

typedef struct BinaryTreeNode {
	char value;
	struct BinaryTreeNode *left;
	struct BinaryTreeNode *right;
}BiTreeNode, *BiTree;

//二叉树的建立
void createBiTree(BiTree &root) {
	char data;
	cin >> data;
	if(data == '#') {
		root = NULL;
	}
	else {
		root = new BiTreeNode();
		root->value = data;
		createBiTree(root->left);
		createBiTree(root->right);
	}
}

(2)二叉树的先序遍历

//先序遍历二叉树_递归
void preOrder_Recursively(BiTree &root) {
	if(root != NULL) {
		cout << root->value << " ";
		preOrder_Recursively(root->left);
		preOrder_Recursively(root->right);
	}
}

//先序遍历二叉树_非递归
void preOrder_UnRecursively(BiTree &root) {
	stack<BiTree> sta;
	BiTree p = root;  //p为遍历指针
	while(p != NULL || !sta.empty()) {
		if(p != NULL) {
			sta.push(p);
			cout << p->value << " ";
			p = p->left;
		}
		else {
			p = sta.top();
			sta.pop();
			p = p->right;
		}
	}
}

(3)二叉树的中序遍历

//中序遍历二叉树_递归
void inOrder_Recursively(BiTree &root) {
	if(root != NULL) {
		inOrder_Recursively(root->left);
		cout << root->value << " ";
		inOrder_Recursively(root->right);
	}
}

//中序遍历二叉树_非递归
void inOrder_UnRecursively(BiTree &root) {
	stack<BiTree> sta;
	BiTree p = root;  //p为遍历指针
	while(p != NULL || !sta.empty()) {
		if(p != NULL) {
			sta.push(p);
			p = p->left;
		}
		else {
			p = sta.top();
			sta.pop();
			cout << p->value << " ";
			p = p->right;
		}
	}
}

(4)二叉树的后序遍历

void postOrder_UnRecursively(BiTree &root) {
	stack<BiTreePost> sta;
	BiTree p = root;
	BiTreePost post;
	while(p != NULL || !sta.empty()) {
		while(p != NULL) {
			post = new BiTreeNodePost();
			post->biTree = p;
			post->tag = 'L';
			sta.push(post);
			p = p->left;
		}
		while(!sta.empty() && (sta.top())->tag == 'R') {
			post = sta.top();
			sta.pop();
			cout << post->biTree->value << " ";
		}
		if(!sta.empty()) {
			BiTreePost post = sta.top();
			post->tag = 'R';
			p = post->biTree->right;
		}
	}
}

(5)二叉树的层次遍历

//层次遍历
void levelOrder(BiTree &root) {
	queue<BiTree> que;
	BiTree p = root;
	if(p != NULL)
		que.push(p);
	while(!que.empty()) {
		p = que.front();
		cout << p->value << " ";
		que.pop();
		if(p->left != NULL)
			que.push(p->left);
		if(p->right != NULL)
			que.push(p->right);
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
叉树是一种常见的数据结构,遍叉树有多种方法,包括先序遍、中序遍和后序遍。以下是在C语言中用数组表示二叉树并进行遍的示例代码: ```c #include <stdio.h> // 定义二叉树节点结构 struct TreeNode { int data; struct TreeNode *left; struct TreeNode *right; }; // 构建二叉树 struct TreeNode nodes[10]; // 使用数组表示二叉树 void buildTree() { for (int i = 0; i < 10; i++) { nodes[i].data = i; nodes[i].left = NULL; nodes[i].right = NULL; } nodes[0].left = &nodes[1]; nodes[0].right = &nodes[2]; nodes[1].left = &nodes[3]; nodes[1].right = &nodes[4]; nodes[2].left = &nodes[5]; nodes[2].right = &nodes[6]; } // 先序遍 void preOrder(struct TreeNode *root) { if (root != NULL) { printf("%d ", root->data); preOrder(root->left); preOrder(root->right); } } // 中序遍 void inOrder(struct TreeNode *root) { if (root != NULL) { inOrder(root->left); printf("%d ", root->data); inOrder(root->right); } } // 后序遍 void postOrder(struct TreeNode *root) { if (root != NULL) { postOrder(root->left); postOrder(root->right); printf("%d ", root->data); } } int main() { buildTree(); printf("先序遍结果:"); preOrder(&nodes[0]); printf("\n中序遍结果:"); inOrder(&nodes[0]); printf("\n后序遍结果:"); postOrder(&nodes[0]); return 0; } ``` 以上代码定义了一个二叉树的结构,使用数组表示节点,并实现了先序、中序和后序遍的方法。在主函数中构建了一个二叉树,并进行了遍操作,打印出遍的结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值