Lecture 21 Trees. “ A useful data structure for many applications”

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

Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Binary Search Trees CSE 331 Section 2 James Daly.
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.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Chapter 4: Trees General Tree Concepts Binary Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
Tree A connected graph that contains no simple circuits is called a tree. Because a tree cannot have a simple circuit, a tree cannot contain multiple.
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
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 Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
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.
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
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.
Discrete Mathematics Chapter 5 Trees.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees.
Data Structures Lakshmish Ramaswamy. Tree Hierarchical data structure Several real-world systems have hierarchical concepts –Physical and biological systems.
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.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
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.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
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.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
Lecture 9 Binary Trees Trees General Definition Terminology
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
1 CMSC 341 Introduction to Trees Textbook sections:
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
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.
Trees. Trees: – A trunk from the roots – Divides into branches – Ends in leaves.
CSE 373 Data Structures Lecture 7
CSCE 210 Data Structures and Algorithms
Week 6 - Wednesday CS221.
Source Code for Data Structures and Algorithm Analysis in C (Second Edition) – by Weiss
Tree.
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
Data Structures & Algorithm Design
CSE 373 Data Structures Lecture 7
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
Trees.
Trees Definitions Implementation Traversals K-ary Trees
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Binary Trees.
Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

Lecture 21 Trees

“ A useful data structure for many applications”

Applications Compiler design text processing (dictionary) searching algorithms DOM trees (browsers) cider like red I apple andwine

A tree is a set of nodes and a set of directed edges that connects pairs of nodes. A tree is a a Directed, Acyclic Graph (DAG) with the following properties - one vertex is distinguished as the root; no edges enter this vertex - every other vertex has exactly one entering edge Trees Ctd…

thus, there exists a unique path from the root to any vertex Every node can have up to two(in a binary tree) other nodes connected to it, which we call the children. – Left and right child Trees

Terminology parent, child, sibling - – immediate predecessor of a node is its parent; – immediate successor is a child ancestor, descendant - – if path from root to node a goes through node b, b is an ancestor of a; – conversely, a is a descendant of b

Path length – the number of edges that must be followed leaf node – a node that has no children Depth of a node – length of the path from root to the node height of a tree – number of nodes in the longest path from root to a leaf node Definitions

internal node - – not a leaf subtree at v - – treat v as if it were a root height of vertex v - – length of longest path from v to a leaf height of tree - – height of root depth of v - – length of path from root to v ordered tree - – children of each vertex are distinguished (ordered left to right) More Definitions

Binary Search Trees Binary Search Tree – An ordered tree in which the nodes can have 0, 1, 2 children Complete - – bottom row missing nodes only on right Full, complete - – all rows full, no missing nodes on any level (max nodes for height) – # nodes in full, complete tree of height k? – # leaves? – Conversely, height of full, complete tree with n nodes?

Consider the following definition of a node class Node { type data; Node left; Node right; Node(type n){data=n; left=NULL;right=NULL;} } data left right Implementation

Node root; root = new Node(10); Node nextnode = new Node(5); // make this node the left child of the root Root.left = nextnode; // create another node and make it the right node nextnode = new Node(20); Root.right = nextnode; Now let us build a tree according to the following criteria. Make the first number the root of the tree For every subsequent number, use the following criteria to determine its place in the tree – if the node is bigger, go right – if the node is smaller, go left Implementation

Open a file read a num Node root = new Node(num); while (!infile.eof()) { int num = infile.getInt(); Node nextnode = new Node(num); Node ptr = root; Node prev; while (ptr != null) { prev = ptr; if (num >= ptr.data) ptr= ptr.right; if (num < ptr.data) ptr= ptr.left; } if (num >= prev.data) pre.right = nextnode; else prev.left = nextnode; } Implementation

Recursive Insert void Insert(Node t, string val) // Note we pass our tree ptr in by ref { // since we need to modify our left right ptrs if (t == null) // within each node { t=new Node( val); return; } if (t.data == val) { cout << "Ignoring Dupe\n"; return; } if ( val < t.data ) Insert(t.left, val ); else Insert(t.right, val ); }

Tree Traversal Three methods of tree traversal – inorder left, root, right – preorder root, left, right – postorder left, right, root

Inorder Traversal void print(Node ptr) { if (ptr != NULL) { print(ptr.left); System.out.print(ptr.data); print(ptr.right); } How does this work?