+ All Categories
Home > Documents > Trees Chapter 10. CS 308 2Chapter 10 -- Trees Preview: Preview: The data organizations presented in...

Trees Chapter 10. CS 308 2Chapter 10 -- Trees Preview: Preview: The data organizations presented in...

Date post: 08-Jan-2018
Category:
Upload: berniece-bradley
View: 220 times
Download: 0 times
Share this document with a friend
Description:
CS 308 3Chapter Trees Terminology Def: Def: vertex, edge vertex, edge parent, child, sibling parent, child, sibling root, leaf root, leaf ancestor, descendant ancestor, descendant

If you can't read please download the document

Transcript

Trees Chapter 10 CS 308 2Chapter Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one after another. The data organizations presented in previous chapters are linear, in that items are one after another. The ADTs in this chapter organize the data in a nonlinear, hierarchical form. The ADTs in this chapter organize the data in a nonlinear, hierarchical form. In particular, this chapter discusses the specifications, implementations, and relative efficiency of the ADT binary tree and the ADT binary search tree. In particular, this chapter discusses the specifications, implementations, and relative efficiency of the ADT binary tree and the ADT binary search tree. CS 308 3Chapter Trees Terminology Def: Def: vertex, edge vertex, edge parent, child, sibling parent, child, sibling root, leaf root, leaf ancestor, descendant ancestor, descendant CS 308 4Chapter Trees Subtree: any node and its descendants Subtree: any node and its descendants Tree: Tree: Subtree Subtree CS 308 5Chapter Trees The primary focus of this chapter will be on binary trees. The primary focus of this chapter will be on binary trees. Formally a binary tree is a set T of nodes such that either: Formally a binary tree is a set T of nodes such that either: T is empty T is empty T is partitioned into three disjoint subsets: T is partitioned into three disjoint subsets: A single node r, the root A single node r, the root Two possibly empty sets that are binary trees, called left and right subtrees Two possibly empty sets that are binary trees, called left and right subtrees CS 308 6Chapter Trees Here are some examples of how to use binary trees to store data in a hierarchical form. Here are some examples of how to use binary trees to store data in a hierarchical form. CS 308 7Chapter Trees A binary search tree is a binary tree that is in a sense sorted according to the values of its nodes. A binary search tree is a binary tree that is in a sense sorted according to the values of its nodes. For each node n, a binary search tree satisfies the following three properties: For each node n, a binary search tree satisfies the following three properties: ns value is greater than all values in its left subtree T L ns value is greater than all values in its left subtree T L ns value is less than all values in its right subtree T R ns value is less than all values in its right subtree T R Both T L and T R are binary search trees Both T L and T R are binary search trees CS 308 8Chapter Trees This figure is an example of a binary search tree This figure is an example of a binary search tree CS 308 9Chapter Trees The height of trees The height of trees The height of any tree is the number of nodes on the longest path from the root to a leaf The height of any tree is the number of nodes on the longest path from the root to a leaf For example: consider the following trees: For example: consider the following trees: CS Chapter Trees You can also define height recursively: You can also define height recursively: height (T) = 1 + max( height(T L ), height(T R ) ) height (T) = 1 + max( height(T L ), height(T R ) ) CS Chapter Trees Full, complete, and balanced binary trees Full, complete, and balanced binary trees In a full binary tree of height h, all nodes that are at a level less than h have two children each. In a full binary tree of height h, all nodes that are at a level less than h have two children each. Here is a full binary tree of height 3 Here is a full binary tree of height 3 CS Chapter Trees A complete binary tree of height h is a tree that is full down to level h-1, with level h filled from left to right. A complete binary tree of height h is a tree that is full down to level h-1, with level h filled from left to right. CS Chapter Trees The ADT Binary Tree As an abstract data type, the binary tree has operations that add and remove nodes and subtrees. As an abstract data type, the binary tree has operations that add and remove nodes and subtrees. By using these basic operations, you can build any binary tree. By using these basic operations, you can build any binary tree. Other operations set or retrieve the data in the root of the tree and determine whether the tree is empty. Other operations set or retrieve the data in the root of the tree and determine whether the tree is empty. CS Chapter Trees Traversal operations that visit every node in a binary tree are typical. Traversal operations that visit every node in a binary tree are typical. Visiting a node means doing something with or to the node. Visiting a node means doing something with or to the node. We saw traversals for linked lists in Chapter 4. We saw traversals for linked lists in Chapter 4. Traversal of a binary tree, however, visits the trees nodes in one of several different orders. Traversal of a binary tree, however, visits the trees nodes in one of several different orders. The three standard orders are called preorder, inorder, and postorder The three standard orders are called preorder, inorder, and postorder CS Chapter Trees In summary, the ADT binary tree has the following UML diagram In summary, the ADT binary tree has the following UML diagram CS Chapter Trees Traversals of a Binary Tree Traversals of a Binary Tree A traversal algorithm for a binary tree visits each node in the tree. A traversal algorithm for a binary tree visits each node in the tree. For purpose of discussion, assume that visiting a node simply means displaying the data portion of the node. For purpose of discussion, assume that visiting a node simply means displaying the data portion of the node. CS Chapter Trees Examples of preorder, inorder, and postorder traversals of the same tree. Examples of preorder, inorder, and postorder traversals of the same tree. CS Chapter Trees Code: Code: preorder(Node *ptr) preorder(Node *ptr) { if(ptr!=NULL) if(ptr!=NULL) { { cout data; cout data; preorder(ptr->left); preorder(ptr->left); preorder(ptr->right) preorder(ptr->right) } } } CS Chapter Trees Possible Representations of a Binary Tree. Possible Representations of a Binary Tree. You can implement a binary tree by using the constructs of C++ in one of three general ways. You can implement a binary tree by using the constructs of C++ in one of three general ways. Two of these approaches use arrays, but the typical implementation uses pointers. Two of these approaches use arrays, but the typical implementation uses pointers. In order to illustrate these three ways we will implement a binary tree of names each way In order to illustrate these three ways we will implement a binary tree of names each way CS Chapter Trees An Array-based representation: An Array-based representation: const int MAX_NODES = 100; const int MAX_NODES = 100; typedef string TreeIremType; typedef string TreeIremType; class TreeNode{ class TreeNode{ private: private: TreeNode(); TreeNode(); TreeNode(const TreeItemType & nodeItem, TreeNode(const TreeItemType & nodeItem, int left, int right); int left, int right); TreeItemType item; TreeItemType item; int leftChild; int leftChild; int rightChild; int rightChild; friend class BinaryTree; friend class BinaryTree; }; }; CS Chapter Trees TreeNode[MAX_NODES] tree; TreeNode[MAX_NODES] tree; int root; int root; int free; int free; We now know the root of the tree, as well a the head of a free list. We now know the root of the tree, as well a the head of a free list. CS Chapter Trees An array-based representation of a complete tree. An array-based representation of a complete tree. complete binary trees have special attributes: given a node i, you can easily locate both of its children and its parent complete binary trees have special attributes: given a node i, you can easily locate both of its children and its parent The left child (if it exists) is tree[2*i+1] The left child (if it exists) is tree[2*i+1] its right child (if it exists) is tree[2*i+2] its right child (if it exists) is tree[2*i+2] and its parent (if tree[i] is not the root) is tree[(i-1)/2] and its parent (if tree[i] is not the root) is tree[(i-1)/2] CS Chapter Trees A Complete binary tree, and its array-based implementation: A Complete binary tree, and its array-based implementation: CS Chapter Trees A pointer-based representation A pointer-based representation You can use C++ pointers to link the nodes in the tree. Thus you can represent a tree by using the following C++ statements You can use C++ pointers to link the nodes in the tree. Thus you can represent a tree by using the following C++ statements typedef string TreeItemType; typedef string TreeItemType; class TreeNode{ class TreeNode{ private: private: TreeNode(){ }; TreeNode(){ }; TreeNode(const TreeItemType& nodeItem, TreeNode(const TreeItemType& nodeItem, TreeNode *left = NULL, TreeNode *left = NULL, TreeNode *right = NULL): TreeNode *right = NULL): item(nodeItem), leftChildPtr(left), item(nodeItem), leftChildPtr(left), rightChildPtr(right) { } rightChildPtr(right) { } TreeItemType item; TreeItemType item; TreeNode * leftChildPtr; TreeNode * leftChildPtr; TreeNode * rightChildPtr; TreeNode * rightChildPtr; friend class BinaryTree; friend class BinaryTree; }; }; CS Chapter Trees Here is an illustration of this representation: Here is an illustration of this representation: CS Chapter Trees A Pointer-Based Implementation of the ADT Binary Tree A Pointer-Based Implementation of the ADT Binary Tree This section gives the header and implementation of a binary tree in C++ This section gives the header and implementation of a binary tree in C++ CS Chapter Trees The ADT Binary Search Tree Searching for a particular item is one operation for which a general binary tree is ill suited. Searching for a particular item is one operation for which a general binary tree is ill suited. The binary search tree is a binary tree that corrects this deficiency by organizing the data by value. The binary search tree is a binary tree that corrects this deficiency by organizing the data by value. CS Chapter Trees The UML diagram for a Binary Search Tree is given below: The UML diagram for a Binary Search Tree is given below: CS Chapter Trees Algorithms for the ADT Binary Search Tree Operations Algorithms for the ADT Binary Search Tree Operations Search: Search: if the tree is empty if the tree is empty the desired record is not found the desired record is not found else if(search_key == roots item) else if(search_key == roots item) the desired record is found the desired record is found else if (search_key < roots item) else if (search_key < roots item) search(left subtree) search(left subtree) else else serach (right subtree) serach (right subtree) CS Chapter Trees Insertion Insertion CS Chapter Trees CS Chapter Trees CS Chapter Trees CS Chapter Trees CS Chapter Trees CS Chapter Trees CS Chapter Trees CS Chapter Trees CS Chapter Trees CS Chapter Trees CS Chapter Trees CS Chapter Trees CS Chapter Trees CS Chapter Trees The Efficiency of Binary Search Tree Operations The Efficiency of Binary Search Tree Operations CS Chapter Trees CS Chapter Trees CS Chapter Trees CS Chapter Trees CS Chapter Trees CS Chapter Trees CS Chapter Trees CS Chapter Trees CS Chapter Trees CS Chapter Trees CS Chapter Trees CS Chapter Trees CS Chapter Trees CS Chapter Trees CS Chapter Trees CS Chapter Trees CS Chapter Trees CS Chapter Trees CS Chapter Trees CS Chapter Trees


Recommended