Introduction to Trees (Binary Tree) in Python - A Simplified Tutorial

preview_player
Показать описание
This tutorial explains how to implement binary trees in Python. In subsequent lessons, we would see how to implement:
Tree traversal using Breadth First Search(BFS) Algorithm
Tree Traversal using Depth First Search (DFS) Algorithm

Python Tutorials here

Data Science Tutorial

Рекомендации по теме
Комментарии
Автор

Thanks Kindson, it is indeed a good video about tree creation in Python.

hendrag
Автор

here is the code:

class Node:
def __init__(self, data=None):
self.left = None
self.right = None
self.data = data

#set the root
def insert(self, data):
if self.data is None:
self.data = data
else:
if data < self.data:
if self.left is None:
self.left = Node(data)
else:
self.left.insert(data)

elif data > self.data:
if self.right is None:
self.right = Node(data)
else:
self.right.insert(data)


def PrintTree(self):
if self.left:
self.left.PrintTree()

print(self.data)

if self.right:
self.right.PrintTree()

root = Node("g")
root.insert("c")
root.insert("b")
root.insert("a")
root.insert("e")
root.insert("d")
root.insert("f")
root.insert("i")
root.insert("h")
root.insert("j")
root.insert("k")
root.PrintTree()

zhoujenny
Автор

sorry but I think you are meant to order that tree according to the binary search tree? I don't get why you put BFS as the title unless we are doing some sort of tree traversal in this video. I might be wrong though

cinward
Автор

This was very useful tutorial. Thanks for detailed explanation with python code.

rajeshchellappa
Автор

Thanks bro. I don't know if I understand better because of the Nigerian accent, but this helped a lot.

meilyn
Автор

How can I print it and see if it is in order?

juanjose
Автор

The first three lines in insert method is unnecessary. You can't initialize a Node without data.

avid_traveler
Автор

any info about making a 2d visualization of a data tree? like printing the drawing on the left as the output of the code

zurder
Автор

why does the output says AttributeError: 'NoneType' object has no attribute 'insert'?

zhoujenny
Автор

i have a doubt in --" if data<self.data:
if self.left==None:
self.left =Node(data) "


we could have written: -- " self.left=data"
why Node(data) ??

utkarsh
Автор

I seriously dont get the part where he says if data is less than self.data. Is that an arithmetic please

goodylili
Автор

thank you... informative video ... may I have your email to contact with you

hibamahmood
join shbcf.ru