Ajouter un commentaire

Niroken
Une aide:)

Bien donc je suis parti du principe que tu voulais un arbre
binaire d'expression mathématique avec le plus de noeuds
droit possible par niveau, ce qui revient a dire selon moi
que tu cherches a équilibrer ton arbre.

J'ai donc pour l'occasion codé qq chose qui peut répondre à ton
problème, je n'ai pas détaillé ma pensée mais si tu le souhaites
je t'aiderais.

Voici le code :

import java.io.*;
import java.util.regex.*;

public class MyTree
{
	String[] mathExpr;
	MyNode rootNode;
	
	public MyTree(String[] mathExpr_arg)
	{
		mathExpr = mathExpr_arg;
		rootNode = new MyNode();
	}
	
	public void ParcoursMyTreeForAff(MyNode currentNode, int niveau)
	{
		System.out.println("Noeud de niveau : " + niveau + " data : "+ currentNode.data);
		
		if (currentNode.leftChildNode != null)
		{
			ParcoursMyTreeForAff(currentNode.leftChildNode, (niveau + 1));
		}
		
		if (currentNode.rightChildNode != null)
		{
			ParcoursMyTreeForAff(currentNode.rightChildNode, (niveau + 1));
		}
	}
	
	public void FillMyTree(String[] datasToLeft, String[] datasToRight, String data, MyNode currentNode)
	{
		currentNode.data = data;
		
		if (datasToLeft.length > 1)
		{
			currentNode.leftChildNode = new MyNode();
			currentNode.parentNode = currentNode;
			FillMyTree(CutMathExprInTwo(datasToLeft)[0], CutMathExprInTwo(datasToLeft)[1], CutMathExprInTwo(datasToLeft)[2][0], currentNode.leftChildNode);
		}
		if (datasToRight.length > 1)
		{
			currentNode.rightChildNode = new MyNode();
			currentNode.parentNode = currentNode;
			FillMyTree(CutMathExprInTwo(datasToRight)[0], CutMathExprInTwo(datasToRight)[1], CutMathExprInTwo(datasToRight)[2][0], currentNode.rightChildNode);
		}
		if (datasToLeft.length == 1)
		{
			currentNode.leftChildNode = new MyNode();
			currentNode.parentNode = currentNode;
			currentNode.leftChildNode.data = datasToLeft[0];
		}
		if (datasToRight.length == 1)
		{
			currentNode.rightChildNode = new MyNode();
			currentNode.parentNode = currentNode;
			currentNode.rightChildNode.data = datasToRight[0];
		}
	}
	
	public String[][] CutMathExprInTwo(String[] mathExprCurrent)
	{
		String[][] myDecoupage = new String[3][];
		
		//System.out.println("" + (mathExprCurrent.length / 2));
		
		if (!mathExprCurrent[mathExprCurrent.length / 2].matches("[0-9]+"))
		{
			myDecoupage[0] = new String[mathExprCurrent.length / 2];
			myDecoupage[1] = new String[mathExprCurrent.length / 2];
			myDecoupage[2] = new String[1];
			
			for (int i = 0; i < mathExprCurrent.length; i++)
			{
				if (i < mathExprCurrent.length / 2)
				{
					myDecoupage[0][i] = mathExprCurrent[i];
				}
				if (i > mathExprCurrent.length / 2)
				{
					myDecoupage[1][i - (mathExprCurrent.length / 2) - 1] = mathExprCurrent[i];
				}
				myDecoupage[2][0] = mathExprCurrent[(mathExprCurrent.length / 2)];
			}
		}
		if (mathExprCurrent[mathExprCurrent.length / 2].matches("[0-9]+"))
		{
			myDecoupage[0] = new String[(mathExprCurrent.length / 2) + 1];
			myDecoupage[1] = new String[(mathExprCurrent.length / 2) - 1];
			myDecoupage[2] = new String[1];
		
			for (int i = 0; i < mathExprCurrent.length; i++)
			{
				if (i < (mathExprCurrent.length / 2) + 1)
				{
					myDecoupage[0][i] = mathExprCurrent[i];
				}
				if (i > (mathExprCurrent.length / 2) + 1)
				{
					myDecoupage[1][i - (mathExprCurrent.length / 2) - 2] = mathExprCurrent[i];
				}
				myDecoupage[2][0] = mathExprCurrent[(mathExprCurrent.length / 2) + 1];
			}
		}
		
		return myDecoupage;
	}
	
	public void AffTab2D(String[][] tab2D)
	{
		for (int i = 0; i < tab2D.length; i++)
		{	
			for (int j = 0; j < tab2D[i].length; j++)
			{
				System.out.print(tab2D[i][j] + " ");
			}
		
			System.out.println("");
		}
	}
	
	public static void main(String[] args)
	{
		String[] test = new String[7];
		test[0] = "0";
		test[1] = "+";
		test[2] = "1";
		test[3] = "-";
		test[4] = "2";
		test[5] = "+";
		test[6] = "2";
		
		MyTree newTree = new MyTree(test); 
		newTree.FillMyTree(newTree.CutMathExprInTwo(test)[0], newTree.CutMathExprInTwo(test)[1], newTree.CutMathExprInTwo(test)[2][0], newTree.rootNode);
		newTree.ParcoursMyTreeForAff(newTree.rootNode, 0);
		//newTree.AffTab2D(newTree.CutMathExprInTwo(test));
	}
}

class MyNode
{
	MyNode rightChildNode;
	MyNode leftChildNode;
	MyNode parentNode;
	String data;
}

Bonne chance

Filtered HTML

Plain text

CAPTCHA
Cette question permet de vérifier que vous n'êtes pas un robot spammeur :-)
 PPPP   H  H  U   U   GGG    CCC 
P P H H U U G C
PPPP HHHH U U G GG C
P H H U U G G C
P H H UUU GGG CCC