Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.

Similar presentations


Presentation on theme: "CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com."— Presentation transcript:

1 CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

2 CS-2852 Data Structures, Andrew J. Wozniewicz Agenda Tree Overview Binary Tree – Binary Search Tree Searching Traversals – Pre-Order – In-Order – Post-Order

3 CS-2852 Data Structures, Andrew J. Wozniewicz Trees Leaves Branches Root COMPUTER SCIENCE Root Branches Leaves NATURE

4 CS-2852 Data Structures, Andrew J. Wozniewicz Trees COMPUTER SCIENCE Leaves Branches Root

5 What is a Tree? Vice- President Team Lead SE1 SE2 SE3 Team Lead SE4 SE5 Manager BA1 BA2 Consultant

6 What is a Tree? Vice- President Team Lead SE1 SE2 SE3 Team Lead SE4 SE5 Manager BA1 ConsultantSecretary ROOT

7 What is a Tree? Vice- President Team Lead SE1 SE2 SE3 Team Lead SE4 SE5 Manager BA1 ConsultantSecretary ROOT CHILD

8 What is a Tree? Vice- President Team Lead SE1 SE2 SE3 Team Lead SE4 SE5 Manager BA1 ConsultantSecretary ROOT CHILD LEAVES

9 CS-2852 Data Structures, Andrew J. Wozniewicz What is a Tree? There is exactly one (i.e. unique) path from the root to each node. There is exactly one path from each node to the root. There is exactly one path between any nodes. Each node can have arbitrarily many children. Each Child has exactly one Parent

10 CS-2852 Data Structures, Andrew J. Wozniewicz What is a Tree? Like Linked Lists, Trees are “linked” data structures. Hierarchical, rather than linear chaining. A node can be a “child” of another node; the only node that is not a “child” is the “root”. Nodes with children are referred to as “Parents”.

11 CS-2852 Data Structures, Andrew J. Wozniewicz Height of a Tree Depth (level): Measures the distance from its root – Root node has level 1 – Any other node’s level is the level of its parent + 1 Number of nodes in the longest path from the root node to a leaf node. DEFINITION

12 CS-2852 Data Structures, Andrew J. Wozniewicz Tree Terminology Node (External/Internal) Root, Parent, Child Ancestor, Descendant, Sibling Branch, Subtree

13 Binary Tree Hierarchy of data with some constraints A Root node 0-2 children – Left Child – Right Child Each child is itself a tree

14 CS-2852 Data Structures, Andrew J. Wozniewicz (Binary) Tree Node protected static class Node { protected E data; protected Node left; protected Node right; public Node(E data) { this.data = data; this.left = null; this.right = null; } What does it resemble?

15 CS-2852 Data Structures, Andrew J. Wozniewicz Binary Search Tree Same structural rules as Binary Tree… A sorted hierarchy of data – Left child less than parent – Right child greater than parent 13 2 4 57 6 26 1 3 7 5

16 CS-2852 Data Structures, Andrew J. Wozniewicz Adding Data Recursive algorithm: Case 1: Empty Tree – Becomes the root node Case 2: Smaller Value – Recursively add to left Case 3: Larger Value – Recursively add to right Case: Equal Value – Treat as larger value 1 2 4 7 6 26 1 7 4 4

17 CS-2852 Data Structures, Andrew J. Wozniewicz Searching Node find(Node root, E value) { if (root == null) return null; if (root.value==value) return root; if (value < root.value) return find(current.left, value); return find(current.right,value); } 13 2 4 57 6 theRoot find(theRoot, 3); find(theRoot, 5); find(theRoot, 8);

18 CS-2852 Data Structures, Andrew J. Wozniewicz Advantages of Trees Don’t ever have to examine every node to determine if a value is in. – Unlike in a Linked List Only looking at a subset of the data in the tree The largest possible number of comparisons is equal to the height of the tree.

19 CS-2852 Data Structures, Andrew J. Wozniewicz Full, Perfect, and Complete BT Full: – All nodes have either 0 or 2 children Perfect – Full, with 2 height -1 nodes Complete – Perfect through level height-1

20 CS-2852 Data Structures, Andrew J. Wozniewicz Linked Structure Traversals Linked List, Array: – Left-to-Right – Right-to-Left Stack – Last-In, First-Out Queue – First-In, First-Out Tree? – Left-First? – Right-First? 13 2 4 57 6 theRoot

21 CS-2852 Data Structures, Andrew J. Wozniewicz Tree Traversals Enumerate nodes in a well-known order… Basic algorithm: – Process Current Node – Visit Left – Visit Right What varies is the order No particular Order Implied

22 CS-2852 Data Structures, Andrew J. Wozniewicz Three Common Traversal Orders Pre-Order – Process Current – Visit Left Child – Visit Right Child In-Order – Vist Left Child – Process Current – Visit Right Child Post-Order – Visit Left Child – Visit Right Child – Process Current Child

23 CS-2852 Data Structures, Andrew J. Wozniewicz Pre-Order Traversal void visit(Node current) { if (current==null) return; process(current.value); visit(current.left); visit(current.right); } 4 2 1 3 8 6 5 7

24 CS-2852 Data Structures, Andrew J. Wozniewicz In-Order Traversal void visit(Node current) { if (current==null) return; visit(current.left); process(current.value); visit(current.right); } 1 2 3 4 5 6 7 8

25 CS-2852 Data Structures, Andrew J. Wozniewicz Post-Order Traversal void visit(Node current) { if (current==null) return; visit(current.left); visit(current.right); process(current.value); } 1 3 2 5 7 6 8 4

26 CS-2852 Data Structures, Andrew J. Wozniewicz Summary What is a Tree? Binary Tree – Binary Search Tree Searching Traversals – Pre-Order – In-Order – Post-Order

27 Questions? Image copyright © 2010 andyjphoto.com


Download ppt "CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com."

Similar presentations


Ads by Google