Télécharger la présentation
## Trees

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**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 • 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)**Tree Terminology and Applications**Section 6.1**Node**dog cat wolf canine**Root**The node at the very top of a tree is called its root dog cat wolf canine**Branches**The links from a node to its successors are called branches dog cat wolf canine**Children**The successors of a node are called its children dog cat wolf canine**Parent**The predecessor of a node is called its parent dog cat wolf canine**Parent uniqueness**Each node in a tree has exactly one parent except for the root node, which has no parent dog cat wolf canine**Siblings**Nodes that have the same parent are siblings dog cat wolf canine**Leaf**A node that has no children is called a leaf dog cat wolf canine**Internal and external nodes**External nodes-leaf nodes Internal nodes-non leaf nodes dog cat wolf canine**Ancestor-descendant relationship**A generalization of the parent-child relationship is the ancestor-descendant relationship dog cat wolf canine**Ancestor-descendant relationship-1**dog dog is the parent of cat in this tree cat wolf canine**Ancestor-descendant relationship-2**dog cat is the parent of canine in this tree cat wolf canine**Ancestor-descendant relationship-3**dog cat wolf canine is a descendant of cat in this tree canine**Ancestor-descendant relationship-4**dog dog is an ancestor of canine in this tree cat wolf canine**Subtree**dog cat wolf canine A subtree of a node is a tree whose root is a child of that node**Subtree-1**dog cat wolf canine**Subtree-2**dog cat wolf canine**The level of a node**The level of a node is determined by its distance from the root dog cat wolf canine**Level example**Distance from the root plus 1 dog Level 1 cat wolf Level 2 canine Level 3**Level is defined recursively**dog Level 1 cat wolf Level 2 canine Level 3**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.**Example**Examples: d : 10110 e : 010**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**Search Performance**average O(log n) Worst case O(n)**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**General Trees**Can have any number of subtrees**General Tree and Binary Tree**A general tree can be represented using a binary tree**Tree Traversals**Section 6.2**Tree Traversals**• Tree traversal • Walking through the tree in a prescribed order • Three types of traversal • Inorder • Preorder • Postorder**Three types of Tree Traversals**• Preorder • RootTLTR • Inorder • TLrootTR • Postorder • TLTRroot**Euler Tour**Keep the tree always at the left**Euler Tour**• Preorder traversal • a b d g e h c f i j**Inorder Traversal**The sequence isd g b h e a i f j c