Download presentation
Presentation is loading. Please wait.
2
trees1 Binary Trees
3
trees2 Basic terminology nodesFinite set of nodes (may be empty -- 0 nodes), which contain data rootFirst node in tree is called the root
4
trees3 Basic terminology child nodes childrenEach node may be linked to 0, 1 or 2 child nodes (or children) parent leafA node with children is a parent; a node with no children is a leaf Nodes d and e are children of b Node c is parent of f and g
5
trees4 More botany & sociology ancestorThe root node has no parent; it is the ancestor of all other nodes descendantsAll nodes in the tree are descendants of the root node siblingsNodes with the same parent are siblings subtreeAny node with descendants (any parent node) is the root node of a subtree
6
trees5 But wait, there’s more! depthTree depth is the maximum number of steps through ancestor nodes from the deepest leaf back to root –tree with single node (root) has depth of 0 –empty tree has depth of -1 Depth of entire tree is 2; depth of b’s subtree is 1; depth of f’s subtree is 0
7
trees6 Tree shape terminology FullFull binary tree –every parent has 2 children, –every leaf has equal depth
8
trees7 Tree shape terminology CompleteComplete binary tree –every level is full except possibly the deepest level –if the deepest level isn’t full, leaf nodes are as far to the left as possible
9
trees8 Some examples Complete binary tree Full and complete Neither full nor complete Complete Neither Both
10
trees9 Array representation of a binary tree Complete binary tree is easily represented as an array (either static or dynamic) Root data is stored at index 0 Root’s children are stored at index 1 and 2 A B C A BC
11
trees10 Array representation of a binary tree In general: –root is tree[0] –for any node at tree[n], the parent node will be found at index (n-1)/2 –for any node at tree[n], the children of n (if any) will be found at tree[2n + 1] and tree[2n + 2]
12
trees11 Array representation of a binary tree Since these formulas apply to any complete tree represented as an array, can create a tree class with 2 private members: –an array that stores the data –a counter to keep track of the number of nodes in the tree Relationships between nodes can always be determined from the given formulas
13
trees12 Binary tree with array representation template class binTree {... private: Item* root; int used; int capacity; } Private member root represents container - root is a pointer to a dynamic array of Items Private member used represents number of values currently stored in tree; capacity is current maximum number of values that can be stored
14
trees13 Binary tree with array representation Choice of member functions for binary tree would depend on application Later on, we’ll look at the heap data structure, which is a binary tree based on an array representation
15
trees14 Array representation of a binary tree What if tree isn’t complete? –Can still use array, but problem is more complicated -- have to keep track of which children actually exist –One possibility would be to place a known dummy value in empty slots –Another is to add another private member to the class -- an array of bools paired to the data array -- index value is true if data exists, false if not
16
trees15 Linked representation of binary tree Again, as with linked list, entire tree can be represented with a single pointer -- in this case, a pointer to the root node Nodes are instances of class BTnode, described in the next several slides The class contains functions that operate on single nodes; we will also look at non- member functions that perform tree operations
17
trees16 template class BTnode { … private: Item data; BTnode *left; BTnode *right; }; Linked representation of binary tree Private member data holds the information in the node; members left and right are pointers to the node’s left and right children
18
trees17 Functions of BTnode class Constructor: creates node with specified data and left and right children Observer functions –data() returns value of data member –get_left() returns pointer to left child –get_right() returns pointer to right child –is_leaf() returns true if node is a leaf
19
trees18 Functions of BTnode class Modifiers –set_data(): changes data portion of node –set_left(): assigns new left child to node –set_right(): assigns new right child to node –modifier versions of get_data(), get_left() and get_right()
20
trees19 Non-member functions Observers (entire tree) –tree_size() returns number of nodes in tree –traversal functions (preorder(), inorder() and postorder()) provide different methods for moving through and processing all nodes of a tree or subtree –print() function uses traversal to print values of data portions of all nodes in a tree shape
21
trees20 Non-member functions Modifiers (entire tree): –tree_clear(): return memory occupied by an entire tree or subtree to system heap –tree_copy(): returns pointer to a node that is the root of an exact copy of the tree passed to the function as a parameter
22
trees21 BTnode class {public: BTnode (const Item& entry = Item(), BTnode* L = NULL, BTnode* R = NULL); const Item& data( ) const {return data;} const BTnode* get_left( )const {return left;} const BTnode* get_right( )const {return right;} bool is_leaf( ) const {return (left == NULL) && (right == NULL);} …
23
trees22 Default constructor template BTnode::BTnode(const Item& entry, BTnode* L,BTnode* R) { data = entry; left = L; right = R; }
24
trees23 BTnode class definition continued … Item& data( ) { return data; } Btnode* get_left( ) { return left; } Btnode* get_right( ) { return right; } void set_data(const Item& it) { data = it; } void set_left( BTnode* L) { left = L; } void set_right( BTnode* R) { right = R; } …
25
trees24 Non-member functions … void tree_clear ( Btnode& root); Btnode* tree_copy (const Btnode & root); …
26
trees25 Function tree_clear( ) Good example of a function in which a recursive solution is the simplest and most elegant method Base case: root is NULL (function does no work) Problem reduction: –clear left subtree (recursive call) –clear right subtree (recursive call) To finish: –return root’s memory to heap –set root to NULL
27
trees26 Function tree_clear( ) template void tree_clear(BTnode *& root) { if (root != NULL) { tree_clear(root->left); tree_clear(root->right); delete root; root = NULL; } }
28
trees27 Tree_clear in action Original tree:
29
trees28 Tree_clear in action Since root is not NULL, proceed recursively down left subtree until a NULL pointer is encountered
30
trees29 Tree_clear in action This node’s left pointer is NULL; so recursion stops here, and most recent call to function returns with call to tree_clear (root->right) Since this node’s right pointer is also NULL, this recursive call returns, and we proceed to next line: delete root; followed by: root = NULL;
31
trees30 Tree_clear in action The function then returns, and the same process is repeated at each subtree in root’s left subtree
32
trees31 Tree_clear in action The same process takes place in the right subtree
33
trees32 Tree_clear in action Finally, only root is left:
34
trees33 Function tree_copy( ) Copying a tree is also a recursive function Goal is to return a pointer to a tree that is an exact copy of the tree referenced by the source root pointer Base case: source root is NULL (return NULL) Recursive calls: call copy function on source root’s left & right subtrees Final step: use constructor to create new root pointer
35
trees34 template BTnode * tree_copy(const BTtnode * root) { BTnode *lp; BTnode *rp; if (root == NULL) return NULL; else { lp = tree_copy(root->left); rp = tree_copy(root->right); return new BTnode (root->data, lp, rp); }
36
trees35 Binary Trees - ends -
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.