10 Binary Search Trees

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

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

Binary Search Trees 15-211: Fundamental Data Structures and Algorithms Charlie Garrod 16 February 2010 Reading for today: 19.1 – 19.5

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

View Full Document
2 Announcements HW3 is out! Theory due in class next Tuesday No quiz tomorrow!
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

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

View Full Document
4 Today: Binary Search Trees (BSTs) A quick tree review Binary tree traversals Binary Search Trees Basic BST operations Balanced BSTs AVL trees
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

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

View Full Document
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
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

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

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

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
16 A level-order traversal
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
Ask a homework question - tutors are online