1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.

Slides:



Advertisements
Similar presentations
§2 Binary Trees Note: In a tree, the order of children does not matter. But in a binary tree, left child and right child are different. A B A B andare.
Advertisements

CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
CS 201 Data Structures and Algorithms Chapter 4: Trees (BST) Text: Read Weiss, §4.3 1Izmir University of Economics.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
4.5 AVL Trees  A tree is said to be balanced if for each node, the number of nodes in the left subtree and the number of nodes in the right subtree differ.
Trees, Binary Trees, and Binary Search Trees COMP171.
Chapter 4: Trees General Tree Concepts Binary Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Binary Tree B G E D I H F c A Binary tree
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.
Marc Smith and Jim Ten Eyck
Joseph Lindo Trees Sir Joseph Lindo University of the Cordilleras.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
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.
Trees EENG212 Algorithms and Data Structures. Trees Outline  Introduction to Trees  Binary Trees: Basic Definitions  Traversing Binary Trees  Node.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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 ),
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,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
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.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
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.
Trees, Binary Trees, and Binary Search Trees COMP171.
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 Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
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.
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.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
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 5 TREE CSEB324 DATA STRUCTURES & ALGORITHM.
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.
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.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
1 CMSC 341 Introduction to Trees Textbook sections:
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
CSE 373 Data Structures Lecture 7
CS 201 Data Structures and Algorithms
CC 215 Data Structures Trees
Binary Search Tree (BST)
CMSC 341 Introduction to Trees.
CSE 373 Data Structures Lecture 7
TREE DATA STRUCTURE Data Structure 21-Sep-18 Tree
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
Trees CSE 373 Data Structures.
CE 221 Data Structures and Algorithms
Data Structures and Algorithm Analysis Trees
Binary Trees, Binary Search Trees
CE 221 Data Structures and Algorithms
Trees.
Trees CSE 373 Data Structures.
Trees CSE 373 Data Structures.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees to support searching operations in O(logN) average time

2 4.1: Preliminaries One natural way to define a tree is recursively. A tree is a collection of nodes. The collection can be empty; otherwise, a tree consists of a distinguished node r, called root, and zero or more nonempty (sub)trees T 1, T 2, … T k, each of whose roots are connected by a direct edge from r.

3 4.1: Preliminaries Parent Node A is the parent of node B if B is the root of the left or right subtree of A Left (Right) Child Node B is the left (right) child of node A if A is the parent of B.

4 4.1: Preliminaries Sibling Node B and node C are siblings if they are left and right siblings of the same node A Leaf A node is called a leaf if it has no children

5 4.1: Preliminaries Ancestor Node A is ancestor of node B if A is either the parent of B or is the parent of some ancestor of B Left (Right) Descendant Node B is the left (right) descendant of node A if B is either the left (right) child of A or a descendant of a left (right) child of A

6 4.1: Preliminaries Level of a Node Level of the root of a binary tree is 0, and the level of any other node in the tree is one more than the level of its parent Depth of a Tree The depth of a tree is the maximum level of any leaf in the tree (also called the height of the tree)

Preliminaries: Implementation of Tree typedef struct TreeNode * PtrToNode; struct TreeNode {ElementType element; PtrToNode FirstChild; PtrToNode NextSibling; }

Preliminaries: Implementation of Tree Example

Preliminaries: Implementation of Tree FirstChild pointer: arrow that points downward NextSibling pointer: arrow that goes left to right

Preliminaries: Unix File System

Preliminaries: Unix File System Any suggestions to handle such a file system?

Preliminaries: Unix File System Static void ListDir(DirectoryOrFile D, int Depth) {if (D is a legitimate entry) {PrintName(D, Depth); if (D is a directory) for each child,C, of D ListDir(C, Depth +1);} } void ListDirectory(DirectoryOrFile D) {ListDir (D, 0); }

Preliminaries: Unix File System Static void SizeDirectory(DirectoryOrFile D) {int TotalSize; TotalSize = 0; if (D is a legitimate entry) {TotalSize = FileSize(D); if (D is a directory) for each child, C, of D TotalSize += SizeDirectory(C); } return TotalSize; }

Preliminaries: Traversal Strategy 3 methods of traversal postorder traversal strategy preorder traversal strategy inorder traversal strategy

Preliminaries: Traversal Strategy Preorder traversal (depth-first order) 1.Visit the node 2.Traverse the left subtree in preorder 3.Traverse the right subtree in preorder

Preliminaries: Traversal Strategy Inorder traversal (symmetric order) 1.Traverse the left subtree in inorder 2.Visit the node 3.Traverse the right subtree in inorder

Preliminaries: Traversal Strategy Postorder traversal (breadth-first order) 1.Traverse the left subtree in postorder 2.Traverse the right subtree in postorder 3.Visit the node

