Trees. Chapter 6. Chapter Objectives. Tree structure Tree recursion Tree traversing Tree examples binary trees binary search trees heaps Tree implementation linked data structures arrays. Chapter Objectives (cont.). Binary search tree Information store and retrieve efficiently
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation
Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement.Téléchargez la présentation en cliquant sur ce lien.Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.
Chapter Objectives • Tree structure • Tree recursion • Tree traversing • Tree examples • binary trees • binary search trees • heaps • Tree implementation • linked data structures • arrays
Chapter Objectives (cont.) • Binary search tree • Information store and retrieve efficiently • Huffman tree • reduced storage
What is a Tree? • Linear structure • one predecessor and successor • Accessing in O(n) • Trees • Nonlinear • Hierarchical • One predecessor • Multiple successors
How to use Tree structure? • Represent hierarchy • class hierarchy • disk directory and subdirectories • family tree • State organization • Process trees recursively • recursive data structures
Binary Tree • Each element has two successors • Be represented by • Arrays • Linked data structures • Searching • O(log n) versus O(n)
Recursive definition of level • If node n is the root of tree T, its level is 1 • If node n is not the root of tree T • its level is 1 + the level of its parent dog Level 1 cat wolf Level 2 canine Level 3
Height the number of nodes in the longest path from the root to a leaf dog cat wolf canine
Height example dog cat wolf The height of this tree is 3 canine
Binary Trees • Each node has two subtrees • T is a binary tree- either is true • T is empty • Its root node has two subtrees, TL and TR • TL and TR are binary trees
Expression Tree • Internal node • operator • Leaf node • Operands • Parentheses • stored in the tree structure • Evaluation • from bottom to top • from Left to right (x + y) * ((a + b) / c)
Huffman Tree? • Represents Huffman codes • Huffman code • different numbers of bits to encode letters • fewer bits for high frequently used letters • Compressing files
Huffman Tree To form a code, traverse the tree from the root to the chosen character, appending 0 if you turn left, and 1 if you turn right.
Binary Search Tree • Binary search trees • TL precedes TR • A formal definition: T is a binary search tree with either true • T is empty • If T is not empty,TLand TR are binary search trees • TL< root <TR dog cat wolf canine
Features • Sorted • Order is maintained • Add a new element • Remove an element
Recursive Algorithm for Searching a Binary Tree if the tree is empty return null (target is not found)else if the target matches the root node's data return the data stored at the root nodeelse if the target is less than the root node's data return the result of searching the left subtree of the rootelse return the result of searching the right subtree of the root
Full Binary Tree All nodes have either 2 children or 0 children 7 1 2 4 5 6 10 0 3 9 12 11 13
Perfect Binary Trees Full binary tree Height n with exactly 2n – 1 nodes 3 1 5 0 2 4 6
Complete Binary Tree A perfect binary tree through level n – 1 Extra leaf nodes at level n All toward the left 3 1 5 0 2 4