Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.

Slides:



Advertisements
Similar presentations
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
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.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Trees Chapter 8.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
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.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
KNURE, Software department, Ph , N.V. Bilous Faculty of computer sciences Software department, KNURE The trees.
Binary Trees Chapter 6.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
Tree.
Slow Insertion in an Ordered Array 1. Slow Searching in a Linked List 2.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
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.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
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.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
Compiled by: Dr. Mohammad Omar Alhawarat
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Starting at Binary Trees
Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
Introduction to Trees IT12112 Lecture 05 Introduction Tree is one of the most important non-linear data structures in computing. It allows us to implement.
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.
© University of Auckland Trees – (cont.) CS 220 Data Structures & Algorithms Dr. Ian Watson.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
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.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
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.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
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.
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.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Chapter 8 (Lafore’s Book) Binary Tree Hwajung Lee.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
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 Saurav Karmakar
Data Structures and Design in Java © Rick Mercer
Binary Tree.
Section 8.1 Trees.
Tonga Institute of Higher Education
Binary Trees, Binary Search Trees
CS223 Advanced Data Structures and Algorithms
Introduction to Trees IT12112 Lecture 05.
Trees.
Data Structures and Algorithm Analysis Trees
Binary Trees, Binary Search Trees
Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree

Why use a Tree? Trees combines the advantages of two other data structures – an ordered array and a linked list. You can search a tree quickly as you can an ordered array You can also insert and delete items quickly as you can with a linked list

Definition of Trees A tree is a data structure. It is used for –storing files –compiler design –text processing –searching algorithms A tree can be defined non-recursively or recursively. Non-recursive definition: A tree consists of a set of nodes and a set of directed edges that connect pairs of nodes. –One node is distinguished as the root –Every node c, except the root, is connected by an edge from exactly one other node p. Node p is c’s parent, and c is one of p’s children –A unique path traverses from the root to each node. The number of edges that must be followed is the path length. –A leaf is a node that has no children.

More Definitions –A tree with N nodes must have N-1 edges, every node except the root has an incoming edge –Depth of a node is the length of the path from the root to the node –Height of a node is the length of the path from the node to the deepest leaf –Nodes with the same parents are called siblings –If there is a path from node u to node v, then u is an ancestor of v and v is a descendant of u –Size of a node is the number of descendants the node has including the node itself. The size of a tree is the size of the root. –A node may be considered the root of a sub-tree. A node’s sub-tree contains all its descendants. –A node is visited when the program control arrives at the node for the purpose of carrying out some operation – checking the value of one of its data fields or displaying it. Merely passing over a node from one node to another is not considered visiting the node

More Definitions –To traverse a tree means to visit all the nodes in some specified order. –Level of a particular node refers to how many generations the node is from the root. It is the same thing as the depth. –One data item in a node is usually designated the key value. This value is used to search for the item or perform other operations on it. –In a binary tree each node can have at most two children – the left child and the right child –In a binary search tree a node’s left child must have a key less than its parent, and node’s right child must have a key greater than or equal to its parent. A recursive definition of a tree –Either a tree is empty or it consists of a root and zero or more nonempty sub-trees T 1, T 2,..., T k each of whose roots are connected by an edge from the root.

Uses of Trees Expression tree is used in compiler design. The leaves of an expression tree are operands (constants or variables) and the other nodes contain operators. Huffman coding tree is used to implement a data compression algorithm. Each symbol in the alphabet is stored at a leaf. Its code is obtained by following the path to it from the root. A left link corresponds to 0 and a right link to a 1. Binary search trees allow logarithmic time insertions and access of items.

Finding a Node Use variable current to hold a node that it is examining Start at the root In the while loop compare value to be found (key) with the iData value (key field). If key is less than this value then current is set to the node’s left child If key is greater than this value then current is set to the node’s right child. If current becomes null, then we have not found the node If node is found then while loop is exited and the found node is returned

Inserting a Node First find the place to insert the node. (Same process as finding a node that turns out not to exist) Follow path from the root to the appropriate node which will become the parent of the new node. Once the parent has been found the new node is connected as its left or right child, depending on whether the new node’s key is less than or greater than that of the parent.

Traversing a Tree Inorder traversal – the left child is recursively visited, the node is visited, and the right child is recursively visited. Preorder traversal – a node is visited and then its children are visited recursively. Postorder traversal – a node is visited after both children are visited.

Parsing Expressions Infix notation: A * ( B + C ) Prefix notation: * A + B C Postfix notation : A B C + *

Finding Minimum and Maximum Values To get the minimum go to the left child of the root; then go to the left child of that child, and so on, until you come to a node that has no left child. That node is a minimum. To get the maximum go to the right child of the root, then go to the right child of that child, and so on, until you come to a node that has no right child. That node is a maximum.

Deleting a Node Find the node you want to delete. Once you have found the node there are three cases to consider: –The node is a leaf (has no children) –The node has one child –The node to be deleted has two children To delete a leaf node simply change the appropriate child field in the node’s parent to be null. The space in memory occupied by that node will be reclaimed by the Java garbage collector. If the node to be deleted has one child then directly connect the parent of this node to the child of this node. If the node to be deleted has two children then replace this node with its inorder successor.

Finding the inorder successor The inorder successor is the smallest node of the set of nodes that are larger than the original node. When you go to the original node’s right child, all the nodes in the resulting sub-tree are greater than the original node. To obtain the minimum value in this sub-tree follow the path down all the left children. The first node that has no left child is the successor node.

Number of Nodes per Level No. of LevelsNo. of Nodes Num of Nodes = 2 Level - 1

Efficiency of Binary Trees Most operations with trees involve descending from level to level. In a full tree, there is one more node on the bottom row than in the rest of the tree. This about half of all operations require finding a node on the lowest level. In an unordered array or a linked list containing 1,000,000 items it would take on the average 500,000 comparisons to find the one you wanted. In a tree of 1,000,000 items it takes 20 (or fewer) comparisons.