180 likes | 300 Views
Trees. CIS 237 – Data Structures. internal node path level of a node (depth) depth of the tree subtree.
E N D
Trees CIS 237 – Data Structures
internal node path level of a node (depth) depth of the tree subtree Trees - Terminologya tree consists of a finite set of nodes and a finite set of branches, that connect the nodes. The tree may be empty or has one node designated as the root, off of which extend subtrees • root • child • parent • sibling • descendant • leaf node
A B C D E F G H I J K L M N P Q A General Tree
Binary Trees Definition: A binary tree T is a finite set of nodes with one of the following properties: • T is a tree if the set of nodes is empty • The set consists of a root R and exactly two distinct binary trees, the left subtree TL and the right subtree TR. The nodes in T consist of node R and all the nodes in TL and TR A B C D = = = = = =
Binary Tree Nodes template <class T> class tnode { public: T nodeValue; tnode<T> *left, *right; //default constructor tnode() :left(NULL), right(NULL) {} //initialization of data members tnode(const T& item, tnode<T> *lptr =NULL, tnode<T> *rptr = NULL) : nodeValue(item), left(lptr), right (rptr) {} };
Q F X A W B Z N C Level Order Scan 1. q.push(root) 2. while !q.empty() 1. p = q.front() 2. q.pop() 3. process p->nodeValue 4. if (p->left not null) q.push(p->left) 5. if (p->right not null) q.push(p->right)
Recursive Scans N – visiting a node L - traversing left subtree R - traversing right subtree Preorder tree traversal NLR QFABXWCNZ Postorder tree traversal LRN ABFCNWZXQ Inorder tree traversal LNR AFBQCWNXZ
Inorder Scan Algorithm inOrderScan(val p <node ptr>) PRE: p points to node in tree (or null) POST: node is processed 1. if (p is not null) 1. inOrderScan(p->left) 2. process p->nodeValue 3. inOrderScan(p- >right) 2. end if 3. return
Tree Scan Algorithms • Leaf count • Depth • Copy • Display
+ + * + 2 * 10 8 5 * 9 3 23 Expression Trees
Infix for Expression Trees Algorithm infix (val tree <tree pointer>) if (tree not empty) if (tree->token is operand) print tree->token else print ( infix (tree->left) print tree->token infix (tree->right) print ) end if end if
K All < K All > K Binary Search Treesis a binary tree such that at each node the left subtree contains key values less than the value at the node and the right subtree contains values greater than the the value at the node
Find Smallest • Algorithm findSmallest (val p<pointer>) • This algorithm finds the smallest node in a binary search tree • Pre p is a pointer to a tree or subtree • Post returns a pointer to the node • if (p is null ) • return NULL • if p->left is null • return p • else • return findSmallest(p->left)
Insert in a Binary Search Tree • Algorithm insert (ref p <pointer>, val value <data>) • Recursive function that inserts the value in the tree if it is not there already • Pre p is the root of a tree or subtree • Post node inserted in tree • Return true if inserted, false if already there • if ( p is null) • p = new node • p-> nodeValue = value • treeSize++ • return true • else if (value < p->nodeValue) • return insert (p->left, value) • else if (value > p-> nodeValue) • return insert (p->right, value) • Else //already there • return false; • end if
Searching • Algorithm findNode (val p <pointer>, ref value <key>) • Search a binary search tree for a given value • Pre p is the root of a tree or subtree • Return return pointer to the node with the value or NULL • if ( p is null || value == p-> nodeValue) • return p • else if (value < p-> nodeValue) • return findNode (p->left, value) • else if (value > p-> nodeValue) • return findNode (p->right, value) • end if
Deleting • Algorithm erase (val p <pointer>, val value <key>) • Deletes the node with value in it • Pre p is the root of a tree or subtree • Post value deleted from the tree • Return true if deleted and false if not found • if ( p is null) • return false //not found • if (value < p-> nodeValue) • return erase (p->left, value) • else if (value > p-> nodeValue) • return erase (p->right, value) • else • //actually deleting the node • => Next slide
Deleting the node • 1. q = p • 2. if (p->left == NULL) //no left child • p = p->right • delete q • else if (p->right == NULL) //no right child • p = p->left • delete q • 4. else //two non-null children • n = findSmallest(p->right) • p-> nodeValue = n->nodeValue • erase(p->right, n->nodeValue) • end if • treeSize is reduced by 1
An Outline Print Algorithm print(val p <pointer>, val depth <integer>) Indents and prints depth and value If p is Not NULL //indent for i=0; i<depth*3; i++ cout << “ “ cout << depth <<“.” << p->data print(p->left, depth+1) print(p->right, depth+1)