30 general and binary trees
play

30: General and Binary Trees Chris Wyatt Electrical and Computer - PowerPoint PPT Presentation

ECE 2574 Introduction to Data Structures and Algorithms 30: General and Binary Trees Chris Wyatt Electrical and Computer Engineering Trees are non-linear, value oriented structures List Holding 7 Items A B C D E F G A B C Example of


  1. ECE 2574 Introduction to Data Structures and Algorithms 30: General and Binary Trees Chris Wyatt Electrical and Computer Engineering

  2. Trees are non-linear, value oriented structures List Holding 7 Items A B C D E F G A B C Example of a Tree Holding 7 Items, a D E Hierarchy G F

  3. Tree Terminology Tree A Node (vertex) Links (edges) B C Parent Child D E Sibling Root G F Leaf Ancestor Descendent

  4. Tree Terminology A Subtree B C E Binary Tree M-ary Tree G D F General Tree A A B C B C D D E D E F G F

  5. Formal Definition of a Binary Tree A Binary Tree T is a set of nodes such that T T is empty R T is partitioned into three subsets: 1. A single node R, the root L R Two, possible empty sets forming binary trees 2. the left subtree 3. the right subtree

  6. Binary Tree Terminology Path Height Full Tree Complete Tree Balanced Tree A A A B C B B C C G D G D E F D E F

  7. The Binary Tree ADT A B D E A attach right B C subtree G D E F A C detach left subtree G F

  8. Traversals of Binary Trees Preorder traversal if T is not empty visit the root of T preorder traverse left subtree of T preorder traverse right subtree of T A B C G D E F

  9. Traversals of Binary Trees Inorder traversal if T is not empty inorder traverse left subtree of T visit the root of T inorder traverse right subtree of T A B C G D E F

  10. Traversals of Binary Trees Postorder traversal if T is not empty postorder traverse left subtree of T postorder traverse right subtree of T visit the root of T A B C G D E F

  11. Examples

  12. In class exercise What is the preorder, inorder, and postorder traversals of the following Binary Tree A B C G D E F I H

  13. What are trees good for? Parsing and representing relationships

  14. What are trees good for? Representing Hierarchies

  15. What are trees good for? Modeling decisions State space for NIM game, 7 tokens 7 6-1 5-2 4-3 5-1-1 4-2-1 3-2-2 3-3-1 4-1-1-1 3-2-1-1 2-2-2-1 3-1-1-1-1 2-2-1-1-1 2-1-1-1-1-1

  16. What are trees good for? Organization and searching

  17. Representing Binary Trees Array based implementation for complete trees A 0 1 2 3 4 5 B C A B C D E F D E F Why does this not work for non-complete trees?

  18. Representing Binary Trees List based representation (not in your text) Consider a list with contents given by a pair (tuple) of and item (atom) followed by a list. A struct list B C { item a; D ( A ( ( B ( D ( ) ) ) ( C ( ) ) ) ) list l; }

  19. Representing Binary Trees Pointer based implementation, an extension of a linked list struct node A { B C item a; node * left; D node * right; }

  20. Representing Binary Trees struct node { item a; node * left; node * right; }

  21. Next Actions and Reminders Read CH pp. 442-449 Program 4 is due 11/17

Recommend


More recommend