# chapter 06 compiled by: dr. mohammad omar alhawarat trees

Post on 04-Jan-2016

216 views

Embed Size (px)

TRANSCRIPT

CS2320: Data structures & Algorithms

Chapter 06Compiled by: Dr. Mohammad Omar AlhawaratTrees1Definition of TreeA tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root node) to every other node in the tree

2Hierarchical OrganizationExample: File Directories

33Tree 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 each44Example of a Tree A C B D E F Groot5Example of a Tree A C B D E F GrootIn a tree, every pair of linked nodes have a parent-child relationship (the parent is closer to the root)6Example of a Tree (Cont.) A C B D E F GrootFor example, C is a parent of G7Example of a Tree (Cont.) A C B D E F GrootE and F are children of D8Example of a Tree (Cont.) A C B D E F GrootThe root node is the only node that has no parent.9Example of a Tree (Cont.) A C B D E F GrootLeaf nodes (or leaves for short) have no children.10Subtrees A B C I K D E F J G HsubtreerootA subtree is a part of a tree that is a tree in itself11Subtrees (Cont.) A B C I K D E F J G HsubtreerootIt normally includes each node reachable from its root.12Subtrees (Cont.) A B C I K D E F J G HrootEven though this looks like a subtree in itself13Binary TreesA binary tree is a tree in which each node can only have up to two children14Not a Binary Tree A B C I K D E F J G Hroot15Example of a Binary Tree A B C I K E J G HrootThe links in a tree are often called edges16Levels A B C I K E J G Hrootlevel 0level 1level 2level 3The level of a node is the number of edges in the path from the root node to this node17Full Binary Tree B D Hroot I A E J K C F L M G N OIn a full binary tree, each node has two children except for the nodes on the last level, which are leaf nodes 18Complete Binary TreesA complete binary tree is a binary tree that is eithera full binary treeORa tree that would be a full binary tree but it is missing the rightmost nodes on the last level19Complete Binary Trees (Cont.) B D Hroot I A E C F G20Complete Binary Trees (Cont.) B D Hroot I A E J K C F L G21Full Binary Trees B D Hroot I A E J K C F L M G N OA full binary tree is also a complete binary tree.22The 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:

Full Binary Trees (Cont.)23To 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.Balanced Binary Trees24Binary TreesA binary tree is either empty or has the following form

Where Tleft and Tright are binary trees

2525Binary 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)

2626

Binary Trees2727A tree is a set of nodes that either:is empty orhas a designated node, called the root, from which hierarchically descend zero or more subtrees, which are also trees.Recursive definition of a tree28Recursive definition of a tree (Cont.)

29Tree TraversalFour meaningful orders in which to traverse a binary tree.PreorderInorderPostorderLevel order3030Tree Traversal (Cont.)

3131In Preorder, the rootis visited before (pre)the subtrees traversalsIn Inorder, the root isvisited in-between left and right subtree traversalIn Postorder, the rootis visited after (post)the subtrees traversalsPreorder Traversal:Visit the rootTraverse left subtreeTraverse right subtreeInorder Traversal:Traverse left subtreeVisit the rootTraverse right subtreePostorder Traversal:Traverse left subtreeTraverse right subtreeVisit the rootTree Traversal (Cont.)32Tree Traversal (Cont.)Visiting a nodeProcessing 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 subtree3333Tree Traversal (Cont.)Preorder traversal: visit root before the subtrees

3434Tree Traversal (Cont.)Inorder traversal: visit root between visiting the subtrees

3535Tree Traversal (Cont.)Postorder traversal: visit root after visiting the subtrees

These are examples of a depth-first traversal.3636Tree Traversal (Cont.)Level-order traversal: begin at the root, visit nodes one level at a time

This is an example of a breadth-first traversal.3737Tree Traversals Example 1

38Assume: visiting a node is printing its labelPreorder: 1 3 5 4 6 7 8 9 10 11 12Inorder:4 5 6 3 1 8 7 9 11 10 12Postorder:4 6 5 3 8 11 12 10 9 7 113119846571210Tree Traversals Example 239Assume: visiting a node is printing its dataPreorder: 15 8 2 6 3 711 10 12 14 20 27 22 30Inorder: 2 3 6 7 8 10 1112 14 15 20 22 27 30Postorder: 3 7 6 2 10 1412 11 8 22 30 27 20 1561582371110141220272230Tree Traversals Example 340Tree Traversals Example 4

41Examples of Binary TreesExpression Trees

4242Example: 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)434343

43Cpt S 223Washington State UniversityTree Traversal - SummaryLevel 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.4444Questions?Questions ?4545

Recommended