Skip to content

Instantly share code, notes, and snippets.

@ansarisufiyan777
Created September 3, 2019 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ansarisufiyan777/4ebf4cc10d84dce53266b1781a2afd2b to your computer and use it in GitHub Desktop.
Save ansarisufiyan777/4ebf4cc10d84dce53266b1781a2afd2b to your computer and use it in GitHub Desktop.
Binary tree in typescript
var stack = [];
/**
* get empty object node
*/
function getObj() {
return {
data: null,
isleft: true,
left: null,
right: null
};
}
/**
* add element to tree unbalanced
*/
function addElementToTree(ele, tree) {
if (!stack.length) {
setAndPushObject(tree);
}
else {
var lo = stack.splice(0, 1)[0];
setAndPushObject(lo);
}
function setAndPushObject(to) {
to.data = ele;
to.left = getObj();
to.right = getObj();
stack.push(to.left);
stack.push(to.right);
}
}
/**
* Print binary tree by level starting from top
*/
var printStack = [];
function printBinaryTreeDataByLevel(obj) {
console.log(">>Level>>", obj.data);
if (obj.left && obj.left.data) {
printStack.push(obj.left);
}
if (obj.right && obj.right.data) {
printStack.push(obj.right);
}
while (printStack.length) {
var o = printStack.splice(0, 1)[0];
printBinaryTreeDataByLevel(o);
}
}
/**
* Print binary tree by order
*/
function printBinaryTreeDataByPreOrder(obj) {
console.log(">>Pre Order>>", obj.data);
if (obj.left && obj.left.data) {
printBinaryTreeDataByPreOrder(obj.left)
}
if (obj.right && obj.right.data) {
printBinaryTreeDataByPreOrder(obj.right);
}
}
/**
* Print binary tree by in order
*/
function printBinaryTreeByInOrder(obj) {
debugger;
if (obj.left.data) {
printBinaryTreeByInOrder(obj.left)
}
console.log(">>In Order>>", obj.data)
if (obj.right.data) {
printBinaryTreeByInOrder(obj.right)
}
}
/**
* print binary tree post order
*/
function printBinaryTreeByPostOrder(obj) {
debugger;
if (obj.left.data) {
printBinaryTreeByPostOrder(obj.left)
}
if (obj.right.data) {
printBinaryTreeByPostOrder(obj.right)
}
console.log(">>Post Order>>", obj.data)
}
/**
* Create balanced binary (Has bug)
*/
function createBalancedBinaryTree(arr, obj) {
let l = arr.length;
let c = Math.ceil(l / 2)
if (l > 1) {
let d = arr[Math.ceil(l / 2)]
obj.data = d
obj.left = getObj()
obj.right = getObj()
createBalancedBinaryTree(arr.slice(0, c), obj.left)
createBalancedBinaryTree(arr.slice(c + 1, l), obj.right)
} else {
let d = arr[0]
obj.data = d
}
}
/**
* Start of the program
*/
function run() {
var obj = {
data: null,
left: null,
right: null
};
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
for (var _i = 0, arr_1 = arr; _i < arr_1.length; _i++) {
var i = arr_1[_i];
addElementToTree(i, obj);
}
console.log("Binary tree:", obj);
printBinaryTreeDataByLevel(obj);
printBinaryTreeDataByPreOrder(obj)
printBinaryTreeByInOrder(obj)
printBinaryTreeByPostOrder(obj)
var balancedObj = {
data: null,
left: null,
right: null
}
createBalancedBinaryTree(arr, balancedObj)
console.log('Balanced binary tree', balancedObj)
printBinaryTreeDataByLevel(balancedObj);
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment