Created
April 18, 2022 03:36
-
-
Save anushshukla/561b5e99ee9badb9699331136bc17b93 to your computer and use it in GitHub Desktop.
[Draft] Binary tree level wise average of node values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type TypeNode = BinaryNode | null; | |
| class BinaryNode { | |
| public value: number; | |
| private _leftChild: TypeNode; | |
| private _rightChild: TypeNode; | |
| constructor(node: number, leftChild: TypeNode = null, rightChild: TypeNode = null) { | |
| this.value = node; | |
| if (leftChild) { | |
| this._leftChild = leftChild; | |
| } | |
| if (rightChild) { | |
| this._rightChild = rightChild; | |
| } | |
| } | |
| get leftChild() { | |
| return this._leftChild; | |
| } | |
| get rightChild() { | |
| return this._leftChild; | |
| } | |
| public getChildAverage() { | |
| const leftChildeNodeValue = this._leftChild?.value || 0; | |
| const rightChildeNodeValue = this._rightChild?.value || 0; | |
| return (leftChildeNodeValue + rightChildeNodeValue) / 2; | |
| } | |
| } | |
| class BinaryTree { | |
| private _tree: BinaryNode; | |
| private _nodeAvgLevelWise = [] as number[]; | |
| constructor(tree: BinaryNode) { | |
| this._tree = tree; | |
| } | |
| public generateNodeAvgLevelWise() { | |
| this._nodeAvgLevelWise.push( | |
| this._tree.value, // 4 | |
| this._tree.getChildAverage(), // 8 | |
| ); | |
| let grandChildAvg = 0 | |
| let grandChildAvgDivisor = 0; | |
| if (this._tree.leftChild) { | |
| grandChildAvg += this._tree.leftChild.getChildAverage(); // 6 | |
| grandChildAvgDivisor++; | |
| } | |
| if (this._tree.rightChild) { | |
| grandChildAvg += this._tree.rightChild.getChildAverage(); // 6 | |
| grandChildAvgDivisor++; | |
| } | |
| grandChildAvgDivisor = grandChildAvgDivisor || 1; | |
| this._nodeAvgLevelWise.push(grandChildAvg/grandChildAvgDivisor); // 6 | |
| let greatGrandChildAvg = 0; | |
| let greatGrandChildAvgDivisor = 0; | |
| if (this._tree.leftChild?.leftChild) { | |
| greatGrandChildAvg += this._tree.leftChild.leftChild.getChildAverage(); | |
| greatGrandChildAvgDivisor++; | |
| } | |
| if (this._tree.leftChild?.rightChild) { | |
| greatGrandChildAvg += this._tree.leftChild.rightChild.getChildAverage(); | |
| greatGrandChildAvgDivisor++; | |
| } | |
| if (this._tree.rightChild?.leftChild) { | |
| greatGrandChildAvg += this._tree.rightChild.leftChild.getChildAverage(); | |
| greatGrandChildAvgDivisor++; | |
| } | |
| if (this._tree.rightChild?.rightChild) { | |
| greatGrandChildAvg += this._tree.rightChild.rightChild.getChildAverage(); | |
| greatGrandChildAvgDivisor++; | |
| } | |
| grandChildAvgDivisor = greatGrandChildAvgDivisor || 1; | |
| this._nodeAvgLevelWise.push(grandChildAvg/2); // 6 | |
| } | |
| } | |
| const binaryTree = new BinaryNode( | |
| 4, | |
| new BinaryNode(7, | |
| new BinaryNode(10), | |
| new BinaryNode(2, | |
| null, | |
| new BinaryNode(6, | |
| new BinaryNode(2) | |
| ) | |
| ) | |
| ), | |
| new BinaryNode(9, | |
| null, | |
| new BinaryNode(6) | |
| ) | |
| ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment