Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Kako napraviti u javaScriptu binarno stablo gde korisnik unosi nivo grananja(depth)?

[es] :: Javascript i AJAX :: Kako napraviti u javaScriptu binarno stablo gde korisnik unosi nivo grananja(depth)?

[ Pregleda: 1180 | Odgovora: 2 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

mls_tod

Član broj: 176231
Poruke: 2
*.dynamic.isp.telekom.rs.



Profil

icon Kako napraviti u javaScriptu binarno stablo gde korisnik unosi nivo grananja(depth)?08.04.2012. u 20:50 - pre 146 meseci
Draw a binary tree but with an arbitrary depth. User can enter/adjust the depth. All these should be completed in JavaScript.
 
Odgovor na temu

mls_tod

Član broj: 176231
Poruke: 2
*.amres.ac.rs.



Profil

icon Re: Kako napraviti u javaScriptu binarno stablo gde korisnik unosi nivo grananja(depth)?20.04.2012. u 17:13 - pre 146 meseci
I evo odgovora! Ne mogu da verujem da niko nije ponudio neko resenje!?

<!DOCTYPE html>
<html>
<head>
<title>BinaryTree</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript">
function Tree(x, y, width, height, ctx) {
this.draw = function(){
ctx.fillStyle = "rgb(200,0,0)";
ctx.fillRect (x, y, width, height);
ctx.rect(x, y, width, height);
};
};

function formSubmit() {
n = document.getElementById('first').value;
if (n==null || n==""){formReset();
alert("Enter the number in the blank text box");}
else drawTree(n);
}

function formReset() {
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

var rectWidth = 10000;
var rectHeight = 4000;

ctx.clearRect (0, 0, rectWidth, rectHeight);

document.getElementById('first').value = null;
}

function drawTree(depth) {
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

var nodeWidth = 30;
var nodeHeight = 30;

var rectWidth = 10000;
var rectHeight = 4000;

ctx.clearRect (0, 0, rectWidth, rectHeight);

for(i = 0; i <=depth; i++) {
for(j = 0; j < Math.pow(2, i); j++) {
var canvasWidth = Math.pow(2, depth) * nodeWidth + (Math.pow(2, depth) / 2 - 1) * nodeWidth + Math.pow(2, depth) / 2 * 3 * nodeWidth + nodeWidth;

var canvasHeight = depth * 2 * nodeHeight;

var nodeSpace = canvasWidth / Math.pow(2, i);

var x = nodeSpace / 2 - nodeWidth / 2 + nodeSpace * j;
var y = canvasHeight - i * nodeHeight * 1.5;

var node = new Tree(x, y, nodeWidth, nodeHeight, ctx);
node.draw();

if(i < depth) {
ctx.beginPath();
ctx.lineWidth = 5;
ctx.moveTo(x + nodeWidth / 2, y);
ctx.lineTo(x - nodeSpace / 4 + nodeWidth / 2, y - nodeHeight / 2);
ctx.stroke();

ctx.moveTo(x + nodeWidth / 2, y);
ctx.lineTo(x + nodeSpace / 4 + nodeWidth / 2, y - nodeHeight / 2);
ctx.stroke();
}
}
}
}
</script>

</head>
<body>

<form id="frm1" action="form_action.asp">
Enter Depth: <input id="first" type="text" name="fname" /><br />
<input type="button" onclick="formSubmit()" value="Submit form" />
<input type="button" onclick="formReset()" value="Reset" />
</form>

<canvas id="myCanvas" width="10000" height="4000"></canvas>

</body>
</html>
 
Odgovor na temu

Milos911
Serbia

Član broj: 219127
Poruke: 1230
77.243.16.*



+303 Profil

icon Re: Kako napraviti u javaScriptu binarno stablo gde korisnik unosi nivo grananja(depth)?20.04.2012. u 18:37 - pre 146 meseci
Pa nisi ponudio nagradu :)
 
Odgovor na temu

[es] :: Javascript i AJAX :: Kako napraviti u javaScriptu binarno stablo gde korisnik unosi nivo grananja(depth)?

[ Pregleda: 1180 | Odgovora: 2 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.