C ++二叉树问题 [英] C++ binary tree problem

查看:79
本文介绍了C ++二叉树问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<iostream>
using namespace std;
struct treeNode{
	int indexI,indexJ;
	treeNode *Right,*Down;
};
treeNode*Try(int i,int j,int n,int x=0);
void preorderPrint( treeNode *root );
int**Array;
void read(int**A,int n){
	for(int i=0;i<n;i++)>
		for(int j=0;j<n;j++){>
			cout<<"\nEnter the element["<<i<<"]["<<j<<"]: ";
			cin>>A[i][j];
		}
}
void main(){
	int size;
	cout<<"\nEnter the size of the array : ";
	cin>>size;
	Array=new int*[size];
	for(int i=0;i<size;i++)>
		Array[i]=new int[size];
	read(Array,size);
	treeNode*root=Try(0,0,size);
	preorderPrint( root );
}
treeNode*Try(int i,int j,int n,int x){
	treeNode*p=new treeNode;
	if(Array[i][j]!=-1){
		bool b=false;
		if(x==0){
			if((Array[i][j]+j)<n){>
				p->indexI=i; p->indexJ=j;
				b=true;}
		}
		else{
			if((Array[i][j]+i)<n){>
				p->indexI=i; p->indexJ=j;
				b=true;}
		}
		if(b){
			p->Right=Try(i,j+Array[i][j],n,0);
			p->Down=Try(i+Array[i][j],j,n,x=1);
			return p;
		}
		else
			return NULL;
	}

}
void preorderPrint( treeNode *root ) {
           // Print all the items in the tree to which root points.
           // The item in the root is printed first, followed by the
           // items in the left subtree and then the items in the
           // right subtree.
        if ( root != NULL ) {  // (Otherwise, there's nothing to print.)
			cout << root->indexI << " "<<root->Right<<" ";      // Print the root item.
			preorderPrint( root->Down );    // Print items in left subtree.
           preorderPrint( root->Right );   // Print items in right subtree.
        }
     } // end preorderPrint()



该程序抛出异常我不知道为什么。


The program is throwing exceptions and I do not know why.

推荐答案

嗨朋友,如果你的程序有错误不介意再检查一下数据结构的概念,否则我可以在java中做同样的程序如果你希望我能发送你的话,那么如果你想让mlm结构与我联系,那么java最好做这个工作...
hi friend if ur program has error dont mind just check the Data Structure concepts again if else i can do the same program in java and if u wish i can send u
java is best to do this work dude if u want the mlm structure contact me...


这篇关于C ++二叉树问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