# notes75 - CS 251 Lecture Note 2/7/2005 - 2/9/2005 Binary...

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

CS 251 Lecture Note 2/7/2005 - 2/9/2005 Binary Trees Ordered tree with all internal nodes of degree equal to 2. Binary Tree (height = 4) - interval nodes - leaf nodes Not a binary tree. (Why? C is internal node and the degree is 1) Note: For this course, the leaf nodes (marked as square) do not contain any

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

View Full Document
information and may be represented as NULL. -> NULL -> leaf node Binary Trees i. # of external nodes(leaf) = # of internal nodes + 1 ii. # of nodes at lever i <= 2 i iii. # of external nodes <= 2 height (height is the max level in the tree.) log 2 (#of external nodes) <= log 2 2 height iv. log 2 (#of external nodes) <= height From i substituting in iv, v. log 2 (#of internal nodes + 1) <= height Balanced Tree - It is a tree where the level of the external nodes differ at most by 1. The external nodes have a level that differ at most by 1. => Balanced Binary Tree Binary Tree. Not Balanced Binary Tree. The external nodes have a level that differs by more than 1. largest level=3 smallest level=1
Therefore, not a balanced binary tree. o Searching an item in a balanced binary tree takes O(log n) where n is the number of nodes and log n is the height of the tree. Searching takes time proportional to the length of the path from the root to the external nodes. o If the tree is unbalanced, then searching an item may take O(n) and this will happen when the nodes are added to only one side(left or right) of the tree. In this case the tree will look like a list. Search is O(n) (unbalanced tree) Search is O(log n)

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

View Full Document
Implementation of a non-balanced binary tree - We will see later how to balance binary tree. Struct Binary Node { //Implementation of a binary tree of names. char *_name; void *_data; BinaryNode *_left; BinaryNode *_right; }; _name -Name of a student. _data -Data associated to that student such as grade, schedule etc. We can use the binary tree to get the data of a student given the name. class BinaryTree { BinaryNode *_root; public: BinaryTree();
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/02/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue University-West Lafayette.

### Page1 / 14

notes75 - CS 251 Lecture Note 2/7/2005 - 2/9/2005 Binary...

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

View Full Document
Ask a homework question - tutors are online