Lect17_trees

# Lect17_trees - ITI 1121 Introduction to Computing II Marcel...

This preview shows pages 1–13. Sign up to view the full content.

ITI 1121. Introduction to Computing II * Marcel Turcotte School of Information Technology and Engineering Version of March 27, 2010 Abstract Binary search tree (part I) * These lecture notes are meant to be looked at on a computer screen. Do not print them unless it is necessary.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Binary tree A binary tree is a tree-like (hierarchical) data structure such that each node stores a value and has at most two children, which are called left and right . 5 8 15 9 29 11
Applications (general trees) Representing hierarchical information such as hierarchical ﬁle systems (directories have sub-directories), programs (parse trees); Huﬀman trees are used for (de-)compressing information (ﬁles); An eﬃcient data structure to implement abstract data types such as heaps, priority queues and sets.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
5 8 15 9 29 11 All the nodes except one have exactly one parent. That node that has no parent is called the root (which is drawn at the top of the diagram). Each node has 0, 1 or 2 children. Nodes that have no children are called leaves (or external nodes). Links between nodes are called branches .
Binary tree 5 8 15 9 29 11 A node and its descendants is called a subtree . The size of tree is the number of nodes in the tree. An empty tree has size 0. Since the discussion is restricted to binary trees, we will sometimes use the word tree to mean a binary tree.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Binary tree Binary trees can be deﬁned recursively, A binary tree is empty, or; A binary tree consists of a value as well as two sub-trees;
Binary tree The depth of a node is the number of links starting from the root that must be 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.

This preview has intentionally blurred sections. Sign up to view the full version.

View 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 deﬁnition precludes duplicate values.

This preview has intentionally blurred sections. Sign up to view the full version.

View 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; }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Binary search tree Instance variable(s) of the class BinarySearchTree ? public class BinarySearchTree< E extends Comparable<E> > {
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 69

Lect17_trees - ITI 1121 Introduction to Computing II Marcel...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online