Presentation is loading. Please wait.

Presentation is loading. Please wait.

This Lecture Intro to Tree ADT Terminologies Tree Types Tree Traversals Binary Search Tree Expression Trees.

Similar presentations


Presentation on theme: "This Lecture Intro to Tree ADT Terminologies Tree Types Tree Traversals Binary Search Tree Expression Trees."— Presentation transcript:

1 This Lecture Intro to Tree ADT Terminologies Tree Types Tree Traversals Binary Search Tree Expression Trees

2 Tree a set of leaves connected by branches – you can get to any leaf by following some path from the root a set of nodes connected by edges the first node is the root interior nodes provide information and links

3 Tree Usually diagrammed upside down There is only one root node Information is stored in the nodes The edges represent some relationship between the nodes

4 Terminology parent children siblings descendents ancestors leaf node non-leaf node subtree path  direct ancestor  direct descendents  have same parent  children & children's children  parent & parent's parents  no children  has children  node and its descendents  nodes and edges to a node

5 Diagrams L treeR tree node L edgeR edge subtree

6 Familiar Uses Organizational Chart Family Tree Inheritance Hierarchy

7 Organization Chart Shows the personnel of a company – Each node represents a person or title – Each edge indicates supervisor / subordinate relationship

8 Widgets, Inc President / CEO VP SalesVP Service Human Resources Mgr Eastern Region Mgr Western Region Mgr Telephone Support Mgr Returns/Repairs Agents VP Research & Development Project Manger Agents Engineers Technicians Documentation Testing Operators Technicians height = 4

9 Family Tree Nodes are individuals Edges represent the parent / child relationship Siblings have the same parent

10 My Ancestors Me MotherFather Maternal Grandmother Maternal Grandfather Paternal Grandmother Paternal Grandfather Great Grandmother Great Grandfather Great Grandmother Great Grandfather Great Grandmother Great Grandfather Great Grandmother Great Grandfather

11 Descendents Great Grandparents DaughterSon Grand child Son-in-law Daughter-in-law Son Daughter-in-law Grand child great grandchild

12 Inheritance Hierarchy Object Throwable Component Comparable Exception Error Frame Window

13 A Tree of Trees Tree Examples Organization Chart Family Tree Inheritance Hierarchy

14 Not so Familiar Uses Algebraic Expressions Decision trees (flow chart) – Binary Search Trees Parse trees (based on grammar) Game trees (possible moves)

15 Which nodes are … leaf nodes? siblings of G? ancestors of L? descendents of D? children of I? A CD FIE B RSP GH T ML O KJ Q N

16 More Terms height length of path left child right child left subtree right subtree  number of levels  1 + height of tallest subtree  number of edges of the path  first child  second or last child  left child & its descendents  right child & its descendents

17 Tree Types General n-ary Binary – Binary Search Tree (bst) – Expression – Parse – Heap

18 General any number of children per node no max A CD FIE B RSP GH T ML O KJ Q N

19 n-ary no more than n children per node 3-ary A CD FIE B RSP GH T ML O KJ Q N

20 Binary Trees very common at most 2 children per node each subtree is also a binary tree Full - when every node in all levels, except the last level, has 2 children. Nodes in last level have 0 children Complete - full to next to last level & last level is filled from left to right Other - not full or complete

21 Full, Complete or Other? A CD FIE B RSP GH T ML O KJ Q N not binary

22 Full, Complete or Other? A D FI B RSP GH T ML O K N

23 A D F I B R S P G H T M L O K N

24 A D F I B R S P G H T M L O K N

25 A D F I B R S P G H T M L O K N

26 A D F I B Q S R G H T M L P K N O

27 A D F I B Q S R G H T M L P K N O

28 A D F I B QR G H M L P K N O

29 Traversals Visit each node only once (process data in the node) – Which order? – How do we keep track? Mark the node or Distinguish subtrees from the root and visit based on an algorithm 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

30 Preorder Traversals 1.Visit the root 2.Visit Left subtree 3.Visit Right subtree A D F I B R S P G H T M L O K N 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

31 Inorder Traversals 1.Visit Left subtree 2.Visit the root 3.Visit Right subtree A D F I B R S P G H T M L O K N 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 1 1

32 Postorder Traversals 1.Visit Left subtree 2.Visit Right subtree 3.Visit the root A D F I B R S P G H T M L O K N 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 2 2 3

33 Level Order Traversals 1.Visit the root 2.Visit root of Left subtree 3.Visit root of Right subtree 4.??? B H M N A D F I B R S P G H T M L O K N 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 2 1718 19 20

34 Level Order Traversal w/Queue 1.Enqueue the root node 2.while queue is not empty a)Dequeue the front of queue to current b)Visit current node c)Enqueue all children of current

35 Binary Search Tree special type of binary tree node stores one Comparable data item left subtree (edge) is "less than" right subtree (edge) is "greater than"

36 Example of Binary Search Tree 4 6 1 2 5 3 Full, Complete, or Other? Which traversal returns items in sorted order?

37 3 5 1 2 4 6 Full, Complete, or Other? Example of Binary SearchTree

38 6 4 1 2 3 5 Full, Complete, or Other? Example of Binary Search Tree

39 1 4 6 2 3 5 Full, Complete, or Other?


Download ppt "This Lecture Intro to Tree ADT Terminologies Tree Types Tree Traversals Binary Search Tree Expression Trees."

Similar presentations


Ads by Google