10 Binary Search Trees

10 Binary Search Trees - Binary Search Trees 15-211:...

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

View Full Document Right Arrow Icon
Binary Search Trees 15-211: Fundamental Data Structures and Algorithms Charlie Garrod 16 February 2010 Reading for today: 19.1 – 19.5
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Announcements HW3 is out! Theory due in class next Tuesday No quiz tomorrow!
Background image of page 2
3 Last time: Heaps The min-heap-order property: The item at each node is less than the items at it’s children The heap structure property: The heap is a complete binary tree 13 21 16 39 31 19 68 47 42 32 13 32 31 68 19 16 21 39 42 47 0 1 2 3 4 5 6 7 8 9 10 11 12
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Today: Binary Search Trees (BSTs) A quick tree review Binary tree traversals Binary Search Trees Basic BST operations Balanced BSTs AVL trees
Background image of page 4
5 A brief review of trees Terms you should know: node, root, leaf, parent, children, level, sibling, path, … The depth of a node is the length of the path from the root to that node The height of a node is the length of the longest path from that node to a leaf The height of a tree is the height of its root
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 What is the minimum height of a rooted tree with n >1 nodes? What is the maximum height of a rooted tree with n nodes? A brief review of trees
Background image of page 6
7 What is the minimum height of a rooted tree with n >1 nodes? What is the maximum height of a rooted tree with n nodes? A brief review of trees
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 Binary trees A tree is a binary tree if each node has at most two children public class BinaryTree { private BinaryTree leftChild; private BinaryTree rightChild; }
Background image of page 8
9 What is the minimum height of a binary tree with n nodes? What is the maximum height of a binary tree with n nodes? What is the maximum number of nodes at level i ? Binary trees
Background image of page 9

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

View Full DocumentRight Arrow Icon
10 What is the minimum height of a binary tree with n nodes? What is the maximum height of a binary tree with n nodes? What is the maximum number of nodes at level i ? Binary trees n-1 b lg n c 2 i
Background image of page 10
11 Binary tree terminology A binary tree is full if every node has exactly 0 or 2 children A binary tree is perfect if every level i has exactly 2 i nodes A binary tree is complete if it is perfect except for the last level, which is filled left-to-right
Background image of page 11

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

View Full DocumentRight Arrow Icon
12 Binary tree traversals Pre-order: “Visit” root first, then left child, then right child In-order: Visit left child, then root, then right child Post-order: Visit left child, then right child, then root Level-order: Visit all siblings of a level from left to right, at increasing depth starting from the root
Background image of page 12
13 A pre-order traversal Node order: Running time: void visit() { System.out.print(key); if (n.leftChild != null) n.leftChild.visit(); if (n.rightChild != null) n.rightChild.visit(); } 42 25 12 9 17 37 29 46 50 71 47
Background image of page 13

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

View Full DocumentRight Arrow Icon
14 An in-order traversal Node order: Running time: void visit() { if (n.leftChild != null) n.leftChild.visit(); System.out.print(key); if (n.rightChild != null) n.rightChild.visit(); } 42 25 12 9 17 37 29 46 50 71 47
Background image of page 14
15 A post-order traversal Node order: Running time: void visit() { if (n.leftChlid != null) n.leftChild.visit(); if (n.rightChild != null) n.rightChild.visit(); System.out.print(key); } 42 25 12 9 17 37 29 46 50 71 47
Background image of page 15

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

View Full DocumentRight Arrow Icon
16 A level-order traversal
Background image of page 16
Image of page 17
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 05/10/2011.

Page1 / 67

10 Binary Search Trees - Binary Search Trees 15-211:...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online