1 / 25

Trees

Trees. Linked lists, stacks and queues are linear data structures A tree is a nonlinear, two-dimensional data structure Tree nodes contain two or more links There are two types of trees Binary trees Multiway trees. 1. The nodes of the binary tree contains a maximum two links

Télécharger la présentation

Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Trees • Linked lists, stacks and queues are linear data structures • A tree is a nonlinear, two-dimensional data structure • Tree nodes contain two or more links • There are two types of trees • Binary trees • Multiway trees 1

  2. The nodes of the binary tree contains a maximum two links A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the root The root node is the first node in the tree Binary trees 2

  3. Binary tree • Each link in the root node refers to a child (nodes A and D) • The left child (node A) is the root node of the left subtree (which contains only node A) • The right child (node D) is the root node of the right subtree (which contains nodes D and C) • The children of a single node are called siblings (e.g., nodes A and D are siblings) • A node with no children is called a leaf node (e.g., nodes A and C are leaf nodes) 3

  4. Traversal of Binary Trees • One of the most important operations on a binary tree is traversal • Traversal is moving through all the nodes of the binary tree, visiting each one in turn • The action taken when visitingeach node will depend on the application • For trees, there are many different orders in which we could traverse all the nodes

  5. Traversal of Binary Trees • At a given node, then, there are three tasks are to be done in some order • We shall visit the node itself • We shall traverse its left subtree • We shall traverse its right subtree • The key distinction in traversal orders is to decide if the node itself to be visited before traversing either subtree, between the subtrees • Or after traversing both subtrees • Let the tasks of visiting a node is denoted by V, traversing the left subtree L, and traversing the right subtree R • Then there are six ways to arrange them VLR LVR LRV VRL RVL RLV

  6. Standard Traversal Orders • By standard convention, these six ways can be reduced to three ways • This can be achieved by considering only the ways in which the left subtree is traversed before the right • The three ways with left before right are given special names V L R L V R L R V preorder inorder postorder

  7. preorder, inorder, and postorder • With preorder traversal, the root node is visited before the subtrees • With inorder traversal, the root node visited between them • With postorder traversal, the root node is visited after both of the subtrees

  8. Example 1 • Consider the binary tree shown on the right • Under preorder traversal, the root, labeled 1, is visited first • Then the traversal moves to the left subtree • Then preorder traversal moves to the right subtree of the root, finally visiting the node labeled 3.

  9. Example 1 • Under the ineorder traversal, the root, labeled 2, is visited first • Then the traversal moves to node 1 • Then preorder traversal moves to node 3 • Under postorder node 2 is visited first followed by node 3 then finally back to node

  10. Example 2 • Consider the binary tree shown on the right • Under preorder traversal, the sequence will be 1,2,3,4,5 • The inorder sequence will be 1,4,3,5,2 • Then post order traversal will be 4,5,3,2,1

  11. Expression Trees • An expression tree is built up from the simple operands and operators of an (arithmetical or logical) expression • This can be done by • placing the simple operands as the leaves of a binary tree • the operators as the interior nodes

  12. For a unary operator, one of the two subtrees will be empty • Traditionally some unary operators are written to the left of their operands such as • ‘−’ (unary negation) • the standard functions like log( ) and cos() • Other unary operators are written on the right such as • The factorial function ( )! • The function that takes the square of a number ()2

  13. Polish form • Traversal of an expression tree in preorder yields the prefix form in which every operator is written before its operand(s) • Inorder traversal gives the infix form (the customary way to write the expression) • Postorder traversal gives the postfix form, in which all operators appear after their operand(s)

  14. A binary search tree (with no duplicate node values) has the following characteristic The values in any left subtree are less than the value in its parent node, The values in any right subtree are greater than the value in its parent node Binary Search Trees 15

  15. Binary search tree 16

  16. // Fig. 21.20: Treenode.h // Template TreeNode class definition. #ifndef TREENODE_H #define TREENODE_H // forward declaration of class Tree template< typename NODETYPE > class Tree; // TreeNode class-template definition template< typename NODETYPE > class TreeNode { friend class Tree< NODETYPE >; public: // constructor TreeNode( const NODETYPE &d ) : leftPtr( 0 ), // pointer to left subtree data( d ), // tree node data rightPtr( 0 ) // pointer to right substree { // empty body } // end TreeNode constructor // return copy of node's data NODETYPE getData() const { return data; } // end getData function private: TreeNode< NODETYPE > *leftPtr; // pointer to left subtree NODETYPE data; TreeNode< NODETYPE > *rightPtr; // pointer to right subtree }; // end class TreeNode #endif Implementing the Binary Search Tree Program

  17. // Fig. 21.21: Tree.h // Template Tree class definition. #ifndef TREE_H #define TREE_H #include <iostream> using std::cout; using std::endl; #include "Treenode.h" // Tree class-template definition template< typename NODETYPE > class Tree { public: Tree(); // constructor void insertNode( const NODETYPE & ); void preOrderTraversal() const; void inOrderTraversal() const; void postOrderTraversal() const; private: TreeNode< NODETYPE > *rootPtr; // utility functions void insertNodeHelper( TreeNode< NODETYPE > **, const NODETYPE & ); void preOrderHelper( TreeNode< NODETYPE > * ) const; void inOrderHelper( TreeNode< NODETYPE > * ) const; void postOrderHelper( TreeNode< NODETYPE > * ) const; }; // end class Tree Defining Tree class

  18. template< typename NODETYPE > void Tree< NODETYPE >::insertNode( const NODETYPE &value ) { insertNodeHelper( &rootPtr, value ); } // end function insertNode // utility function called by insertNode; receives a pointer // to a pointer so that the function can modify pointer's value template< typename NODETYPE > void Tree< NODETYPE >::insertNodeHelper( TreeNode< NODETYPE > **ptr, const NODETYPE &value ) { // subtree is empty; create new TreeNode containing value if ( *ptr == 0 ) *ptr = new TreeNode< NODETYPE >( value ); else // subtree is not empty { // data to insert is less than data in current node if ( value < ( *ptr )->data ) insertNodeHelper( &( ( *ptr )->leftPtr ), value ); else { // data to insert is greater than data in current node if ( value > ( *ptr )->data ) insertNodeHelper( &( ( *ptr )->rightPtr ), value ); else // duplicate data value ignored cout << value << " dup" << endl; } // end else } // end else } // end function insertNodeHelper Inserting node in the tree

  19. // begin preorder traversal of Tree template< typename NODETYPE > void Tree< NODETYPE >::preOrderTraversal() const { preOrderHelper( rootPtr ); } // end function preOrderTraversal // utility function to perform preorder traversal of Tree template< typename NODETYPE > void Tree< NODETYPE >::preOrderHelper( TreeNode< NODETYPE > *ptr ) const { if ( ptr != 0 ) { cout << ptr->data << ' '; // process node preOrderHelper( ptr->leftPtr ); // traverse left subtree preOrderHelper( ptr->rightPtr ); // traverse right subtree } // end if } // end function preOrderHelper begin preorder traversal of Tree

  20. Begin inorder traversal // begin inorder traversal of Tree template< typename NODETYPE > void Tree< NODETYPE >::inOrderTraversal() const { inOrderHelper( rootPtr ); } // end function inOrderTraversal // utility function to perform inorder traversal of Tree template< typename NODETYPE > void Tree< NODETYPE >::inOrderHelper( TreeNode< NODETYPE > *ptr ) const { if ( ptr != 0 ) { inOrderHelper( ptr->leftPtr ); // traverse left subtree cout << ptr->data << ' '; // process node inOrderHelper( ptr->rightPtr ); // traverse right subtree } // end if } // end function

  21. Begin postorder traversal of Tree // begin postorder traversal of Tree template< typename NODETYPE > void Tree< NODETYPE >::postOrderTraversal() const { postOrderHelper( rootPtr ); } // end function postOrderTraversal // utility function to perform postorder traversal of Tree template< typename NODETYPE > void Tree< NODETYPE >::postOrderHelper( TreeNode< NODETYPE > *ptr ) const { if ( ptr != 0 ) { postOrderHelper( ptr->leftPtr ); // traverse left subtree postOrderHelper( ptr->rightPtr ); // traverse right subtree cout << ptr->data << ' '; // process node } // end if } // end

  22. Inorder Traversal Algorithm • Traverse the left subtree with an inorder traversal. (This is performed by the call to inOrderHelper) • Process the value in the nodei.e., print the node value • Traverse the right subtree with an inorder traversal. (This is performed by the call to inOrderHelper)

  23. Preorder Traversal Algorithm • Process the value in the node • Traverse the left subtree with a preorder traversal. (This is performed by the call to preOrderHelper) • Traverse the right subtree with a preorder traversal. (This is performed by the call to preOrderHelper)

  24. Postorder Traversal Algorithm • Traverse the left subtree with a postorder traversal. (This is performed by the call to postOrderHelper • Traverse the right subtree with a postorder traversal. (This is performed by the call to postOrderHelper • Process the value in the node

More Related