Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Edwin
Edwin

Posted on

     

Depth First Search Binary Tree

Depth-first search

This approach involves backtracking for traversal and the deepest node is visited first and then backtracks up to the parent. There are three types of DFS traversal:-

  • Preorder
  • Inorder
  • postorder

PreOrder

In pre-order traversal of a binary tree, we first traverse the root, then the left subtree, and then finally the right subtree. We do this recursively to benefit from the fact that left and right subtrees are also trees.

The steps to follow.

  1. Create a function traverse and call it on the root
  2. call traverse on the left sub-tree.
  3. call traverse on the right sub-tree.

InOrder

In the In-order traversal of a binary tree, we first traverse the left subtree, then traverse the root, and then finally the right subtree. We do this recursively to benefit from the fact that left and right subtrees are also trees.

The steps to follow.

  1. call traverse on the left sub-tree.
  2. Create a function traverse and call it on the root
  3. call traverse on the right sub-tree.

PostOrder

In post-order traversal of a binary tree, we first traverse the left subtree, then the right subtree, and then finally the root. We do this recursively to benefit from the fact that left and right subtrees are also trees.

JavaScript implementation:-

classNode{constructor(val){this.val=val;this.left=null;this.right=null;}}classBST{constructor(){this.root=null;}insert(val){letnewNode=newNode(val);if(!this.root){this.root=newNode;}else{letcurrent=this.root;while(true){if(val<current.val){if(current.left===null){current.left=newNode;returnthis}else{current=current.left;}}else{if(current.right===null){current.right=newNode;returnthis}else{current=current.right}}}}}find(val){letcurrent=this.root;letfound=false;while(current&&!found){if(val<current.val){if(current.val===val){found=true;returncurrent;}else{current=current.left;}}else{if(current.val===val){found=true;returncurrent;}else{current=current.right;}}}return'not found'}DFSPreOrder(){letdata=[];functiontraverse(node){data.push(node.val);if(node.left)traverse(node.left);if(node.right)traverse(node.right);}traverse(this.root);returndata;}DFSPostOrder(){letdata=[];functiontraverse(node){if(node.left)traverse(node.left);if(node.right)traverse(node.right);data.push(node.val);}traverse(this.root);returndata;}DFSInOrder(){letdata=[];functiontraverse(node){if(node.left)traverse(node.left);data.push(node.val);if(node.right)traverse(node.right);}traverse(this.root);returndata;}}
Enter fullscreen modeExit fullscreen mode

Python implementation:-

classNode:def__init__(self,val):self.val=valself.left=Noneself.right=NoneclassBST:def__init__(self):self.root=Nonedefinsert(self,val):newNode=Node(val)ifself.root==None:self.root=newNodeelse:current=self.rootwhileTrue:ifval<current.val:ifcurrent.left==None:current.left=newNodereturnselfelse:current=current.leftelse:if(current.right==None):current.right=newNodereturnselfelse:current=current.rightdeffind(self,val):current=self.rootfound=Falsewhilecurrentandnotfound:ifval<current.val:current=current.leftelifval>current.val:current=current.rightelse:found=Trueif(notfound):return"not found"returncurrentdefdfspreorder(self):data=[]deftraverse(node):data.append(node.val)if(node.left):traverse(node.left)if(node.right):traverse(node.right)traverse(self.root)returndatadefdfsInorder(self):data=[]deftraverse(node):if(node.left):traverse(node.left)data.append(node.val)if(node.right):traverse(node.right)traverse(self.root)returndatadefdfspostorder(self):data=[]deftraverse(node):if(node.left):traverse(node.left)if(node.right):traverse(node.right)data.append(node.val)traverse(self.root)returndatabst=BST()bst.insert(100)bst.insert(200)bst.insert(150)bst.insert(175)bst.insert(160)bst.insert(180)bst.insert(75)bst.insert(50)bst.insert(65)bst.insert(40)bst.insert(55)bst.insert(20)print(bst.bfs())print(bst.dfspreorder())
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Python , JavaScript and Elixir Developer
  • Location
    Nairobi ,Kenya
  • Work
    Software Engineer
  • Joined

More fromEdwin

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp