Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


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

1 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 following properties: If T is nonempty, it has a special node, called the root of T, that has no parent. Each node v of T different from the root has a unique parent node w; every node with parent w is a child of w. An edge of tree T is a pair of nodes (u, v) such that u is the parent of v, or vice versa. Nodes={A,B,C,D,E,f,G,H} Edges={(A,B),(A,E),(B,F),(B,G),(B,H), (E,C),(E,D)} A directed path from node m1 to node m k is a list of nodes m 1, m 2,..., m k such that each is the parent of the next node in the list. The length of such a path is k – 1.Example: A, E, C is a directed path of length 2. A B E CD FHG

2 What is a Tree? (contd.) A tree satisfies the following properties: 1.It has one designated node, called the root, that has no parent. 2.Every node, except the root, has exactly one parent. 3.A node may have zero or more children. 4.There is a unique directed path from the root to each node. 5 2 416 3 5 2 416 3 5 2 4 1 6 3 tree Not a tree

3 Tree Terminology Ordered tree: A tree in which the children of each node are linearly ordered (usually from left to right). Ancestor of a node v: Any node, including v itself, on the path from the root to the node. Proper ancestor of a node v: Any node, excluding v, on the path from the root to the node. A C B ED F G Ancestors of G proper ancestors of E An Ordered Tree

4 Tree Terminology (Contd.) Descendant of a node v: Any node, including v itself, on any path from the node to a leaf node (i.e., a node with no children). Proper descendant of a node v: Any node, excluding v, on any path from the node to a leaf node. Subtree of a node v: A tree rooted at a child of v. Descendants of a node C A C B ED F G Proper descendants of node B A C B ED F G subtrees of node A

5 Tree Terminology (Contd.) AA B D H C E F G J I proper ancestors of node H proper descendants of node C subtrees of A AA B D H C E F G J I parent of node D child of node D grandfather of nodes I,J grandchildren of node C

6 Tree Terminology (Contd.) Degree: The number of subtrees of a node ◦ Each of node D and B has degree 1. ◦ Each of node A and E has degree 2. ◦ Node C has degree 3. ◦ Each of node F,G,H,I,J has degree 0. Leaf: (external nodes ): A node with degree 0. Internal or interior node: a node with degree greater than 0. Siblings: Nodes that have the same parent. Size: The number of nodes in a tree. AA B D H C E F G J I An Ordered Tree with size of 10 Siblings of E Siblings of A

7 Tree Terminology (Contd.) Level (or depth) of a node v: The length of the path from v to the root. Height of a node v: The length of the longest path from v to a leaf node. ◦ The height of a tree is the height of its root mode. ◦ By definition the height of an empty tree is -1. AA B D H C E FG J I k Level 0 Level 1 Level 2 Level 3 Level 4 The height of the tree is 4. The height of node C is 3.

8 Why Trees? Trees are very important data structures in computing. They are suitable for: ◦ Hierarchical structure representation, e.g.,  File directory.  Organizational structure of an institution.  Class inheritance tree. ◦ Problem representation, e.g.,  Expression tree.  Decision tree. ◦ Efficient algorithmic solutions, e.g.,  Search trees.  Efficient priority queues via heaps.

9 Tree ADT We use positions to abstract nodes element(): return the object stored at this position. Generic methods: size(): return the number of nodes in the tree. isEmpty(): Test whether the tree has any nodes or not. iterator(): return an iterator of all the elements stored at nodes of the tree. positions(): return an iterable collection of all the nodes of the tree. replace(v,e): Replace with e and return the element stored at node v. Accessor methods : root(): return the tree's root; an error occurs if the tree is empty. parent (v): return the parent of v; an error occurs if v is the root. children(v): return an iterable collection containing the children of node v. Query methods: isInternal(v): Test whether node v is internal. isExternal(v): Test whether node v is external. isRoot(v): Test whether node v is the root.

10

11 A Linked Structure for General Trees A natural way to realize a tree T is to use a linked structure where we represent each node v of T by a position object with the following fields: A reference to the element stored at v. a link to the parent of v. and a some kind of collection (for example, a list or array) to store links to the children of v.

12 Complexity Operation Time size,isEmptyO(1) iterator, positionO(n) replaceO(1) root,parentO(1) childern(v)O(cv) ; cv # of children of node v Isexternal, isInternal, isRootO(1)

13 Tree Traversal Algorithms Depth: Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. The depth of a node v can also be recursively defined as follows: 1. If v is the root, then the depth of v is 0 2. Otherwise, the depth of v is one plus the depth of the parent of v.

14 Height: The height of a node v in a tree T is also defined recursively: 1. If v is an external node, then the height of v is 0 2. Otherwise, the height of v is one plus the maximum height of a child of v.

15 Preorder Traversal A traversal visits the nodes of a tree in a systematic manner In a preorder traversal, a node is visited before its descendants Algorithm preOrder(v) visit(v) for each child w of v preorder (w)

16 Postorder Traversal In a postorder traversal, a node is visited after its descendants Algorithm postOrder(v) for each child w of v postOrder (w) visit(v)

17 Binary Tree A binary tree is a tree with the following properties: Each internal node has at most two children (exactly two for Proper or Full binary trees) The children of a node are an ordered pair We call the children of an internal node left child and right child Alternative recursive definition: a binary tree is either a tree consisting of a single node, or a tree whose root has an ordered pair of children, each of which is a binary tree Applications: arithmetic expressions decision processes searching

18 Example showing the growth of a complete binary tree:

19 Arithmetic Expression Tree Binary tree associated with an arithmetic expression internal nodes: operators external nodes: operands Example: arithmetic expression tree for the expression (2 * (a - 1) + (3 * b))

20 Anthor Example ((((3 + 1) × 3)/((9 − 5) +2)) − ((3 × (7 − 4)) + 6))

21 Decision Tree Binary tree associated with a decision process internal nodes: questions with yes/no answer external nodes: decisions Example: dining decision

22 Recursive Binary Tree Definition Binary Tree is either empty or consists of: A node r, called the root of T and storing an element A binary tree, called the left subtree of T A binary tree, called the right subtree of T. Binary Tree ADT As an abstract data type, a binary tree is a specialization of a tree that supports three additional accessor methods: left(v): Return the left child of v; an error condition occurs if v has no left child. right(v): Return the right child of v; an error condition occurs if v has no right child. hasLeft(v): Test whether v has a left child. hasRight(v): Test whether v has a right child.

23

24 Properties of Binary Trees Notation n number of nodes e number of external nodes i number of internal nodes h height

25 Inorder Traversal In an inorder traversal a node is visited after its left subtree and before its right subtree Application: draw a binary tree x(v) = inorder rank of v y(v) = depth of v Algorithm inOrder(v) if hasLeft (v) inOrder (left (v)) visit(v) if hasRight (v) inOrder (right (v))

26

27

28

29

30

31


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

Similar presentations


Ads by Google