Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees.

Similar presentations


Presentation on theme: "Trees."— Presentation transcript:

1 Trees

2 What is a Tree? 10/18/2019

3 What is a Tree? Trees are an important data structure
They organize data hierarchically They are used to model hierarchical data in the real world store and retrieve data 10/18/2019

4 A hierarchical organization of a hypothetical company
Computers”R”Us Sales R&D Manufacturing Laptops Desktops Local International Europe Asia Africa A hierarchical organization of a hypothetical company 10/18/2019

5 Tree Terminology Node Child Parent Data stored in the tree
A node descended from another node Parent The node from which another node descended a c b a is the parent of both b & c b & c are children of a 10/18/2019

6 Tree Terminology Root The node from which all others descend Branch
Joins nodes Leaf (External node ) A node without children Degree=3 Maximum # of children Siblings Children of a common parent 10/18/2019

7 Tree Terminology Ancestors of a node Descendant of a node
Any node from which a node descended (parent, grandparent, grand-grandparent, etc). Descendant of a node Any child, grandchild, grand-grandchild, etc. Depth of a node Number of ancestors 10/18/2019

8 Tree Terminology Height of a tree Subtree Path
Maximum depth of any node Subtree Tree consisting of a node and its descendants Path The set of branches connecting an ancestor to a descendant 10/18/2019

9 Tree Terminology Depth = 0 Depth = 2 Depth = 3 Subtree 10/18/2019

10 Trees vs. Graphs A tree Cannot contain cycles Each node has 1 parent or it is at the root Any structure which does not meet these criteria is a graph The structures to the right are graphs 10/18/2019

11 Review: Recursive definition of list
Empty list (0 nodes) OR Head node and 0 or 1 non-empty sublist 10/18/2019

12 Recursive definition of Tree
Empty tree (0 nodes) OR Root node and 0 or more non-empty subtrees (child node and its descendents) 10/18/2019

13 Tree Traversal To traverse any data structure means to visit every node in turn in a systematic manner 10/18/2019

14 Review: List Traversal
List – process sequentially by iteration 2. recursion 10/18/2019

15 Tree Traversal Pre-order In-order Post-order 10/18/2019

16 Pre-order Traversal Recursive definition: Visit root before children
1 2 5 9 3 6 11 8 10 7 4 Recursive definition: Visit root before children Algorithm preOrder(v) visit(v) for each child w of v preOrder (w) 10/18/2019

17 Post-order Traversal Recursive definition: Visit root after children
11 2 10 5 1 4 9 6 7 8 3 Recursive definition: Visit root after children Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) 10/18/2019

18 Traversal example 7 Pre-order: 7 4 3 5 12 9 Post-order: 3 5 4 9 12 7 4
10/18/2019

19 Implementing trees List Tree class ListNode Node: data & reference
Node: data & references class ListNode {int data; ListNode next; } class TreeNode TreeNode child1, child2, child3; 10/18/2019

20 Implementing trees Problem of representing a node:
How many edges to allow for? e.g., Class TreeNode { int data; TreeNode child1, child2, child3; } 10/18/2019

21 Implementing trees Problem of representing a node: Three solutions:
allow lots of child edge references restrict tree structure to two child edges (binary tree) use first child/next sibling representation 10/18/2019

22 First child/next sibling
Class TreeNode { int data; TreeNode child, sibling; } Problem: Makes path to most nodes longer 10/18/2019


Download ppt "Trees."

Similar presentations


Ads by Google