Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees. What is a Tree? In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent- child relation.

Similar presentations


Presentation on theme: "Trees. What is a Tree? In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent- child relation."— Presentation transcript:

1 Trees

2 What is a Tree? In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent- child relation Applications: – Organization charts – File systems – Programming environments

3 What is a Tree? Computers”R”Us SalesHRManufacturing LaptopsDesktops US International EuropeAsiaCanada

4 Tree Terminology Root: node without parent (A) Internal node: node with at least one child (A, B, C, F) External node (a.k.a. leaf ): node without children (E, I, J, K, G, H, D) Ancestors of a node: parent, grandparent, grand- grandparent, etc. Depth of a node: number of ancestors Height of a tree: maximum depth of any node (3) Descendant of a node: child, grandchild, grand- grandchild, etc. Subtree: tree consisting of a node and its descendants

5 subtree Tree Terminology A B DC GH E F IJ K ancestors of I, J and K descendants of A

6 Use of Trees In most OS’s, files are organized hierarchically into folders, and presented in the form of trees. A structured document, such as a book is hierarchically organized as a tree – ordered tree Arithmetic expressions can be represented by an expression tree. Different outcomes in a result can be represented by a decision tree

7 Tree ADT We use positions to abstract nodes Generic methods: – integer size() – boolean isEmpty() – objectIterator elements() – positionIterator positions() Accessor methods: – position root() – position parent(p) – positionIterator children(p)

8 Tree ADT Query methods: – boolean isInternal(p) – boolean isExternal(p) – boolean isRoot(p) Update methods: – swapElements(p, q) – object replaceElement(p, o) Additional update methods may be defined by data structures implementing the Tree ADT

9 Algorithm Analysis on the Operations of the Tree isEmpty() O(1) size() O(1) elements() O(n) positions() O(n) root() O(1) parent() O(1) children( pos ) O(c)* isInternal() O(1) isExternal() O(1) isRoot() O(1) swapElements( w, x ) O(1) replaceElement( pos, e ) O(1) * c represents the number of children

10 Structure of a Tree The tree is a structure that stores information in a parent-child structure. This results in a recursive formation – that is, the root node creates a tree, and each child of the root node creates a sub-tree, and each child in each of those nodes creates other sub-trees.

11 Structure of a Tree This ‘recursive’ format of a tree structure gives rise to writing recursive algorithms for some functionality. For instance, to find the depth of a node all that is necessary is to find the number of parents all the way to the root node. There is no necessity to visit all then nodes in the tree.

12 Depth of a Tree Depth of v = number of ancestors of v If v is the root v’s depth = 0 else v’s depth = 1 + depth of v’s parent v w x y z v = 0 w = 1 + depth( myParent ) = 1 + 0 = 1 x = 1 + depth( myParent ) = 1 + 1 = 2 y = z = 1 + depth( myParent ) = 1 + 2 = 3

13 Finding the Depth int depth( const Tree& T, const Position& v ) { if( T.isRoot(v) ) return 0; else return 1 + depth( T, T.parent(v) ); }

14 Big-O of the depth function The big-O of this function would depend on how the tree is created. If the tree is lop-sided, then in the worse case scenario the big- O would be O(n), but if the tree is balanced, the worse case would be O(logn)

15 Height Height of node v If v is an external node v’s height = 0 else v’s height = 1 + maximum height of v’s children h v = 1 + max( h of myChildren ) h v = 1 + 1 = 2 v h = 0 (external node) h = 1 + max( h of myChildren ) h = 1 + 0 = 1 h = 0

16 Finding the Height of a Tree Algorithm height( T, root ) if T.isExternal(root) then return 0 else h = 0 for each node  T.children(root) do h = max( h, height(T,node) ) return 1 + h r u t v 1 2 T 00


Download ppt "Trees. What is a Tree? In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent- child relation."

Similar presentations


Ads by Google