leftNode null cursorleftNode new TreeNodeelement return cursor cursorleftNode

Leftnode null cursorleftnode new treenodeelement

This preview shows page 1 - 3 out of 3 pages.

if(cursor.leftNode == null){ cursor.leftNode = new TreeNode(element); return; } cursor = cursor.leftNode; } else { if(cursor.rightNode == null){ cursor.rightNode = new TreeNode(element); return; } cursor = cursor.rightNode; } }
Image of page 1
} // enters in the queue for using the breadthfirstsearch algo public static void breathFirstSearch(TreeNode root){ //object created Queue<TreeNode> queue = new LinkedList<TreeNode>(); queue.add(root); TreeNode cursor = root; //loop begins while(!queue.isEmpty()){ cursor = (TreeNode)queue.remove(); if(cursor.leftNode != null){ queue.add(cursor.leftNode); } if(cursor.rightNode != null){ queue.add(cursor.rightNode); } System.out.println("viewed node " + cursor.data); } } // Depthsearch algo for the finding the element public static void depthFirstSearch(TreeNode root){ //object created Stack<TreeNode> stack = new Stack<TreeNode>(); if(root == null){ return; } TreeNode cursor = null; stack.add(root); while(!stack.empty()){ cursor = stack.peek(); // methods of stack to follow the procedure for doing search if(cursor.leftNode != null && !cursor.leftNode.visited){ stack.add(cursor.leftNode); continue; } if(cursor.rightNode != null && !cursor.rightNode.visited){ stack.add(cursor.rightNode); continue; } stack.pop(); cursor.visited = true; System.out.println("viewed node " + cursor.data); } } private void breathFirstSearch(){ Queue<TreeNode> queue = new LinkedList<TreeNode>(); queue.add(root); TreeNode cursor = root; while(!queue.isEmpty()){ cursor = (TreeNode)queue.remove(); if(cursor.leftNode != null){ queue.add(cursor.leftNode); } if(cursor.rightNode != null){ queue.add(cursor.rightNode); } System.out.println("viewed node " + cursor.data); } } private void depthFirstSearch() {
Image of page 2
Image of page 3

You've reached the end of your free preview.

Want to read all 3 pages?

  • Spring '14
  • NancyLawson
  • Data Structures, Binary Search Tree, TreeNode, TreeNode cursor

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes