I am familiar with the big O record and runtime, and most of the time I can find out the runtime of the algorithm, but I'm not sure about that. I am dealing with a tree here.
First method:
static boolean containsData(TreeNode treeRoot, String data) {
if(treeRoot == null)
return false;
if(data.equals(treeRoot.data))
return (true);
else if((data.compareTo(treeRoot.data))<0)
return(containsData(treeRoot.left, data));
else
return(containsData(treeRoot.right, data));
}
Second method:
static boolean containsData2(TreeNode treeRoot,String data){
if (treeRoot != null) {
if (treeRoot.data.contains(data)) {
return true;
} else {
return containsData2(treeRoot.left,data) || containsData2(treeRoot.right,data);
}
}
return false;
}
I would say that both methods are O (n). I do not see how one of them will be O (log (n)) .
source
share