notes76

notes76 - Class notes (Feb 7th, 9th, 11th) Binary Trees! A...

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

View Full Document Right Arrow Icon
Class notes (Feb 7th, 9th, 11th) Binary Trees!! A binary tree is an ordered tree with all internal nodes of degree 2 (each node has 2 children). Note: For this course, the leaf nodes don't contain any information and may be represented as NULL. -> NULL -> Leaf Node Properties of Binary Trees - The number of external nodes is equal to the number of internal nodes + 1 "an internal node is a node that is not a n external node. ." -The number of nodes at level i = 2 ^ i (at most) - The number of external nodes <= 2^(height) - log 2 (# of external nodes) <= height Balanced Binary Trees -A balanced binary tree is a tree where the level of the external nodes differ at most by 1. (Picture 1 is a balanced tree) Why should we have balanced binary trees? -Because searching takes O(log n ). Where n is the # of nodes and log n is the height of the tree.
Background image of page 1

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

View Full DocumentRight Arrow Icon
In an unbalanced tree it could potentially take O(n) time. (This will happen when the nodes are added to only 1 side of the tree.) Example of a unbalanced binary tree: Implementation of an un balanced binary tree: struct BinaryNode{ char * _name; BinaryNode * _left; BinaryNode * _right; void * _data; }; _name is the name of a student _data is the data associated with the student we can use the binary tree to get the data of a student given the name. class BinaryTree { BinaryNode *_root; public: BinaryTree(); bool addNode (char * name, void * data); //add a node to the tree //return false if the name did not exist previously
Background image of page 2
//return true otherwise (and set the data) void * findNode (char * name); //returns data associated with name //or NULL if not found. BinaryTree::BinaryTree()
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
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.

Page1 / 11

notes76 - Class notes (Feb 7th, 9th, 11th) Binary Trees! A...

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

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