4 Tree TerminologyNodes at a given level are children of nodes of previous levelNode with children is the parent node of those childrenNodes with same parent are siblingsNode with no children is a leaf nodeThe only node with no parent is the root nodeAll others have one parent each
22 Full Binary Trees A full binary tree is also a complete binary tree. rootABCDEFGHIJKLMNO
23 Full Binary Trees (Cont.) The formula for the maximum number of nodes is derived from the fact that each node can have only two descendants. Given a height of the binary tree, H, the maximum number of nodes in the full binary tree is given as follows:
24 Balanced Binary TreesTo Assure that a binary tree is Balanced one the following algorithms is used:AVL Trees.Red-Black Trees.2-3 Trees and the general case (B-Trees) B-Trees are used with Fetching data from Large Databases.
25 Binary Trees A binary tree is either empty or has the following form Where Tleft and Tright are binary trees
26 Binary TreesEvery nonleaf in a full binary tree has exactly two childrenA complete binary tree is full to its next-to-last levelLeaves on last level filled from left to rightThe height of a binary tree with n nodes that is either complete or full is log2(n + 1)
32 Tree Traversal (Cont.) In Preorder, the root is visited before (pre) the subtrees traversalsIn Inorder, the root isvisited in-between leftand right subtree traversalIn Postorder, the rootis visited after (post)Preorder Traversal:Visit the rootTraverse left subtreeTraverse right subtreeInorder Traversal:Traverse left subtreeVisit the rootTraverse right subtreePostorder Traversal:Traverse left subtreeTraverse right subtreeVisit the root
33 Tree Traversal (Cont.) Visiting a node Processing the data within a nodeThis is the action performed on each node during traversal of a treeA traversal can pass through a node without visiting it at that momentFor a binary treeVisit the rootVisit all nodes in the root's left subtreeVisit all nodes in the root's right subtree
34 Tree Traversal (Cont.)Preorder traversal: visit root before the subtrees
35 Tree Traversal (Cont.)Inorder traversal: visit root between visiting the subtrees
36 Tree Traversal (Cont.)Postorder traversal: visit root after visiting the subtreesThese are examples of a depth-first traversal.
37 Tree Traversal (Cont.)Level-order traversal: begin at the root, visit nodes one level at a timeThis is an example of a breadth-first traversal.
43 Example: Expression Trees Cpt S 223Example: Expression TreesStore expressions in a binary treeLeaves of tree are operands (e.g., constants, variables)Other internal nodes are unary or binary operatorsUsed by compilers to parse and evaluate expressionsArithmetic, logic, etc.E.g., (a + b * c)+((d * e + f) * g)434343Washington State University
44 Tree Traversal - Summary Level order traversal is sometimes called breadth- first.The other traversals are called depth-first.Traversal takes Θ(n) in both breadth-first and depth- first.Memory usage in a perfect tree is Θ(log n) in depth- first and Θ(n) in breadth-first traversal.