This preview shows pages 1–14. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Chapter 8: Trees 1 Trees Chapter 8: Trees 2 Chapter Outline How trees organize information hierarchically Using recursion to process trees The different ways of traversing a tree Binary trees, Binary Search trees, and Heaps Implementing binary trees, binary search trees, heaps Using linked data structures and arrays Using binary search trees for efficient data lookup Using Huffman trees for compact character storage Chapter 8: Trees 3 Tree Terminology A tree is a collection of elements (nodes) Each node may have 0 or more successors (Unlike a list, which has 0 or 1 successor) Each node has exactly one predecessor Except the starting / top node, called the root Links from node to its successors are called branches Successors of a node are called its children Predecessor of a node is called its parent Nodes with same parent are siblings Nodes with no children are called leaves Chapter 8: Trees 4 Tree Terminology (2) We also use words like ancestor and descendent Chapter 8: Trees 5 Tree Terminology (3) Subtree of a node: A tree whose root is a child of that node Depth of a node: A measure of its distance from the root: Depth of the root = 0 Depth of other nodes = 1 + depth of parent Chapter 8: Trees 6 Tree Terminology (4) Depth 0 (root) Depth 1 Depth 1 Depth 2 Chapter 8: Trees 7 Binary Trees Binary tree: a node has at most 2 nonempty subtrees Set of nodes T is a binary tree if either of these is true: T is empty Root of T has two subtrees, both binary trees (Notice that this is a recursive definition) This is a binary tree Chapter 8: Trees 8 Examples of Binary Trees Expression tree Nonleaf ( internal ) nodes contain operators Leaf nodes contain operands Huffman tree Represents Huffman codes for characters appearing in a file or stream Huffman code may use different numbers of bits to encode different characters ASCII or Unicode uses a fixed number of bits for each character Chapter 8: Trees 9 Examples of Binary Trees (2) Chapter 8: Trees 10 Examples of Binary Trees (3) Code for b = 100000 Code for w = 110001 Code for s = 0011 Code for e = 010 Chapter 8: Trees 11 Examples of Binary Trees (4) Binary search trees Elements in left subtree < element in subtree root Elements in right subtree > element in subtree root Search algorithm: 1. if the tree is empty, return NULL 2. if target equals to root nodes data, return that data 3. if target < root node data, return search(left subtree) 4. otherwise return search(right subtree) Chapter 8: Trees 12 Fullness and Completeness (In computer science) trees grow from the top down New values inserted in new leaf nodes In a full tree, every node has 0 or 2 nonnull children A complete tree of height h is filled up to depth h1, and, at depth h , any unfilled nodes are on the right. Chapter 8: Trees 13 Fullness and Completeness (2) A binary tree is complete...
View Full
Document
 Spring '10
 MITIN

Click to edit the document details