Friday, November 1, 2013

Depth-First Search and Breath-First Search with TreeNode

Depth-First Search(DFS) and Breath-First Search(BFS) is one of Artificial Intelligence algorithm..
It use to search an item in tree form like a Binary Search Tree(BST)..
but binary only can have 2 child...
and DFS or BFS can have more than 2 child...



In this algoritm, i create/use a TreeNode class to store child of tree..
BinaryTreeNode class have two BinaryTree, leftChild and RightChild..
for this case, i will replace leftChild and RightChild with a LinkedList that can store more than one data...

TreeNode.java



Search.java




MainClass.java




Sample Running :

Depth-First Seatch
EN        Opened                   Closed
 --        ------                   ------
           [a]                      []
 a         [b, c]                   [a]
 b         [d, e, c]                [a, b]
 d         [e, c]                   [a, b, d]

Breath-First Seatch
EN        Opened                   Closed
 --        ------                   ------
           [a]                      []
 a         [b, c]                   [a]
 b         [c, d, e]                [a, b]
 c         [d, e, g]                [a, b, c]
 d         [e, g]                   [a, b, c, d]










you can test it by your self..
i hope you like this souce code...
and please give some comment....
                                                                                                                                                      Created By : Z-man, 2013



No comments:

Post a Comment