Sessions 26 and 27 - Data Structures & Algorithm

Subtree right child is root of right subtree leaf

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ty processing a double Queue The list is: 1.1 The list is: 1.1 2.2 Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 56 The list is: 1.1 2.2 3.3 The list is: 1.1 2.2 3.3 4.4 1.1 dequeued The list is: 2.2 3.3 4.4 2.2 dequeued The list is: 3.3 4.4 3.3 dequeued The list is: 4.4 4.4 dequeued The list is empty All nodes destroyed All nodes destroyed Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 57 Trees Linear data structures Lists, queues, stacks Trees Nonlinear, two-dimensional Tree nodes have 2 or more links Binary trees have exactly 2 links/node None, both, or one link can be null Sharif University of Technology Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 58 Trees Terminology Root node: first node on tree Link refers to child of node Left child is root of left subtree Right child is root of right subtree Leaf node: node with no children Trees drawn from root downwards B A D C Sharif University of Technology Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 59 Trees Binary search tree Values in left subtree less than parent node Values in right subtree greater than parent Does not allow duplicate values (good way to remove them) Fast searches, log2n comparisons for a balanced tree Sharif University of Technology Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 60 Trees Inserting nodes Use recursive function Begin at root If current node empty, insert new node here (base case) Otherwise, If value > node, insert into right subtree If value < node, insert into left subtree If neither > nor <, must be = Ignore duplicate Sharif University of Technology Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 61 Trees Tree traversals In-order (print tree values from least to greatest) Traverse left subtree (call function again) Print node Traverse right subtree Preorder Print node Traverse left subtree Traverse right subtree Postorder Traverse left subtree Traverse rigth subtree Print node Sharif University of Technology Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 62 Trees Upcoming program Create 2 template classes TreeNode data leftPtr rightPtr Tree rootPtr Functions InsertNode inOrderTraversal preOrderTraversal postOrderTraversal Sharif University of Technology Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 63 // return copy of node's data 1 2 3 4 5 6 7 8 9 10 11 12 13 14 14 15 16 17 18 19 20 21 22 23 24 NODETYPE getData() const // Template TreeNode class definition. #ifndef TREENODE_H #define TREENODE_H { // forward declaration of class Tree template< class NODETYPE > class Tree; private: template< class NODETYPE > class TreeNode { friend class Tree< NODETYPE >; NODETYPE data; public: // constructor TreeNode( const NODETYPE &d ) : leftPtr( 0 ), data( d ), rightPtr( 0 ) { // empty body return dat...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online