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

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

View Full Document Right Arrow Icon
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.
Background image of page 1

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

View Full Document Right Arrow Icon
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
Background image of page 2
Applications (general trees) Representing hierarchical information such as hierarchical file systems (directories have sub-directories), programs (parse trees); Huffman trees are used for (de-)compressing information (files); An efficient data structure to implement abstract data types such as heaps, priority queues and sets.
Background image of page 3

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

View Full Document Right Arrow Icon
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 .
Background image of page 4
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.
Background image of page 5

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

View Full Document Right Arrow Icon
Binary tree Binary trees can be defined recursively, A binary tree is empty, or; A binary tree consists of a value as well as two sub-trees;
Background image of page 6
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.
Background image of page 7

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

View Full Document Right Arrow Icon
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
Background image of page 8
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.
Background image of page 9

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

View Full Document Right Arrow Icon
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 .
Background image of page 10
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; }
Background image of page 11

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

View Full Document Right Arrow Icon
Binary search tree Instance variable(s) of the class BinarySearchTree ? public class BinarySearchTree< E extends Comparable<E> > {
Background image of page 12
Image of page 13
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 Right Arrow Icon
Ask a homework question - tutors are online