Tree traversal algorithms (BFS and DFS - Preorder, Inorder, Postorder)

preview_player
Показать описание
📚 Learn how to solve problems and build projects with these Free E-Books ⬇️

Experience the power of practical learning, gain career-ready skills, and start building real applications!
This is a step-by-step course designed to take you from beginner to expert in no time!
💰 Here is a coupon to save 10% on your first payment (CODEBEAUTY_YT10).
Use it quickly, because it will be available for a limited time.

Trees are an important data structure because of their non-linear nature, which allows for a faster response time during a search process. So in this video, I decided to teach you about the most important algorithms and ways to search and traverse a binary tree.
After watching the video you'll learn and understand the differences between BFS (breadth-first search) and DFS (depth-first search) algorithms, and also learn everything you need to know about Preorder, Inorder, and Postorder algorithms.

Contents:
00:00 - Introduction
02:38 - BFS and DFS
04:32 - Preorder algorithm
08:38 - Traversing a tree with recursion (explaining the code)
15:34 - Inorder algorithm
19:49 - Postorder algorithm

Follow me on other platforms:
Рекомендации по теме
Комментарии
Автор

📚 Learn how to solve problems and build projects with these FREE E-Books ⬇️
Experience the power of practical learning, gain career-ready skills, and start building real applications!
This is a step-by-step course designed to take you from beginner to expert in no time!
💰 Here is a coupon to save 10% on your first payment (CODEBEAUTY_YT10).
Use it quickly, because it will be available for a limited time.

Code from this video:

#include <iostream>
using namespace std;

struct Node {
int data;
Node* left;
Node* right;
};

Node* createNode(int data) {
Node* newNode = new Node();
newNode->data = data;
newNode->left = newNode->right = nullptr;
return newNode;
}

void printTree(Node* root) {
if (root == nullptr) return;

//preorder algorithm (D, L, R)
cout << root->data << endl;
printTree(root->left);
printTree(root->right);
}

int main()
{
//Level 1
Node* root = createNode(1);
//Level 2
root->left = createNode(2);
root->right = createNode(3);
//Level 3
root->left->left = createNode(4);
root->left->right = createNode(5);
root->right->left = createNode(6);
root->right->right = createNode(7);
//Level 4
root->left->right->left = createNode(9);
root->right->right->left = createNode(15);

printTree(root);

cin.get();
}

CodeBeauty
Автор

4, 9, 5, 2, 6, 15, 7, 3, 1 is the post order traversal. Is this the correct answer, please let me know.

md.abubakkarsiddiq
Автор

Amazing video! I can't explain how long I've been looing for a good explanation, glad I finally found it.

dundyd
Автор

Your video lessons are so helpful for my coding journey. The things which are hard to understand in textbook is easy to follow on your tutorial. Thanks for your amazing work ❤️

graymatter
Автор

Hello. It is the best explanation I've ever seen. Thank you for your lessons

МаксСивухо
Автор

//Postorder algorithm (L, R, D)
printTree(root->left);
printTree(root->right);
cout << root->data << endl;

azixkhan
Автор

Hi, this channel is pure gold, is understandable even for an italian like me. Thank you very much.

farbolos
Автор

Cool! Very simple explaining. Thank you so much!!!

khusanumarov
Автор

Thank you for a great paced and visual explanation of the topic, keep up the great work, the e-books are great.

GMTX-qjor
Автор

since you're learning algorithms like the video it helps youtube algorithms
CLASSIC!!

peterchisangamwamba
Автор

Very refreshing video... keep the good work up.

adammed
Автор

These books are amazing, thanks for sharing free resources with us Saldina ❤️

marym
Автор

Better explained than those tedious and never-ending college CS courses. I enjoy your instructions. Simply Amazing!

jliu
Автор

This is the most excellent explanation on recursion I have ever seen!

charleswayne
Автор

Omg! I have never understood how a recursive function works until I watched this video! Your explanations are top notch!

montyGator
Автор

very comprehensive video Saldina.Your english also are awesome.Can you make if you find free time more video about windows forms applications?

johnniewalkerjohnniewalker
Автор

You managed to make this look super easy. The best traversing trees tutorial I have seen ( and I have seen a lot of them ).

Daniel-umye
Автор

This is the clearest explanation of DFS algorithms that I've seen. Thank you for making it easy (where my book and lecturer did not)!

dangerbirb
Автор

Maybe the most helpful video on recursion that I've seen

deli
Автор

My placement session is coming, can you make more such videos on Data structures and algorithms, your explanations are very good🔥❤️

_shauryaagarwal