# Trees

Télécharger la présentation

## Trees

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Trees Chapter 6

2. Chapter Objectives • Tree structure • Tree recursion • Tree traversing • Tree examples • binary trees • binary search trees • heaps • Tree implementation • linked data structures • arrays

3. Chapter Objectives (cont.) • Binary search tree • Information store and retrieve efficiently • Huffman tree • reduced storage

4. What is a Tree? • Linear structure • one predecessor and successor • Accessing in O(n) • Trees • Nonlinear • Hierarchical • One predecessor • Multiple successors

5. How to use Tree structure? • Represent hierarchy • class hierarchy • disk directory and subdirectories • family tree • State organization • Process trees recursively • recursive data structures

6. Binary Tree • Each element has two successors • Be represented by • Arrays • Linked data structures • Searching • O(log n) versus O(n)

7. Tree Terminology and Applications Section 6.1

8. Node dog cat wolf canine

9. Root The node at the very top of a tree is called its root dog cat wolf canine

10. Branches The links from a node to its successors are called branches dog cat wolf canine

11. Children The successors of a node are called its children dog cat wolf canine

12. Parent The predecessor of a node is called its parent dog cat wolf canine

13. Parent uniqueness Each node in a tree has exactly one parent except for the root node, which has no parent dog cat wolf canine

14. Siblings Nodes that have the same parent are siblings dog cat wolf canine

15. Leaf A node that has no children is called a leaf dog cat wolf canine

16. Internal and external nodes External nodes-leaf nodes Internal nodes-non leaf nodes dog cat wolf canine

17. Ancestor-descendant relationship A generalization of the parent-child relationship is the ancestor-descendant relationship dog cat wolf canine

18. Ancestor-descendant relationship-1 dog dog is the parent of cat in this tree cat wolf canine

19. Ancestor-descendant relationship-2 dog cat is the parent of canine in this tree cat wolf canine

20. Ancestor-descendant relationship-3 dog cat wolf canine is a descendant of cat in this tree canine

21. Ancestor-descendant relationship-4 dog dog is an ancestor of canine in this tree cat wolf canine

22. Subtree dog cat wolf canine A subtree of a node is a tree whose root is a child of that node

23. Subtree-1 dog cat wolf canine

24. Subtree-2 dog cat wolf canine

25. The level of a node The level of a node is determined by its distance from the root dog cat wolf canine

26. Level example Distance from the root plus 1 dog Level 1 cat wolf Level 2 canine Level 3

27. Level is defined recursively dog Level 1 cat wolf Level 2 canine Level 3

28. 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

29. Height the number of nodes in the longest path from the root to a leaf dog cat wolf canine

30. Height example dog cat wolf The height of this tree is 3 canine

31. 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

32. 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)

33. Huffman Tree? • Represents Huffman codes • Huffman code • different numbers of bits to encode letters • fewer bits for high frequently used letters • Compressing files

34. 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.

35. Example Examples: d : 10110 e : 010

36. 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

37. Features • Sorted • Order is maintained • Add a new element • Remove an element

38. Search Performance average O(log n) Worst case  O(n)

39. 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

40. 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

41. Perfect Binary Trees Full binary tree Height n with exactly 2n – 1 nodes 3 1 5 0 2 4 6

42. 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

43. General Trees Can have any number of subtrees

44. General Tree and Binary Tree A general tree can be represented using a binary tree

45. Tree Traversals Section 6.2

46. Tree Traversals • Tree traversal • Walking through the tree in a prescribed order • Three types of traversal • Inorder • Preorder • Postorder

47. Three types of Tree Traversals • Preorder • RootTLTR • Inorder • TLrootTR • Postorder • TLTRroot

48. Euler Tour Keep the tree always at the left

49. Euler Tour • Preorder traversal • a b d g e h c f i j

50. Inorder Traversal The sequence isd g b h e a i f j c