Cpt S 122 Data Structures Templatized Tree Nirmalya

  • Slides: 35
Download presentation
Cpt S 122 – Data Structures Templatized Tree Nirmalya Roy School of Electrical Engineering

Cpt S 122 – Data Structures Templatized Tree Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

Topics n Templated Tree ¡ ¡ insert. Node inoder Traversal preorder Traversal postorder Traversal

Topics n Templated Tree ¡ ¡ insert. Node inoder Traversal preorder Traversal postorder Traversal

Trees n n Linked lists, stacks and queues are linear data structures. A tree

Trees n n Linked lists, stacks and queues are linear data structures. A tree is a nonlinear, two-dimensional data structure. ¡ n arrays arrange data linearly, binary trees can be envisioned as storing data in two dimensions Tree nodes contain two or more links. ¡ trees whose nodes all contain two links (none, one or both of which may be null).

Trees (cont. ) Refer to nodes A, B, C and D. n The root

Trees (cont. ) Refer to nodes A, B, C and D. n The root node (node B) is the first node in a tree. n Each link in the root node refers to a child (nodes A and D). n The left child (node A) is the root node of the left subtree (which contains only node A), and the right child (node D) is the root node of the right subtree (which contains nodes D and C). n The children of a given node are called siblings (e. g. , nodes A and D are siblings). n A node with no children is a leaf node (e. g. , nodes A and C are leaf nodes). n

Trees (cont. ) n A binary search tree (with no duplicate node values) ¡

Trees (cont. ) n A binary search tree (with no duplicate node values) ¡ ¡ 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.

Tree. Node Class Template

Tree. Node Class Template

Tree. Node Class Template (cont. )

Tree. Node Class Template (cont. )

Tree. Node & Tree Class Template (cont. ) n The Tree. Node class template

Tree. Node & Tree Class Template (cont. ) n The Tree. Node class template definition declares Tree<NODETYPE> as its friend. ¡ This makes all member functions of a given specialization of class template Tree friends of the corresponding specialization of class template Tree. Node, n ¡ They can access the private members of Tree. Node objects of that type. Tree. Node template parameter NODETYPE is used as the template argument for Tree in the friend declaration n Tree. Nodes specialized with a particular type can be processed only by a Tree specialized with the same type (e. g. , a Tree of int values manages Tree. Node objects that store int values).

Tree. Node Class Template (cont. ) n Tree. Node’s private data ¡ ¡ ¡

Tree. Node Class Template (cont. ) n Tree. Node’s private data ¡ ¡ ¡ n The constructor sets data to the value supplied as a constructor argument ¡ n the node’s data value pointer left. Ptr (to the node’s left subtree) pointer right. Ptr (to the node’s right subtree). sets pointers left. Ptr and right. Ptr to zero (thus initializing this node to be a leaf node). Member function get. Data returns the data value.

Tree Class Template

Tree Class Template

Tree Class Template (cont. )

Tree Class Template (cont. )

Tree Class Template (cont. )

Tree Class Template (cont. )

Tree Class Template (cont. )

Tree Class Template (cont. )

Tree Class Template (cont. ) n Class template Tree has as private data root.

Tree Class Template (cont. ) n Class template Tree has as private data root. Ptr ¡ ¡ ¡ a pointer to the tree’s root node. public member functions insert. Node (that inserts a new node in the tree) pre. Order. Traversal, in. Order. Traversal and post. Order. Traversal, n n each of which walks the tree in the designated manner. Each of these member functions calls its own recursive utility function to perform the appropriate operations on the internal representation of the tree ¡ ¡ access the underlying private data to perform these functions. recursion requires us to pass in a pointer that represents the next subtree to process.

Tree Class Template (cont. ) n n The Tree constructor initializes root. Ptr to

Tree Class Template (cont. ) n n The Tree constructor initializes root. Ptr to zero to indicate that the tree is initially empty. The Tree class’s utility function insert. Node. Helper is called by insert. Node to recursively insert a node into the tree. ¡ A node can only be inserted as a leaf node in a binary search tree. If the tree is empty, a new Tree. Node is created, initialized and inserted in the tree. If the tree is not empty, the program compares the value to be inserted with the data value in the root node. ¡ If the insert value is smaller , the program recursively calls insert. Node. Helper to insert the value in the left subtree. ¡ If the insert value is larger, the program recursively calls insert. Node. Helper to insert the value in the right subtree.

Tree Class Template (cont. ) n If the value to be inserted is identical

Tree Class Template (cont. ) n If the value to be inserted is identical to the data value in the root node, the program prints the message "dup" ¡ n n returns without inserting the duplicate value into the tree. insert. Node passes the address of root. Ptr to insert. Node. Helper so it can modify the value stored in root. Ptr (i. e. , the address of the root node). To receive a pointer to root. Ptr (which is also a pointer) ¡ insert. Node. Helper’s first argument is declared as a pointer to a Tree. Node.

Tree Class Template Traversal n Member functions ¡ ¡ n in. Order. Traversal, pre.

Tree Class Template Traversal n Member functions ¡ ¡ n in. Order. Traversal, pre. Order. Traversal post. Order. Traversal traverse the tree and print the node values. Consider the following binary search tree.

Tree Class Template preorder Traversal n pre. Order traversal is: root, left, right

Tree Class Template preorder Traversal n pre. Order traversal is: root, left, right

Preorder Traversal n n Function pre. Order. Traversal invokes utility function pre. Order. Helper

