vertical traversal of binary tree
C++
/* This is just a function of vertical traversal of binary tree. You need to
write required code. Thank you. */
// Class to store node and it's distance from parent.
class Obj
{
public:
Node *root;
int dis;
Obj(Node *node, int dist)
{
root = node;
dis = dist;
}
};
// Main logic of vertical traversal.
void verticalTraversal(Node *root)
{
queue<Obj*> q;
Obj *ob = new Obj(root, 0);
q.push(ob);
map<int, vector<int>> m;
while(!q.empty())
{
Obj *ob = q.front();
q.pop();
if(m.find(ob->dis) != m.end())
{
m[ob->dis].push_back(ob->root->data);
}
else
{
vector<int> v;
v.push_back(ob->root->data);
m[ob->dis] = v;
}
if(ob->root->left != NULL)
q.push(new Obj(ob->root->left, ob->dis-1));
if(ob->root->right != NULL)
q.push(new Obj(ob->root->right, ob->dis+1));
}
for(auto it=m.begin(); it!=m.end(); it++)
{
vector<int> v1 = (*it).second;
for(int j = 0; j<v1.size(); j++)
cout << v1[j] << "\t";
}
cout << endl;
}// Java program for printing vertical order of a given binary tree
import java.util.TreeMap;
import java.util.Vector;
import java.util.Map.Entry;
public class VerticalOrderBtree
{
// Tree node
static class Node
{
int key;
Node left;
Node right;
// Constructor
Node(int data)
{
key = data;
left = null;
right = null;
}
}
// Utility function to store vertical order in map 'm'
// 'hd' is horizontal distance of current node from root.
// 'hd' is initially passed as 0
static void getVerticalOrder(Node root, int hd,
TreeMap<Integer,Vector<Integer>> m)
{
// Base case
if(root == null)
return;
//get the vector list at 'hd'
Vector<Integer> get = m.get(hd);
// Store current node in map 'm'
if(get == null)
{
get = new Vector<>();
get.add(root.key);
}
else
get.add(root.key);
m.put(hd, get);
// Store nodes in left subtree
getVerticalOrder(root.left, hd-1, m);
// Store nodes in right subtree
getVerticalOrder(root.right, hd+1, m);
}
// The main function to print vertical order of a binary tree
// with the given root
static void printVerticalOrder(Node root)
{
// Create a map and store vertical order in map using
// function getVerticalOrder()
TreeMap<Integer,Vector<Integer>> m = new TreeMap<>();
int hd =0;
getVerticalOrder(root,hd,m);
// Traverse the map and print nodes at every horigontal
// distance (hd)
for (Entry<Integer, Vector<Integer>> entry : m.entrySet())
{
System.out.println(entry.getValue());
}
}
// Driver program to test above functions
public static void main(String[] args) {
// TO DO Auto-generated method stub
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.left = new Node(6);
root.right.right = new Node(7);
root.right.left.right = new Node(8);
root.right.right.right = new Node(9);
System.out.println("Vertical Order traversal is");
printVerticalOrder(root);
}
}
Also in C++:
- Title
- what does the modularity mean in c++
- Category
- C++
- Title
- c++ overloaded == operator
- Category
- C++
- Title
- arduino delay millis
- Category
- C++
- Title
- primos menores que
- Category
- C++
- Title
- create a bitset of 1024 bits,
- Category
- C++
- Title
- loop through array c++
- Category
- C++
- Title
- strchr function in c++
- Category
- C++
- Title
- c++ string to stream
- Category
- C++
- Title
- calling by reference and pointers c++
- Category
- C++
- Title
- reverse in vector c++
- Category
- C++
- Title
- c++ string to integer without stoi
- Category
- C++
- Title
- Rectangle area hackerrank solution in c++
- Category
- C++
- Title
- c++ set add element
- Category
- C++
- Title
- c++ scanf
- Category
- C++
- Title
- c++ uint32_t
- Category
- C++
- Title
- multiset c++
- Category
- C++
- Title
- c++ remove text file
- Category
- C++
- Title
- c++ initialize a vector
- Category
- C++
- Title
- flake8 max line length
- Category
- C++
- Title
- what is time complexity of min_element()
- Category
- C++
- Title
- how do for loops on c++
- Category
- C++
- Title
- insertion c++
- Category
- C++
- Title
- c++ get last element in vector
- Category
- C++
- Title
- c++ lettura file
- Category
- C++
- Title
- c++ delete dynamically allocated array
- Category
- C++
- Title
- coronavirus
- Category
- C++
- Title
- c++ code to print hello world
- Category
- C++
- Title
- c++ do while loop
- Category
- C++
- Title
- c++ function return array
- Category
- C++
- Title
- flushing output in c++
- Category
- C++
- Title
- if vector contains value c++
- Category
- C++
- Title
- how to include seld declared header file in c++
- Category
- C++
- Title
- cpp how to create an object of template class
- Category
- C++
- Title
- matrix eigen c++ example
- Category
- C++
- Title
- min coin change problem dp
- Category
- C++
- Title
- size of a matrix using vector c++
- Category
- C++
- Title
- convert stirng to int c++
- Category
- C++
- Title
- never gonna give you up
- Category
- C++
- Title
- for c++
- Category
- C++
- Title
- regexp_like oracle c++
- Category
- C++
- Title
- std::reverse
- Category
- C++
- Title
- getch c++ library
- Category
- C++
- Title
- how to take input in C++ in coding
- Category
- C++
- Title
- arrow operator c++
- Category
- C++
- Title
- c++ how to make a negative float positive
- Category
- C++
- Title
- pbds in c++
- Category
- C++
- Title
- arrays in C++
- Category
- C++
- Title
- euler's totient function c++
- Category
- C++
- Title
- cut by delimiter c++
- Category
- C++
- Title
- min heap priority queue c++
- Category
- C++
- Title
- ue4 c++ struct
- Category
- C++
- Title
- c++ triple
- Category
- C++
- Title
- c++ switch case statement
- Category
- C++
- Title
- c++ method name
- Category
- C++
- Title
- new keyword in cpp
- Category
- C++
- Title
- int to float c++
- Category
- C++
- Title
- linear search in c++
- Category
- C++
- Title
- when ratings will be updated for codechef
- Category
- C++
- Title
- how to delete a node c++
- Category
- C++
- Title
- how to turn int into string c++
- Category
- C++
- Title
- map insert c++
- Category
- C++
- Title
- char **
- Category
- C++
- Title
- c++ vector lower_bound index
- Category
- C++
- Title
- how to find the number of cycles in a graph C++
- Category
- C++
- Title
- how to use wasd c++
- Category
- C++
- Title
- how to find length of string in c++
- Category
- C++
- Title
- nginx linux
- Category
- C++
- Title
- statement that causes a function to end in c++
- Category
- C++
- Title
- binary serach in c++
- Category
- C++
- Title
- pairs in c++
- Category
- C++
- Title
- E/flutter (20384): [ERROR:flutter/third_party/txt/src/minikin/FontFamily.cpp(184)] Could not get cmap table size! E/flutter (20384): F/flutter (20384): [FATAL:flutter/third_party/txt/src/minikin/FontCollection.cpp(95)] nTypefaces == 0
- Category
- C++
- Title
- can we compare a long long int with int in c++ using max or min functions
- Category
- C++
- Title
- sfml default program
- Category
- C++
- Title
- C++ string format ctime
- Category
- C++
- Title
- c++ sql
- Category
- C++
- Title
- how to find the size of a character array in c++
- Category
- C++
- Title
- map.erase in c++
- Category
- C++
- Title
- C++ If
- Category
- C++
- Title
- access last element in vector in c++
- Category
- C++
- Title
- ue4 c++ array
- Category
- C++
- Title
- binary search in set c++
- Category
- C++
- Title
- Operator overloading in C++ Programming
- Category
- C++
- Title
- char to string c++
- Category
- C++
- Title
- shuffle vector c++
- Category
- C++
- Title
- user input c++
- Category
- C++
- Title
- c++ class inheritance
- Category
- C++
- Title
- SFML window
- Category
- C++
- Title
- append string to another string c++
- Category
- C++
- Title
- convert entire string to lowercase c++
- Category
- C++
- Title
- what does count function do in hashmap
- Category
- C++
- Title
- building native binary with il2cpp unity
- Category
- C++
- Title
- c++ cout int
- Category
- C++
- Title
- max element in array c++ stl
- Category
- C++
- Title
- input a string in c++
- Category
- C++
- Title
- Merge k sorted linked lists and return it as one sorted list.
- Category
- C++
- Title
- c++ modulo make it give only positive numbers
- Category
- C++
- Title
- array<string, 7> c++
- Category
- C++
- Title
- c++ code 2d block
- Category
- C++
- Title
- map vs unordered_map in C++
- Category
- C++
- Title
- Given an undirected graph, count the number of connected components.
- Category
- C++