25Binary Search Tree

# The root is the most accessible node 5 8 15 9 29 11

• Notes
• 26

This preview shows page 7 - 17 out of 26 pages.

followed to reach that node. The root is the most accessible node. 5 8 15 9 29 11 What is the depth of the root? The root always has a depth of 0. The depth of a tree is the depth of the deepest node.

Subscribe to view the full document.

Binary tree All the trees presented thus far exhibit a certain property, what is it? 5 8 15 12 29 2 7 1 4 50 13
Binary search tree A binary search tree is a binary tree such that, the nodes of a left sub-tree contain elements that are less than the element stored at the local root (or is empty); the nodes of a right sub-tree contain elements that are greater than the element stored at the local root (or is empty). 5 8 15 12 29 2 7 1 4 50 13 The definition precludes duplicate values.

Subscribe to view the full document.

Binary search tree Implementing a binary search tree, what is needed? That’s right, we need a class Node . What are its instance variables? Its instance variables are value , left and right . What are the types of these variables? value should be Comparable , left and right should be of type Node .
Binary search tree A static nested class to store the elements of the tree. public class BinarySearchTree< E extends Comparable<E> > { private static class Node<E> { private E value; private Node<E> left; private Node<E> right; }

Subscribe to view the full document.

Binary search tree Instance variable(s) of the class BinarySearchTree ? public class BinarySearchTree< E extends Comparable<E> > { private static class Node<E> { private E value; private Node<E> left; private Node<E> right; } private Node<E> root;
Memory diagram 8 root t Node BinarySearchTree right left value Comparable

Subscribe to view the full document.

8 9 15 11 5 root t Node BinarySearchTree
8 9 15 11 5 root t 5 8 11 9 15

Subscribe to view the full document.

Observations A leaf is a Node such that both its descendant reference variables ( left and right ) are null .
You've reached the end of this preview.
• Spring '10
• Samaan
• Tree traversal, Nested set model

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern