TREE Ahsan Ahmed College of Computer and Information Science Majma’ah University 1 Tree in C++ 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 2 Tree in C++ 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 3 Tree in C++ 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: Computers”R”Us Organization charts File systems Programming environments Local 4 Europe Sales Manufacturing International Asia Laptops Canada Desktops R&D Tree in C++ 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 E 5 D C F G H subtree I J K Tree in C++ 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 E 6 D C F G H subtree I J K Tree in C++ 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) 7 Tree in C++ 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 8 Tree in C++ 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 How many link fields are needed in such a representation? Data 9 Link 1 Link 2 … Link n Tree in C++ Representation of Tree Node Every tree node: object – useful information children – pointers to its children Data Data Data 10 Data Data Data Data Tree in C++ Representation of Tree Node A node is represented by an object storing Element. Parent node. Sequence of children nodes. B B C 11 A D A D F F E C E Tree in C++ Left Child, Right Sibling Representation Data Left Child A Right Sibling B E J 12 F K C D G H L I Tree in C++ Tree Traversal Two main methods: Preorder Postorder Preorder: visit the root traverse in preorder the children (subtrees) 13 Postorder traverse in postorder the children (subtrees) visit the root Tree in C++ 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 1 Become Rich 2 5 1. Motivations 14 9 2. Methods 3 4 1.1 Enjoy Life 1.2 Help Poor Friends 6 2.1 Get a CS BSc 7 2.2 Start a Web Site 3. Success Stories 8 2.3 Acquired by Google Tree in C++ 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 9 cs16/ 3 7 homeworks/ 15 8 todo.txt 1K programs/ 1 2 h1c.doc 3K h1nc.doc 2K 4 DDR.cpp 10K 5 Stocks.cpp 25K 6 Robot.cpp 20K Tree in C++ Inorder Traversal In an inorder traversal a node is visited after its left subtree and before its right subtree. 6 2 8 1 4 3 16 7 5 9 Tree in C++ 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 A Applications: arithmetic expressions B C decision processes D E F G searching 17 H I Tree in C++ 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 BinaryTree ADT. 18 Tree in C++ ADT: Binary Tree Examples of the Binary Tree Complete Binary Tree Skewed Binary Tree A B 1 A A 2 B B C C 3 D E D 19 E 5 4 H I F G Tree in C++ 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. A B A B Are different when viewed as binary trees. Are the same when viewed as trees. 20 Tree in C++ Data Structure for Binary Trees A node is represented by an object storing Element Parent node Left child node B Right child node B A A D D C 21 E C E Tree in C++ 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 22 a 3 1 b 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) 23 Tree in C++ 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? No Yes How about drink? 24 How about shopping? Yes No Yes Fruits Coffee Drive to City center No Continue driving Tree in C++ Maximum Number of Nodes in a Binary Tree The maximum number of nodes on depth i of a binary tree is 2i, where i>=0. The maximum nubmer of nodes in a binary tree of height k is 2k+1-1, where k>=0. Prove by induction k i k +1 2 2 -1 25 i 0 Tree in C++ Full Binary Tree A full binary tree of a given height k has 2k+1–1 nodes. 26 Height 3 full binary tree.