Preorder Traversal n n Function pre. Order. Traversal invokes utility function pre. Order. Helper to perform the preorder traversal of the binary tree. The steps for an preorder traversal are: ¡ ¡ ¡ n n Process the value in the node. Traverse the left subtree with a preorder traversal. (This is performed by the call to pre. Order. Helper. ) Traverse the right subtree with a preorder traversal. (This is performed by the call to pre. Order. Helper. ) The value in each node is processed as the node is visited. After the value in a given node is processed, the values in the left subtree are processed. Then the values in the right subtree are processed. The preorder traversal of the tree in example Figure is n 27 13 6 17 42 33 48

Tree Class Template in. Order Traversal n in. Order traversal is: left, root, right

Tree Class Template in. Order Traversal n in. Order traversal is: left, root, right

in. Order Traversal n n Function in. Order. Traversal invokes utility function in. Order.

in. Order Traversal n n Function in. Order. Traversal invokes utility function in. Order. Helper to perform the inorder traversal of the binary tree. The steps for an inorder traversal are: ¡ ¡ ¡ n Traverse the left subtree with an inorder traversal. (This is performed by the call to in. Order. Helper) Process the value in the node—i. e. , print the node value. Traverse the right subtree with an inorder traversal. (This is performed by the call to in. Order. Helper. ) The value in a node is not processed until the values in its left subtree are processed, because each call to in. Order. Helper immediately calls in. Order. Helper again with the pointer to the left subtree.

in. Order Traversal n The inorder traversal of the tree in example Figure is

in. Order Traversal n The inorder traversal of the tree in example Figure is n n n 6 13 17 27 33 42 48 Note that the inorder traversal of a binary search tree prints the node values in ascending order. The process of creating a binary search tree actually sorts the data ¡ this process is called the binary tree sort.

Tree Class Template post. Order Traversal n post. Order traversal is: left, right, root

Tree Class Template post. Order Traversal n post. Order traversal is: left, right, root

post. Order Traversal n n Function post. Order. Traversal invokes utility function post. Order.

post. Order Traversal n n Function post. Order. Traversal invokes utility function post. Order. Helper to perform the postorder traversal of the binary tree. The steps for a postorder traversal are: ¡ ¡ ¡ n n Traverse the left subtree with a postorder traversal. (This is performed by the call to post. Order. Helper. ) Traverse the right subtree with a postorder traversal. (This is performed by the call to post. Order. Helper. ) Process the value in the node. The value in each node is not printed until the values of its children are printed. The post. Order. Traversal of the tree in example Figure is n 6 17 13 33 48 42 27

Tree Class Template Test Program

Tree Class Template Test Program

Test Program (cont. )

Test Program (cont. )

Test Program (cont. )

Test Program (cont. )

Test Program (cont. )

Test Program (cont. )

BST Applications: Duplicate Elimination n n The binary search tree facilitates duplicate elimination. An

BST Applications: Duplicate Elimination n n The binary search tree facilitates duplicate elimination. An attempt to insert a duplicate value will be recognized ¡ n n a duplicate will follow the same “go left” or “go right” decisions on each comparison as the original value did. The duplicate will eventually be compared with a node in the tree containing the same value. The duplicate value may simply be discarded at this point.

Binary Tree Search n n Searching a binary tree for a value that matches

Binary Tree Search n n Searching a binary tree for a value that matches a key value is fast. If the tree is balanced, then each branch contains about half the number of nodes in the tree. ¡ ¡ n n Each comparison of a node to the search key eliminates half the nodes. This is called an O(log n) algorithm (Big O notation). A binary search tree with n elements would require a maximum of log 2 n comparisons either to find a match or to determine that no match exists. Searching a (tightly packed) 1, 000 element binary search tree requires no more than 20 comparisons ¡ 220 > 1, 000.

Other Binary Tree Operations n The level order traversal of a binary tree visits

Other Binary Tree Operations n The level order traversal of a binary tree visits the nodes of the tree row-by-row starting at the root node level. ¡ ¡ On each level of the tree, the nodes are visited from left to right. The level order traversal is not a recursive algorithm.

Exercise n Implement the level order binary tree traversal using a common data structure

Exercise n Implement the level order binary tree traversal using a common data structure we have discussed in the class. ¡ Write the pseudo code of this algorithm.

Level Order Binary Tree Traversal n n Use the Queue data structure to control

Level Order Binary Tree Traversal n n Use the Queue data structure to control the output of the level order binary tree traversal. Algorithm ¡ ¡ Insert/enqueue the root node in the queue While there are nodes left in the queue, n n Get/dequeue the node in the queue Print the node’s value If the pointer to the left child of the node is not null ¡ Insert/enqueue the left child node in the queue If the pointer to the right child of the node is not null ¡ Insert/enqueue the right child node in the queue

Other Common Tree Data Strictures n Binary search trees (BSTs) ¡ ¡ Support O(log

Other Common Tree Data Strictures n Binary search trees (BSTs) ¡ ¡ Support O(log 2 N) operations Balanced trees n n AVL trees, Splay trees B-trees for accessing secondary storage

Conclusions n n n Accessing elements in a linear linked list can be prohibitive

Conclusions n n n Accessing elements in a linear linked list can be prohibitive especially for large amounts of input. Trees are simple data structures for which the running time of most operations is O(log N) on average. For example, if N = 1 million: ¡ ¡ Searching an element in a linear linked list requires at most O(N) comparisons (i. e. , 1 million comparisons) Searching an element in a binary search tree requires O(log 2 N) comparisons ( 20 comparisons)