Preliminaries: Traversal Strategy + -H /* + *E - ABCDF G Example

Preliminaries: Traversal Strategy + -H /* + *E - ABCDF G Preorder: +-/+AB*CD*E-FGH Inorder : A+B/C*D-E*F-G+H Postorder: AB+CD*/EFG-*-H+ Preorder: +-/+AB*CD*E-FGH Inorder : A+B/C*D-E*F-G+H Postorder: AB+CD*/EFG-*-H+

Preliminaries: Traversal Strategy Exercise: Using the tree shown in page 9, write down the results if employing the postorder traversal strategy preorder traversal strategy inorder traversal strategy

Binary Trees A binary tree is a finite set of elements called nodes. This set is either empty or it can be partitioned into 3 subsets. The first subset is a single node called the root of the tree. The other two subsets are themselves binary trees. One is called the left subtree and the other the right subtree of the binary tree.

Binary Trees Strictly Binary Tree A binary tree is called strictly binary tree if every nonleaf node in the tree has nonempty left and right subtrees Complete (Full) Binary Tree A complete binary tree of depth d is a strictly binary tree with all leaf nodes at level d.

Binary Trees Almost Complete Binary Tree A binary tree of depth d is an almost complete binary tree if (a)Any node at level less than d-1 has 2 children. (b)For any node N in the tree with a right descendant at level d, N must have a left child and every left descendant of N is either a leaf at level d or has 2 children.

Binary Trees  An almost complete strictly binary tree with n leaves has 2n - 1 nodes.  There is only a single almost complete binary tree with n nodes. This tree is strictly binary if and only if n is odd.

Binary Trees Strictly binary but not almost complete Level 1, 2, 3

Binary Trees Strictly binary but not almost complete Violate condition 2

Binary Trees Strictly binary and almost complete

Binary Trees Almost complete but not strictly binary

Binary Search Trees Every node, X, in the tree, the value of all the keys in its left subtree are smaller than the key value of X, and the values of all the keys in its right subtree are larger than the key value of X.  X Please refer to figure 4.15

BST: Implementation struct TreeNode; typedef struct TreeNode *Position; typedef struct TreeNode *SearchTree; SearchTree MakeEmpty( SearchTree T ); Position Find( ElementType X, SearchTree T ); Position FindMin( SearchTree T ); Position FindMax( SearchTree T ); SearchTree Insert( ElementType X, SearchTree T ); SearchTree Delete( ElementType X, SearchTree T ); ElementType Retrieve( Position P );

BST: Implementation struct TreeNode { ElementType Element; SearchTree Left; SearchTree Right; };

BST Implementation: MakeEmpty SearchTree MakeEmpty( SearchTree T ) { if( T != NULL ) { MakeEmpty( T  Left ); MakeEmpty( T  Right ); free( T ); } return NULL; }

BST Implementation: Find Position Find( ElementType X, SearchTree T ) { if ( T == NULL ) return NULL; if ( X < T  Element ) return Find( X, T  Left ); else if ( X > T  Element ) return Find( X, T  Right ); elsereturn T; }

BST Implementation: FindMin Position FindMin( SearchTree T ) { if ( T == NULL ) return NULL; else if( T  Left == NULL ) return T; else return FindMin( T  Left ); }

BST Implementation: FindMax Position FindMax( SearchTree T ) { if ( T != NULL ) while ( T  Right != NULL ) T = T  Right; return T; }

BST Implementation: Insert Please refer to Figure 4.21 

BST Implementation: Insert SearchTree Insert( ElementType X, SearchTree T ) {if ( T == NULL ) {T = malloc( sizeof( struct TreeNode ) ); if ( T == NULL ) FatalError( "Out of space!!!" ); else {T  Element = X; T  Left = T  Right = NULL; }

BST Implementation: Insert else if ( X < T  Element ) T  Left = Insert( X, T  Left ); else if( X > T  Element ) T  Right = Insert( X, T  Right ); /* Else X is in the tree already; we'll do nothing */ return T; /* Do not forget this line!! */ }

BST Implementation: Delete 3 cases (please refer to Figure 4.24) 

BST Implementation: Delete SearchTree Delete( ElementType X, SearchTree T ) {Position TmpCell; if ( T == NULL ) Error( "Element not found" ); else if ( X < T  Element ) /* Go left */ T  Left = Delete( X, T  Left ); else if ( X > T  Element ) /* Go right */ T  Right = Delete( X, T  Right ); else /* Found element to be deleted */

BST Implementation: Delete if ( T  Left && T  Right ) /* Two children */ {TmpCell = FindMin( T  Right ); T  Element = TmpCell  Element; T  Right = Delete( T  Element, T  Right );} else /* One or zero children */ { TmpCell = T; if ( T  Left == NULL ) /* Also handles 0 children */ T = T  Right; else if ( T  Right == NULL ) T = T  Left; free( TmpCell );} return T;}

: Expression Tree Expression tree for (a+b*c) +((d*e+f)*g)

: Expression Tree Algorithm to convert postfix expression into expression tree e.g. input: ab+cde+**

: Expression Tree