Copyright © 2004-2014 Curt Hill Other Trees Applications of the Tree Structure.

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
1 Theory I Algorithm Design and Analysis (2 - Trees: traversal and analysis of standard search trees) Prof. Th. Ottmann.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CS 171: Introduction to Computer Science II
Binary Tree B G E D I H F c A Binary tree
BST Data Structure A BST node contains: A BST contains
Trees. Definition of a tree A tree is like a binary tree, except that a node may have any number of children –Depending on the needs of the program, the.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
CS 206 Introduction to Computer Science II 12 / 10 / 2008 Instructor: Michael Eckmann.
Binary Trees. 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 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
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. 2 Linear data structures Here are some of the data structures we have studied so far: –Arrays –Singly-linked lists and doubly-linked lists.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees.
Lecture 81 Data Structures, Algorithms & Complexity Tree Algorithms GRIFFITH COLLEGE DUBLIN.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Data Structures and Algorithms Lecture (BinaryTrees) Instructor: Quratulain.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree Data Structures.
Compiled by: Dr. Mohammad Omar Alhawarat
Binary Tree. Contiguous versus Linked List Insertion in Contiguous list needs a lot of move. For big chunks of records it is time consuming. Linked List.
Starting at Binary Trees
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Chapter 7 Trees_Part3 1 SEARCH TREE. Search Trees 2  Two standard search trees:  Binary Search Trees (non-balanced) All items in left sub-tree are less.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
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.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
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.
Chapter 10: Trees A tree is a connected simple undirected graph with no simple circuits. Properties: There is a unique simple path between any 2 of its.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary 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.
Binary Tree.
Bushy Binary Search Tree from Ordered List. Behavior of the Algorithm Binary Search Tree Recall that tree_search is based closely on binary search. If.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Copyright © Curt Hill Other Trees Applications of the Tree Structure.
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
Binary Trees.
Trees Chapter 15.
Binary Trees.
Fundamentals of Programming II Introduction to Trees
Tree.
Section 8.1 Trees.
Binary Trees.
A Kind of Binary Tree Usually Stored in an Array
Binary Trees.
Binary Trees.
A Robust Data Structure
Trees.
Binary Trees.
Chapter 20: Binary Trees.
Binary Trees.
Data Structures Using C++ 2E
A Binary Tree is a tree in which each node has at most 2 children
Presentation transcript:

Copyright © Curt Hill Other Trees Applications of the Tree Structure

Copyright © Curt Hill Expression trees An expression tree contains: –Operators as interior nodes –Values as leaves The shape of the expression tree captures the precedence Consider the following expression: 2+3*4

Copyright © Curt Hill Expression trees 2+3*4 + * *2 34 3*4+2

Copyright © Curt Hill Traversal The names come from the above expression tree There are six (3!) ways to traverse the depending on the order of processing: –The node –The left subtree –The right subtree Inorder (left and right) Preorder Postorder

Copyright © Curt Hill Inorder According to the sorted order of tree Visit lower (left) subtree Process node Visit upper (right) subtree The reverse produces higher to lower Left to right * 4 This gives standard algebraic notation

Copyright © Curt Hill Preorder Node first then subtrees Process node Visit lower (left) subtree Visit upper (right) subtree Expression + 2 * 3 4 Remember this?

Copyright © Curt Hill Postorder Subtrees first and then node Visit lower (left) subtree Visit upper (right) subtree Process node Expression: * + Reverse Polish

Parse Trees Expression trees are a small instance of parse trees A presentation on parse trees also existspresentation on parse trees Copyright © Curt Hill

Balance and Search Times The time it takes to search a tree is based upon the path length to the desired node Assuming equal distributions then –The average search is the sum of the path lengths divided by the number of tree nodes

Copyright © Curt Hill Unbalanced Tree

Copyright © Curt Hill Average Search Length 12 – 1 6 – 2 19 – 2 2 – 3 15 – 3 36 – 3 24 – 4 0 – 4 4 – 4 30 – 5 29 – 6 Sum of 37 for 11 nodes gives average search length of 3.3

Copyright © Curt Hill Perfectly Balanced Tree

Copyright © Curt Hill Average Search Length 36 – 1 4 – 2 24 – 2 2 – – 3 28 – 3 0 – 4 6 – 4 19 – 4 30 – 4 Sum of 33 fpr 11 nodes gives average search length of 3.0 Balanced does perform better

Copyright © Curt Hill AVL Balanced Tree

Copyright © Curt Hill Average Search Length 36 – 1 6 – 2 19 – 2 2 – – 3 28 – 3 0 – 4 4 – 4 28 – 4 30 – 4 Sum of 33 fpr 11 nodes gives average search length of 3.0 AVL balanced has the same performance as perfectly balanced

Copyright © Curt Hill Balanced is Best? The idea of balancing a tree is predicated on equal frequencies of keys –Reasonable assumption if no contrary information –However, if we have frequency information we can do better C++ keywords are not evenly distributed

Copyright © Curt Hill Path Lengths The idea of balance is nice in general but… If we have a reasonable idea of the frequency of entries we can do better than perfectly balanced What we want to do is minimize the average path length With our previous knowledge we could make not assumptions concerning frequency Now we can generate a more precise formula

Copyright © Curt Hill Average Path Length

Copyright © Curt Hill Optimal Search Trees What we want are high frequency words close to the root and low frequency words at the leaves You might think that the most common word should be the root and the next two words the second and third common It does not work that way since we need to maintain the order as well

Copyright © Curt Hill Example For example the word "the" is the most common word in English text The top n are: –the (20) –and (15) –of (13) –to (12) –you (7) –in (7) –a (6) Because the top two are such extremes it may be better to have “of” as the root

Copyright © Curt Hill LISP Lists LISP is very old –Second only to FORTRAN Usually encountered in Programming Language or Artificial Intelligence classes It has an ubiquitous data structure called a list However it is not a list in the sense that it is purely linear Instead it is a tree, but a tree without a key

Copyright © Curt Hill Variables in LISP A variable may be: –An atom –A list An atom is any word or number A list may be: –Empty –A variable followed by a list

Copyright © Curt Hill Lists A list could be a simple list within parenthesis –(Three element list) It could also have sub-lists –(Atom (A sub list) another (list)) –This is clearly not a linear list such as an STL List LISP programs were also lists –The programs and data had same form

Copyright © Curt Hill Implementation The LISP language was influenced by the machine on which it was developed It had a 36 bit word that was partitioned into two pointers –Contents Address Register (CAR) –Contents Data Register (CDR) An atom used the word for data A list used the pointers and atoms A list always ended in nil, a special pointer

Copyright © Curt Hill Example Three element List nil (Three element list)

Copyright © Curt Hill Second Example Atom sub last nil (Atom (sub list) last) list nil

Copyright © Curt Hill List Processing There were two functions that were continually used in LISP to process a list Car gave the first item of the list –Which could be a list itself Cdr gave the rest of the list A heavy dose of recursion and LISP could do it all