CS2420: Lecture 15 Vladimir Kulyukin Computer Science Department Utah State University
Outline Trees (Chapter 4)
Binary Tree Heights and Leaves For any binary tree with L leaves and height H
Binary Tree Properties Property 1: A binary tree with N > 0 nodes has exactly N-1 edges. Proof: Every node, except the root, receives exactly one edge from its parent.
Binary Tree Properties Property 2: If T is a binary tree of height h≥0, T has at least h+1 and at most 2 h nodes. Proof: Since T is of height h, T has h+1 levels. There must be at least one node on each level. So the number of nodes is at least h+1. Each level j has at most 2 j nodes. Thus, the number of nodes is at most 2 h+1 – 1.
Property 2: Lower Bound
Property 2: Upper Bound
Property 2: Upper and Lower Bounds on N
Binary Tree Properties
Binary Tree: Formula-Based Representation Formula-based representation –This assumes that the array counting starts with 1. –Parent(i) = i/2 –LeftChild(i) = 2i –RightChild(i) = 2i+1
Binary Tree: Linked Representation Linked representation –Each node is a structure containing data, and two pointers (LeftChild, RightChild).
BT Node: 2 Node Structures KEYDATA LEFT CHILD PTR RIGHT CHILD PTR KEY= DATA LEFT CHILD PTR RIGHT CHILD PTR