Skip to content

Instantly share code, notes, and snippets.

@d-asensio
Last active September 25, 2016 17:13
Show Gist options
  • Save d-asensio/02da68c6d51923d65aeeda907ee8bdf1 to your computer and use it in GitHub Desktop.
Save d-asensio/02da68c6d51923d65aeeda907ee8bdf1 to your computer and use it in GitHub Desktop.
Sundio - Excercice 1
namespace Sundio {
/**
* Node
*
* A simple implementation of a binary node.
*/
export class Node <T> {
private _n: T;
private _lSon: Node <T>;
private _rSon: Node <T>;
constructor(n: T) {
this._n = n;
this._lSon = null;
this._rSon = null;
}
/**
* Defines the left son of the node.
*
* @param lSon The node that will be defined as the left son.
*/
public setLeftSon(lSon: Node <T>): void {
this._lSon = lSon;
}
/**
* Defines the right son of the node.
*
* @param rSon The node that will be defined as the right son.
*/
public setRightSon(rSon: Node <T>): void {
this._rSon = rSon;
}
/**
* Checks if the node has a left son.
*
* @returns 'true' if the node has left son, 'false' if not.
*/
public hasLeftSon(): boolean {
return this._lSon !== null;
}
/**
* Checks if the node has a right son.
*
* @returns 'true' if the node has right son, 'false' if not.
*/
public hasRightSon(): boolean {
return this._rSon !== null;
}
/**
* Retrieves the current left son.
*
* @returns The left son node if is defined, if is not defined, returns null.
*/
public getLeftSon(): Node <T> {
return this._lSon;
}
/**
* Retrieves the current right son.
*
* @returns The right son node if is defined, if is not defined, returns null.
*/
public getRightSon(): Node <T> {
return this._rSon;
}
/**
* Retrieves the defined value of the node.
*
* @returns The defined value of the node, which always will be defined.
*/
public value(): T {
return this._n;
}
}
/**
* Prints all the nodes of a binary tree in in-order.
*
* @param node The root node of the tree.
*/
export function travese(node: Sundio.Node <number>): void {
// Print the left node first.
if (node.hasLeftSon()) {
travese(node.getLeftSon());
}
// Then the current one.
console.log(node.value());
// And finally the right node.
if (node.hasRightSon()) {
travese(node.getRightSon());
}
}
}
const nodes: Sundio.Node <number> [] = [];
// Setting values of the nodes.
nodes.push(new Sundio.Node(8)); // 0
nodes.push(new Sundio.Node(3)); // 1
nodes.push(new Sundio.Node(10)); // 2
nodes.push(new Sundio.Node(1)); // 3
nodes.push(new Sundio.Node(6)); // 4
nodes.push(new Sundio.Node(14)); // 5
nodes.push(new Sundio.Node(4)); // 6
nodes.push(new Sundio.Node(7)); // 7
nodes.push(new Sundio.Node(13)); // 8
// Setting relations between nodes.
nodes[0].setLeftSon(nodes[1]);
nodes[0].setRightSon(nodes[2]);
nodes[1].setLeftSon(nodes[3]);
nodes[1].setRightSon(nodes[4]);
nodes[2].setRightSon(nodes[5]);
nodes[4].setLeftSon(nodes[6]);
nodes[4].setRightSon(nodes[7]);
nodes[5].setLeftSon(nodes[8]);
// Calling our in-order travese function
Sundio.travese(nodes[0]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment