Presentation is loading. Please wait.

Presentation is loading. Please wait.

TREE Ahsan Ahmed College of Computer and Information Science Majma’ah University 1.

Similar presentations


Presentation on theme: "TREE Ahsan Ahmed College of Computer and Information Science Majma’ah University 1."— Presentation transcript:

1 TREE Ahsan Ahmed College of Computer and Information Science Majma’ah University 1

2 Tree in C++ 2  Outlines:  What is a Tree  Tree Terminology  ADT: Tree  Representation of Tree Node  Left Child, Right Sibling Representation  Tree Traversal  Preorder Traversal  Postorder Traversal  Inorder Traversal

3 Tree in C++ 3  Outlines:  Binary Tree  ADT: Binary Tree  Differences Between A Tree and A Binary Tree  Data Structure for Binary Trees  Arithmetic Expression Tree  Maximum Number of Nodes in a Binary Tree  Full Binary Tree

4 Tree in C++ 4 What is a Tree A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. consists of nodes with a parent-child relation. Applications: Organization charts File systems Programming environments Computers”R”Us SalesR&DManufacturing LaptopsDesktops Local International EuropeAsiaCanada

5 Tree in C++ 5 Tree Terminology Root: node without parent (A) Siblings: nodes share the same parent Internal node: node with at least one child (A, B, C, F) External node (leaf): node without children (E, I, J, K, G, H, D) Ancestors of a node: parent, grandparent, grand-grandparent, etc. Descendant of a node: child, grandchild, grand-grandchild, etc. A B D C GH E F I J K subtree

6 Tree in C++ 6 Tree Terminology …. Cont. Depth of a node: number of ancestors Height of a tree: maximum depth of any node (3) Degree of a node: the number of its children Degree of a tree: the maximum number of its node. Subtree: tree consisting of a node and its descendants. A B D C GH E F I J K subtree

7 Tree in C++ 7 ADT: Tree We use positions (p) 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 in C++ 8 ADT: Tree … Cont. 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 Tree in C++ 9 Representation of Tree Node List Representation ( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) ) The root comes first, followed by a list of links to sub-trees DataLink 1Link 2…Link n How many link fields are needed in such a representation?

10 Tree in C++ 10 Representation of Tree Node Every tree node: object – useful information children – pointers to its children Data      

11 Tree in C++ 11 Representation of Tree Node A node is represented by an object storing Element. Parent node. Sequence of children nodes. B D A CE F  B  ADF  C  E

12 Tree in C++ 12 Left Child, Right Sibling Representation Data Left Child Right Sibling A BCD IHGFE JKL

13 Tree in C++ 13 Tree Traversal Two main methods: Preorder Postorder Preorder: visit the root traverse in preorder the children (subtrees) Postorder traverse in postorder the children (subtrees) visit the root

14 Tree in C++ 14 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 Application: print a structured document Become Rich 1. Motivations3. Success Stories2. Methods 2.1 Get a CS BSc 2.2 Start a Web Site 1.1 Enjoy Life 1.2 Help Poor Friends 2.3 Acquired by Google 1 2 3 5 4 678 9

15 Tree in C++ 15 Postorder Traversal In a postorder traversal, a node is visited after its descendants Application: compute space used by files in a directory and its subdirectories cs16/ homeworks/ todo.txt 1K programs/ DDR.cpp 10K Stocks.cpp 25K h1c.doc 3K h1nc.doc 2K Robot.cpp 20K 9 3 1 7 2 456 8

16 Tree in C++ 16 Inorder Traversal In an inorder traversal a node is visited after its left subtree and before its right subtree. 3 1 2 5 6 79 8 4

17 Tree in C++ 17 Binary Tree A binary tree is a tree with the following properties: Each internal node has at most two children (degree of two). 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 A B C FG D E H I

18 Tree in C++ 18 ADT: Binary Tree The Binary Tree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree ADT. Additional methods: position leftChild(p) position rightChild(p) position sibling(p) Update methods may be defined by data structures implementing the Binary Tree ADT.

19 Tree in C++ 19 ADT: Binary Tree Examples of the Binary Tree A B C G E I D H F Complete Binary Tree 1 2 3 4 A B A B Skewed Binary Tree E C D 5

20 Tree in C++ 20 Differences Between A Tree and A Binary Tree a) The subtrees of a binary tree are ordered; those of a tree are not ordered. b) Binary tree can be empty while general tree can not. c) Nodes in binary tree can not more than 2 where as in tree there is no limit. Are different when viewed as binary trees. Are the same when viewed as trees. A B A B

21 Tree in C++ 21 Data Structure for Binary Trees A node is represented by an object storing Element Parent node Left child node Right child node B D A CE   B AD CE 

22 Tree in C++ 22 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))    2 a1 3b

23 23 Infix: All binary operators appear between the two operands Example: ((1+2) * (3-4)) Prefix: All operators appear near the beginning of the equation, with the two operands appearing right after. Example: * + 1 2 – 3 4 Postfix: The operators will appear near the end Example: 1 2 + 3 4 - * Assignment: (9 + (12 / 3)) * ((12 /4) – 2)

24 Tree in C++ 24 Decision Tree Binary tree associated with a decision process internal nodes: questions with yes/no answer external nodes: decisions Example: dining decision Want a fast meal? How about drink?How about shopping? Fruits Coffee Drive to City center Continue driving Yes No YesNoYesNo

25 Tree in C++ 25 Maximum Number of Nodes in a Binary Tree The maximum number of nodes on depth i of a binary tree is 2 i, where i>=0. The maximum nubmer of nodes in a binary tree of height k is 2 k+1 -1, where k>=0. Prove by induction

26 Tree in C++ 26 Full Binary Tree A full binary tree of a given height k has 2 k+1 –1 nodes. Height 3 full binary tree.


Download ppt "TREE Ahsan Ahmed College of Computer and Information Science Majma’ah University 1."

Similar presentations


Ads by Google