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

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Advertisements

CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
EC-211 DATA STRUCTURES LECTURE Tree Data Structure Introduction –The Data Organizations Presented Earlier are Linear in That Items are One After.
CS 171: Introduction to Computer Science II
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
Binary Trees Chapter 6.
Joseph Lindo Trees Sir Joseph Lindo University of the Cordilleras.
TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree Data Structures.
Trees Dr. Andrew Wallace PhD BEng(hons) EurIng
Binary Trees. 2 Parts of a binary tree A binary tree is composed of zero or more nodes In Java, a reference to a binary tree may be null Each node contains:
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Binary Trees. 2 Parts of a binary tree A binary tree is composed of zero or more nodes In Java, a reference to a binary tree may be null Each node contains:
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
Chapter 6 – Trees. Notice that in a tree, there is exactly one path from the root to each node.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
(c) University of Washington20-1 CSC 143 Java Trees.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Binary Trees.
CSE 373 Data Structures Lecture 7
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Trees Chapter 15.
Binary Trees.
Fundamentals of Programming II Introduction to Trees
Recursive Objects (Part 4)
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Section 8.1 Trees.
Trees.
Data Structures & Algorithm Design
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Depict the Tree Structure in a picture
CS223 Advanced Data Structures and Algorithms
Binary Trees.
slides created by Alyssa Harding
Trees.
Binary Trees.
Design and Analysis of Algorithms
Binary Trees, Binary Search Trees
Binary Trees.
CSC 143 Java Trees.
Chapter 20: Binary Trees.
Binary Trees.
Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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?

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

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

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); } theRoot find(theRoot, 3); find(theRoot, 5); find(theRoot, 8);

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.

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

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? theRoot

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

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

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); }

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); }

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); }

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

Questions? Image copyright © 2010 andyjphoto.com