Home Reference Source
import {TreeOperations} from 'dsa-gfg/src/trees/tree-operations.ts'
public class | source

TreeOperations

This is implementation of operations on a Tree

Method Summary

Public Methods
public

Given a binary tree, find height of it.

public

You are given a tree and you need to do the level order traversal on this tree.

public

Given a Binary Tree, your task is to print its level order traversal such that each level is separated by $.

public

printGivenLevel(node: TreeNode, level: number, ltr: boolean)

public

spiral(node: TreeNode)

Complete the function to print spiral order traversal of a tree.

Public Methods

public height(node: undefined): number source

Given a binary tree, find height of it.

1 / \ 10 39 / 5

Params:

NameTypeAttributeDescription
node undefined

Return:

number

public levelOrder(node: TreeNode) source

You are given a tree and you need to do the level order traversal on this tree. Level order traversal of a tree is breadth-first traversal for the tree. Testcase1: The tree is 1 / \ 3 2 So, the level order would be 1 3 2 Testcase2: The tree is 10 / \ 20 30 / \ 40 60 So, the level order would be 10 20 30 40 60

Params:

NameTypeAttributeDescription
node TreeNode

public levelOrderByLine(node: TreeNode) source

Given a Binary Tree, your task is to print its level order traversal such that each level is separated by $. For the below tree the output will be 1 $ 2 3 $ 4 5 6 7 $ 8 $. 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8

Params:

NameTypeAttributeDescription
node TreeNode

public printGivenLevel(node: TreeNode, level: number, ltr: boolean) source

Params:

NameTypeAttributeDescription
node TreeNode
level number
ltr boolean

public spiral(node: TreeNode) source

Complete the function to print spiral order traversal of a tree. Testcase1: The tree is 1 / \ 3 2 So, the spiral level order would be 1 3 2 Testcase2: The tree is 10 / \ 20 30 / \ 40 60 / \ 50 55 70 80 So, the spiral level order would be 10 20 30 60 40

Params:

NameTypeAttributeDescription
node TreeNode