# 1 Chapter 7 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.

## Presentation on theme: "1 Chapter 7 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."— Presentation transcript:

1 Chapter 7 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 Organization charts File systems File systems Programming environments Programming environments Computers”R”Us SalesR&DManufacturing LaptopsDesktops US International EuropeAsiaCanada

3 Outline Recursive Structure Definitions Tree Terminology Binary Tree Interface Example - Expression Trees Binary Tree Implementation Tree Traversals Tree Property-based methods

4 List Definition - Recursive A list is either empty or it contains a head element and a sublist where the sublist must be a list.

5 Tree Definition - Recursive A tree is either empty or it consists of an element called the root, and a (possibly empty) list of subtrees, where each subtree is a tree. The term node is used to refer to an element at a particular location in a tree.

6 A path is the unique shortest sequence of edges from a node to an ancestor. The length of a path is the number of edges in it. The height of a node = tree.height – length(path(root, node)). The height of a tree is the height of its root. The depth (level) of a node is the length of the path from the root to the node. The degree of a node is the number of children it has. The degree (arity) of a tree is the maximum degree of its nodes.

7 If node N2 is the root of a direct subtree of node N1, then node N2 is a child of node N1 and node N1 is the parent of node N2. The root node of a tree has no parent node in that tree. A node N1 is an ancestor of a node N2, if N1 is the parent of node N2 or if N1 is the ancestor of a parent of N2 (this is a recursive definition: ancestors(N) = parent(N) + ancestors(parent(N))). A node N2 is a descendent of a node N1, if N1 is an ancestor of node N2. A node with no children is called a leaf node. A node with children is called an interior node. Two nodes are siblings if they have the same parent.

8 Height(10)? Height(11)? Path (43,99)? Path (21,67)? Children(25)? Descendants(43)? Siblings(25)? Depth(99)? Depth(10)? Arity(tree)? Degree(99)? Degree(10)?

9 A binary tree is a tree in which nodes can have a maximum of 2 children. A binary tree is oriented if every node with 2 children differentiates between the children by calling them the left child and right child and every node with one child designates it either as a left child or right child. A node in a binary tree is full if it has arity 2. A full binary tree of height h has leaves only on level h and each of its interior nodes is full. A complete binary tree of height h is a full binary tree of height h with 0 or more of the rightmost leaves of level h removed.

10 Examples of Binary Trees Expert System Example

11 Binary Tree Traversals There are four common binary tree traversals: – Preorder: process root then left subtree then right subtree – Inorder: process left subtree then root then right subtree – Postorder: process left subtree then right subtree then root – Levelorder: process nodes of level i, before processing nodes of level i + 1

12  Linked Structure for Trees A node is represented by an object storing Element Element Parent node Parent node Sequence of children nodes Sequence of children nodes Node objects implement the Position ADT B D A CE F B  ADF  C  E

13 Array-Based Representation of Binary Trees nodes are stored in an array … let rank(node) be defined as follows: rank(root) = 1 if node is the left child of parent(node), rank(node) = 2*rank(parent(node)) if node is the right child of parent(node), rank(node) = 2*rank(parent(node))+1 1 23 6 7 45 1011 A HG FE D C B J

14 Arithmetic Expression Tree Binary tree associated with an arithmetic expression internal nodes: operators internal nodes: operators external nodes: operands external nodes: operands Example: arithmetic expression tree for the expression (2  ( a  1)  (3  b))    2 a1 3b

15 Decision Tree Binary tree associated with a decision process internal nodes: questions with yes/no answer internal nodes: questions with yes/no answer external nodes: decisions external nodes: decisions Example: dining decision Want a fast meal? How about coffee?On expense account? StarbucksSpike’sAl FornoCafé Paragon Yes No YesNoYesNo

16 Print Arithmetic Expressions Specialization of an inorder traversal print operand or operator when visiting node print operand or operator when visiting node print “(“ before traversing left subtree print “(“ before traversing left subtree print “)“ after traversing right subtree print “)“ after traversing right subtree Algorithm printExpression(v) if hasLeft (v) print( “(’’ ) inOrder (left(v)) print(v.element ()) if hasRight (v) inOrder (right(v)) print ( “)’’ )    2 a1 3b ((2  ( a  1))  (3  b))

17 Evaluate Arithmetic Expressions Specialization of a postorder traversal recursive method returning the value of a subtree recursive method returning the value of a subtree when visiting an internal node, combine the values of the subtrees when visiting an internal node, combine the values of the subtrees Algorithm evalExpr(v) if isExternal (v) return v.element () else x  evalExpr(leftChild (v)) y  evalExpr(rightChild (v))   operator stored at v return x  y    2 51 32

Download ppt "1 Chapter 7 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."

Similar presentations