[c++]基于数组的二叉树遍历

16 篇文章 0 订阅

树节点是以数据作为底层存储结构来存放的,在遍历结点的时候,分别采用了递归和非递归的方法。


#include <cstdio>
#include <iostream>
#include <vector>
#include <stack> 

using namespace std ;

struct node 
{
	int loc , key ,rchild ,lchild ;

	node ( int l , int k ):loc(l) , key(k) ,lchild (-1 ) , rchild (-1)
	{}
	node ( int l , int k , int ll , int r )
	{
		loc = l ;
		key = k ;
		lchild = ll ;
		rchild = r ;
	}
} ;

typedef node *tree ;
typedef node treeNode ;

treeNode *T[100] ;

int root = -1 ;



void visitTreeRecur( treeNode* t[]  , int k )
{
	if ( k == -1 )
		return ;
	else
	{
		cout<<"node loc: " <<t[k]->loc<<"node key: "<<t[k]->key<<endl  ;
		visitTreeRecur(t , t[k]->lchild) ;
		visitTreeRecur(t  , t[k]->rchild ) ;
	}
}


void InOrderNoRecur( treeNode *t [] , int root )
{
	if ( root == -1 )
		return ;
	else
	{
		int x = root ;
		stack<int> s ; 
		while ( (x!= -1) || !s.empty() )
		{
			if ( x != -1  ) 
			{
				s.push(x) ;
				x = t[x]->lchild ;
			}
			else
			{
				x = s.top () ;
				s.pop () ;

				cout<<"node loc :"<<t[x]->loc <<" node key :"<<t[x]->key <<endl ;

				x = t[x]->rchild ;
			}
		}
	}
}

void PreOrderNonRecur ( treeNode *t [] , int root )
{
	if ( root == -1  )
		return ;
	else
	{
		stack<int> s ;
		int temp  = root ;


		while ( (temp != -1 ) || !s.empty() )
		{
			if ( temp != -1 )
			{
				cout<<" node loc :" <<t[temp]->loc <<" node key : " <<t[temp]->key <<endl ;

				s.push(temp) ;
				temp = t[temp]->lchild ;
			}
			else
			{
				temp = s.top () ;
				s.pop () ;
				temp = t[temp]->rchild ;
			}
		}
	}
}


void initTree ()
{
	T[1] = new treeNode(1 , 12, 7 , 3 ) ;
	T[2] = new treeNode (2, 15 , 8 , -1) ;
	T[3] = new treeNode(3, 4, 10 , -1 ) ;
	T[4] = new treeNode (4 , 4 , 5 , 9 ) ;
	T[5] = new treeNode (5 , 2 , -1 , -1) ;
	T[6] = new treeNode (6 , 18 , 1 , 4) ;
	T[7] = new treeNode (7 , 7 , -1 , -1 ) ;
	T[8] = new treeNode (8 , 14 , 6 , 2 ) ;
	T[9] = new treeNode (9 , 21 , -1 , -1 ) ;
	T[10] = new treeNode (10 , 5 , -1 , -1 ) ;
	root = 6 ;
	
	PreOrderNonRecur(T , root ) ;

	InOrderNoRecur(T , root ) ;

	visitTreeRecur ( T , root ) ;
}
void main ( void )
{
	initTree () ;

	system("pause") ;
}



//coding me 

  • 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、付费专栏及课程。

余额充值