Trees.

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

Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Computer Science C++ High School Level By Guillermo Moreno.
Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CS 171: Introduction to Computer Science II
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Lecture 17 Non-Linear data structures Richard Gesick.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
Emma Price 1.  To be able to:  Explain what a binary tree is.  To traverse a binary tree using the three different methods. 2.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
Discrete Structures Trees (Ch. 11)
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.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
Binary Search Trees (BST)
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
Lecture 17: Trees and Networks I Discrete Mathematical Structures: Theory and Applications.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Hello Everyone!!! 1. Tree And Graphs 2 Features of Trees  Tree Nodes Each node have 0 or more children A node have must one parent  Binary tree Tree.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
(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.
Topic 2: binary Trees COMP2003J: Data Structures and Algorithms 2
Planning & System installation
CSCE 210 Data Structures and Algorithms
Chapter 25 Binary Search Trees
Fundamentals of Programming II Introduction to Trees
Exam information in lab Tue, 18 Apr 2017, 9:00-noon programming part
Recursive Objects (Part 4)
Binary search tree. Removing a node
Planning & System installation
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
Trees.
Chapter 20: Binary Trees.
Data Structures and Database Applications Binary Trees in C#
CS223 Advanced Data Structures and Algorithms
Chapter 21: Binary Trees.
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Abstract Data Structures
Trees.
Binary Trees.
Trees Lecture 9 CS2110 – Fall 2009.
Trees Definitions Implementation Traversals K-ary Trees
CSC 143 Java Trees.
Chapter 20: Binary Trees.
Trees.
Non-Linear data structures
Binary Tree Implementation And Applications
Data Structures Using C++ 2E
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

Trees

Graphs a graph is a data structure made up of nodes each node stores data each node has links to zero or more nodes in graph theory the links are normally called edges graphs occur frequently in a wide variety of real-world problems social network analysis e.g., six-degrees-of-Kevin-Bacon, Facebook Friend Wheel transportation networks e.g., http://ac.fltmaps.com/en many other examples http://www.visualcomplexity.com/vc/

Trees trees are special cases of graphs a tree is a data structure made up of nodes each node stores data each node has links to zero or more nodes in the next level of the tree children of the node each node has exactly one parent node except for the root node

50 34 11 6 79 23 99 33 67 88 31 1 6 83

50 11 6 79 34 88 67 23 33 99 1 31 83

Trees the root of the tree is the node that has no parent node all algorithms start at the root

root 50 34 11 6 79 23 99 33 67 88 31 1 6 83

Trees a node without any children is called a leaf

50 34 11 6 79 23 99 33 67 88 31 1 6 83 leaf leaf leaf leaf leaf leaf

Trees the recursive structure of a tree means that every node is the root of a tree

50 34 11 6 79 23 99 33 67 88 31 1 subtree 6 83

50 34 11 6 79 23 99 33 67 88 31 1 subtree 6 83

50 subtree 34 11 6 79 23 99 33 67 88 31 1 6 83

50 34 11 6 subtree 79 23 99 33 67 88 31 1 6 83

50 34 11 6 79 23 99 33 67 88 subtree 31 1 6 83

Binary Tree a binary tree is a tree where each node has at most two children very common in computer science many variations traditionally, the children nodes are called the left node and the right node

50 left right 27 73 8 44 83 73 93

50 27 73 left right 8 44 83 73 93

50 27 73 right 8 44 83 73 93

50 27 73 8 44 83 left right 74 93

Binary Tree Algorithms the recursive structure of trees leads naturally to recursive algorithms that operate on trees for example, suppose that you want to search a binary tree for a particular element

examine root examine left subtree examine right subtree public static <E> boolean contains(E element, Node<E> node) { if (node == null) { return false; } if (element.equals(node.data)) { return true; boolean inLeftTree = contains(element, node.left); if (inLeftTree) { boolean inRightTree = contains(element, node.right); return inRightTree; examine root examine left subtree examine right subtree

t.contains(93) 50 27 73 8 44 83 74 93

50 50 == 93? 27 73 8 44 83 74 93

50 27 73 27 == 93? 8 44 83 74 93

50 27 73 8 44 83 8 == 93? 74 93

50 27 73 8 44 83 44 == 93? 74 93

50 27 73 73 == 93? 8 44 83 74 93

50 27 73 8 44 83 83 == 93? 74 93

50 27 73 8 44 83 74 93 74 == 93?

50 27 73 8 44 83 74 93 93 == 93?

Iteration visiting every element of the tree can also be done recursively 3 possibilities based on when the root is visited inorder visit left child, then root, then right child preorder visit root, then left child, then right child postorder visit left child, then right child, then root

50 27 73 8 44 83 74 93 inorder: 8, 27, 44, 50, 73, 74, 83, 93

50 27 73 8 44 83 74 93 preorder: 50, 27, 8, 44, 73, 83, 74, 93

50 27 73 8 44 83 74 93 postorder: 8, 44, 27, 74, 93, 83, 73, 50