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.

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

Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Binary Search Trees CSE 331 Section 2 James Daly.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CS 171: Introduction to Computer Science II
Computer Science 2 Data Structures and Algorithms V section 2 Introduction to Trees Professor: Evan Korth New York University.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Binary Trees. 2 Linear data structures Here are some of the data structures we have studied so far: –Arrays –Singly-linked lists and doubly-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.
Lecture 17 Non-Linear data structures Richard Gesick.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
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:
Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node.
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.
Starting at Binary Trees
Tree Traversal.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
Rudiments of Trees a Joshua presentation DATA STRUCTURES.
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.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
1. Iterative Preorder Traversal Rpreorder(T) 1. [process the root node] if T!= NULL then Write Data(T) else Write “empty Tree” 2. [process the left subtree]
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
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:
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
Binary Trees.
Binary Search Trees Chapter 7 Objectives
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Binary Trees.
Binary Trees and Binary Search Trees
Recursive Objects (Part 4)
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Trees.
Tree.
Section 8.1 Trees.
Trees.
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Binary Trees.
Trees.
Binary Trees.
Binary Trees.
Trees Definitions Implementation Traversals K-ary Trees
Binary Trees.
Chapter 20: Binary Trees.
Trees.
Trees Trees.
Non-Linear data structures
Presentation transcript:

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 two parents Node A Node D Node B Node C parent child NOT OK

Roots, Leaves, Depth, and Subtrees Root node: ancestor of all others Leaf node: has no children Depth: most links from root to any leaf Subtree: subset of nodes including all children of included nodes Graph Theory: A tree is a directed acyclic graph with exactly one root node such that there is exactly one path from the root to all other nodes. root node Subtree leaf node leaf node leaf node leaf node

Nodes Can Hold Data Typically one datum per node These nodes each have a char A Linked List is a lot like a data holding tree where each node has 1 child. ‘H’ ‘O’ ‘L’ ‘D’ ‘A’ ‘T’ ‘A’

Binary Trees ≤ 2 children per node These show up all over the place. NOT OK

Binary Search Trees Each node holds a datum. Data must be orderable. Each node has at most 2 children: – Left: subtree with lesser data – Right: subtree with greater data ‘F’ ‘B’ ‘A’‘D’ ‘C’ ‘E’ ‘G’ ‘I’ ‘H’

Searching Binary Search Trees Determine if a tree holds a datum Get something else stored with a datum Find data in range Starting at root, Node’s datum: – Too high? Move left – Too low? Move right – Just right? Done! At most Depth (which can be as low as Log(# nodes)) steps! Search for ‘E’ ‘F’ ‘B’ ‘A’‘D’ ‘C’ ‘E’ ‘G’ ‘I’ ‘H’ Start Too high Too Low Found it!

Searching Binary Search Trees public class TreeNode { private TreeNode left; private TreeNode right; private Comparable datum; public Comparable searchFor(Comparable obj) { int comparison = obj.compareTo(datum); if (comparison == 0) { return datum; } else if (comparison < 0) { if (left == null) return null; return left.searchFor(obj); } if (right == null) return null; return right.searchFor(obj); }

Assembling Binary Search Trees Start with a root node Add each datum, starting at root, new datum: – > this node’s? Move right – < this node’s? Move left – This node doesn’t exist yet? Make one Done! Add: F, B, G, D, C, I, A, E, H ‘F’ ‘B’ ‘A’‘D’ ‘C’ ‘E’ ‘G’ ‘I’ ‘H’

public class TreeNode { private TreeNode left, right; private Comparable datum; public TreeNode(Comparable d) { datum = d; } public void add(Comparable item) { if (item.compareTo(datum) > 0) { if (right == null) right = new TreeNode(item); else right.add(item); } else { if (left == null) left = new TreeNode(item); else left.add(item); } Assembling Binary Search Trees

Balance At best: depth ≈ Log 2 (# nodes) – Faster search – Faster insertion Balanced: minimally deep Balanced Tree – There is no way to put 9 nodes in depth < 3 ‘F’ ‘B’ ‘A’‘D’ ‘C’ ‘E’ ‘G’ ‘I’ ‘H’

An Unbalanced Tree ‘F’ ‘G’ ‘I’ ‘H’ ‘B’ ‘A’ ‘D’ ‘C’ ‘E’ Same elements as our balanced tree Added in a different order

Traversal Preorder: datum (traverse other nodes) – H (O (D)) (L (D (A) (T) (A))) – Can be written H O D L D A T A Postorder: (traverse other nodes) datum – ((D) O) (((A) (T) (A) D) L)H – Can be written D O A T A D L H Inorder (Binary Trees Only): (traverse left) datum (traverse right) ‘H’ ‘O’ ‘L’ ‘D’ ‘A’ ‘T’ ‘A’

InOrder Traversal (traverse left) datum (traverse right) ((A) B ((C)D (E))) F (G ((H) I)) Get the elements of a BST in order ‘F’ ‘B’ ‘A’‘D’ ‘C’ ‘E’ ‘G’ ‘I’ ‘H’

Tree Traversal: “Flags” The order in which the nodes are visited during a tree traversal can be easily determined by imagining there is a “flag” attached to each node preorder inorder postorder A BC DEFG A BC DEFG A BC DEFG A B D E C F G D B E A F C G D E B F G C A To traverse the tree, collect the flags: Thanks to David Matuszek

public class TreeNode { private TreeNode left, right; private Comparable datum; public TreeNode(Comparable d) { datum = d; } public List preorder() { ArrayList list = new ArrayList (); list.add(datum); if (left != null) list.addAll(left.preorder()); if (right != null) list.addAll(right.preorder()); return list; } Traversal

Syntax Trees and Reverse Polish Notation Syntax tree: – Parent node is operator – Children are operands This tree: – Inorder: (36÷(3×4))+(neg 5) – Preorder: + (÷ 36 (× 34)) (neg 5) (LISP looks kind of like this) – Postorder: (36 (3 4 ×) ÷) (5 neg) + + ÷ 36 × 34 neg 5 Reverse Polish Notation (RPN): postorder, no () If each operator has a known number of operands (1 for neg, 2 for +), no () required!