Serialize and Deserialize a Binary Tree

  Рет қаралды 33,126

IDeserve

IDeserve

Күн бұрын

Пікірлер: 68
@IDeserve
@IDeserve 8 жыл бұрын
Dear Friends, If you like our content and would like us to continue making great content for you, please spread the word about IDeserve. A share/appreciation from you on social network would mean the world to us! Also, do like our Facebook page: facebook.com/IDeserve.co.in :) Thanks, -Team IDeserve.
@TheGreatOne428
@TheGreatOne428 7 жыл бұрын
your code is not going to work because you are not passing the value of index to the function
@DivyanshuBansal
@DivyanshuBansal 7 жыл бұрын
int index is declared globally, so it will work.
@urge-bull
@urge-bull 5 жыл бұрын
First i was thinking this question is tough...but when i saw your tutorial in comment section of geekforgeeks i found it was the easiest question on trees.Thanks to you... i was surely clear after watching this videos that what i have to do to approch solution.
@IDeserve
@IDeserve 5 жыл бұрын
You are welcome Ritik!
@divjose90
@divjose90 5 жыл бұрын
Mind blown. Best explanation and visualization on Recursive call I have come across till date. Keep it up! Looking forward to more videos.
@IDeserve
@IDeserve 5 жыл бұрын
Thanks Divya!
@saisrikar7987
@saisrikar7987 6 жыл бұрын
Can anyone else be more clearer than this!!! What an explanation!!!..Loved it!!..Hope u make more videos!!!
@IDeserve
@IDeserve 6 жыл бұрын
Thank you so much for your kind words Sai 😀
@viralmehta2542
@viralmehta2542 3 жыл бұрын
appreciate the efforts to explain in such such great details!!
@IDeserve
@IDeserve 3 жыл бұрын
Thanks Viral!
@learnsharegrow7294
@learnsharegrow7294 5 жыл бұрын
Algorithm made easy with this video. Thanks.
@IDeserve
@IDeserve 5 жыл бұрын
Thanks Waseem!
@piyushsharma1638
@piyushsharma1638 5 жыл бұрын
Easiest way of explanation, thanks for making clear.
@IDeserve
@IDeserve 5 жыл бұрын
Thanks Piyush for your kind words!
@socialkeviv5444
@socialkeviv5444 4 жыл бұрын
I loved the serialise logic. ThankYou IDeserve. deserialise can be easy, something like below. Pseudocode // start=0; end=arr.length-1; deserialise(arr, start, end) { if(arr[left]==-1) return null; mid=start+(end-start)/2; new node root root->info=arr[start]; root-left= deserialise(arr, start +1, mid); root-right=deserialise(arr, mid+1, end); return root; } //
@abhishekkeer1520
@abhishekkeer1520 4 жыл бұрын
what a explaination .thanks IDeserve ,you deserve more like.
@IDeserve
@IDeserve 4 жыл бұрын
Thanks Abhishek!
@AviatorBro
@AviatorBro 4 жыл бұрын
Clear and precise explanation. Thank you
@IDeserve
@IDeserve 4 жыл бұрын
Thanks Amartya!
@akhilbhardwaj7733
@akhilbhardwaj7733 4 жыл бұрын
tough question , but explained nicely
@hydara.r.7003
@hydara.r.7003 5 жыл бұрын
My GOD what an excellent Explanation!
@IDeserve
@IDeserve 5 жыл бұрын
Thank you so much for your kind words Hydar!
@humanbeing496
@humanbeing496 4 жыл бұрын
great job explaining. appreciate it.
@IDeserve
@IDeserve 4 жыл бұрын
Thanks!
@ahmedrbii346
@ahmedrbii346 4 жыл бұрын
@@IDeserve Why did you stop downloading videos on your channel? The last video was downloaded was in 2016
@congdoan2006
@congdoan2006 6 жыл бұрын
It's such an thorough explanation. However for the base case ( i.e. step 1 ), "index == array.size()" test is not needed because "index" only reaches the array size when all the function calls complete and return ( i.e. after the very first function call returns ).
@MuhammadH
@MuhammadH 5 жыл бұрын
how can you do this without using a global variable index (stateless) ?
@shubhamchopra5518
@shubhamchopra5518 5 жыл бұрын
pass index also in the recursive call.
@BB31307
@BB31307 4 жыл бұрын
Hi there! All the way from GFG. so i was getting this Segmentation fault without this line "pre_index %= Arr.size();". For multiple test case the new_index starts from previous value. So that's the reason we are adding this line! So, is that so or anything else. Just wanted more info.
@preetisaroha3118
@preetisaroha3118 5 жыл бұрын
Nice explanation!! Thanks for your efforts.
@IDeserve
@IDeserve 5 жыл бұрын
Thanks Preeti!
@MyLifeMyWorld08
@MyLifeMyWorld08 5 жыл бұрын
Excellent explanation indeed !
@IDeserve
@IDeserve 5 жыл бұрын
Thanks Jitendra!
@jakubfraczek1208
@jakubfraczek1208 3 жыл бұрын
Thanks, helped me a lot!
@SushilRakhonde
@SushilRakhonde 6 жыл бұрын
There is bug in this programme, if condition in the deserialize method, if index greater than equal to array.size() then you should return immediately, else will throw exception
@SR-we1vl
@SR-we1vl 4 жыл бұрын
Well Delimiter used to identify "null" should be used as some random character in place of -1, cuz it will fail in case of negative numbers, viz. tree containing -1 as the value of the Tree node.
@ytuser659
@ytuser659 5 жыл бұрын
Brilliant! Thank you!
@rahuljha8038
@rahuljha8038 8 жыл бұрын
you guys are doing great job ........ please make a video to merge n sorted linked list using heap with code in c++
@IDeserve
@IDeserve 8 жыл бұрын
Thanks Ravi :) As per your request, we will soon create a video on the requested topic. We would really appreciate if you could spread the word about IDeserve in your college and to your colleagues. Also please check out our website at: www.ideserve.co.in It has features like Algorithm Visualization, Learn Together and many more coming soon. Please check it out and leave us a comment there! Thanks, -Team IDeserve.
@rahuljha8038
@rahuljha8038 8 жыл бұрын
yeah sure
@IDeserve
@IDeserve 8 жыл бұрын
Thanks Rahul.
@dangahng9911
@dangahng9911 4 жыл бұрын
Excellent!
@siddheshshirsat6904
@siddheshshirsat6904 8 жыл бұрын
In the deserialize() implementation, is there a way to not have variable index as global (or static to the method)? Having global variables might put the limitation of not being able to run the method concurrently in different threads.
@MrAditya4997
@MrAditya4997 7 жыл бұрын
You can just remove the first element of the array after processing it. So element at 0 index at any time should be current element to process. No need to keeping track of index variables (if you can modify the given array)
@justinejose5111
@justinejose5111 6 жыл бұрын
Pass idx as an argument and pass it by reference, so that it will keep updating. (Talking about C++)
@JJ-Bond
@JJ-Bond 5 жыл бұрын
so this deserialization uses index as a global variable to keep track of the states, do u know if there's a way that can avoid this? thanks
@ashwani6360
@ashwani6360 5 жыл бұрын
you can use a queue instead of array; and the base case will be to return NULL when queue is empty or q.front()==-1
@prabhakarp9861
@prabhakarp9861 5 жыл бұрын
Thought we need atleast two traversals inorder + (pre-order / postoder) to serialising the exact tree. May be i am missing something
@JJ-Bond
@JJ-Bond 5 жыл бұрын
good stuff starts at 10:36
@aashitaramteke6726
@aashitaramteke6726 4 жыл бұрын
Great!!!!!!!!!!!!
@IDeserve
@IDeserve 4 жыл бұрын
Thanks Ashita!
@arpit35007
@arpit35007 7 жыл бұрын
Will this approach work if there are duplicates in the tree.
@3hurricane4mark7
@3hurricane4mark7 6 жыл бұрын
Awesome! Walking the tree is the best way to understand recursive algorithm. I notice that by using -1 to denote null, and your code does not show value comparision, I assume it could only work for positive integers.
@prashantbhanarkar3280
@prashantbhanarkar3280 8 жыл бұрын
proof of correctness for serialization?
@RAVIKUMAR-ef4wo
@RAVIKUMAR-ef4wo 6 жыл бұрын
what an explanation!!!
@IDeserve
@IDeserve 6 жыл бұрын
Thank you so much for your kind words Ravi!
@rajranjan750
@rajranjan750 8 жыл бұрын
Is this solution correct My Approach If we make array reperesentation of a tree then we know that for any index the left child will be at 2*index and the right child will be at 2*index+1 so public static void serializeTree(TreeNode node,int[] arr,int index){ if(node==null){ return; } arr[index]=node.data; serializeTree(node.left, arr, 2*index); serializeTree(node.right, arr, 2*index+1); } public static TreeNode deserializeTree(int[] arr,TreeNode node,int index){ if(arr[index]==0){ return null; } node = new TreeNode(arr[index]); node.left=deserializeTree(arr, node, 2*index); node.right=deserializeTree(arr, node, 2*index+1); return node; }
@Kriishna47
@Kriishna47 7 жыл бұрын
I GUESS THIS WILL WORK OF COMPLETE BINARY TREE, BUT NOT FOR binary tree.
@ZiiiP2142
@ZiiiP2142 7 жыл бұрын
Raj Ranjan That idea should work as well.
@adicool00
@adicool00 6 жыл бұрын
What if the node is a negative value?
@uploder247
@uploder247 5 жыл бұрын
Use generics then and add a character X when value is not present. You can send both preorder and inorder to construct the tree back.
@virendrasinghbais9028
@virendrasinghbais9028 8 жыл бұрын
very helpful. thankx
@tonyriddle7646
@tonyriddle7646 3 жыл бұрын
im having segmentation fault... anyone ? which line? /* A binary tree node has data, pointer to left child and a pointer to right child struct Node { int data; Node* left; Node* right; }; */ void preorder(Node *root, vector& res){ if(root == nullptr){ res.push_back(-1); return; } res.push_back(root->data); // cout data left, res); preorder(root->right, res); } vector serialize(Node *root){ //Your code here if(root == nullptr) return {-1}; vector res; preorder(root, res); return res; } /*this function deserializes the serialized vector A*/ int dserial = 0; Node* deSerialize(vector &A){ if(dserial == A.size() || A[dserial] == -1){ dserial++; return nullptr; } Node *root; root->data = A[dserial]; dserial++; root->left = deSerialize(A); root->right = deSerialize(A); return root; }
@tonyriddle7646
@tonyriddle7646 3 жыл бұрын
never mind ...found the error this should be the statement... Node *root = new Node(A[dserial]); I didnt know i can do that with struct too...
@ZiiiP2142
@ZiiiP2142 7 жыл бұрын
Awesome
@IDeserve
@IDeserve 7 жыл бұрын
Thanks Ádám for your kind words :) We would really appreciate if you could spread the word about IDeserve in your college and to your colleagues. Also please check out our website at: www.ideserve.co.in It has features like Algorithm Visualization, Learn Together and many more coming soon. Please check it out and leave us a comment there! Thanks, -Team IDeserve.
@chanakyaveer8257
@chanakyaveer8257 5 жыл бұрын
GOODGOOD
Largest Subset With Consecutive Numbers
5:29
IDeserve
Рет қаралды 10 М.
Adaptive Huffman Coding Tree Example
25:13
IDeserve
Рет қаралды 28 М.
How Much Tape To Stop A Lamborghini?
00:15
MrBeast
Рет қаралды 200 МЛН
Ice Cream or Surprise Trip Around the World?
00:31
Hungry FAM
Рет қаралды 18 МЛН
Serialize and Deserialize a Binary Search Tree
13:58
IDeserve
Рет қаралды 14 М.
Adaptive Huffman Coding - Part 2
34:55
IDeserve
Рет қаралды 8 М.
Largest Node on the Right for Each Node in a Linked List
23:22
Populate next right pointers in a binary tree
18:15
IDeserve
Рет қаралды 16 М.
Object-Oriented Programming is Bad
44:35
Brian Will
Рет қаралды 2,3 МЛН
Check if binary tree is balanced
17:56
IDeserve
Рет қаралды 28 М.
Premature Optimization
12:39
CodeAesthetic
Рет қаралды 833 М.
The hidden beauty of the A* algorithm
19:22
Polylog
Рет қаралды 894 М.
How Much Tape To Stop A Lamborghini?
00:15
MrBeast
Рет қаралды 200 МЛН