Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees Chapter 10. CS 308 2Chapter 10 -- Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one.

Similar presentations


Presentation on theme: "Trees Chapter 10. CS 308 2Chapter 10 -- Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one."— Presentation transcript:

1 Trees Chapter 10

2 CS 308 2Chapter 10 -- 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.

3 CS 308 3Chapter 10 -- Trees Terminology Def: Def: vertex, edge vertex, edge parent, child, sibling parent, child, sibling root, leaf root, leaf ancestor, descendant ancestor, descendant

4 CS 308 4Chapter 10 -- Trees Subtree: any node and it’s descendants Subtree: any node and it’s descendants Tree: Tree: Subtree Subtree

5 CS 308 5Chapter 10 -- 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

6 CS 308 6Chapter 10 -- 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.

7 CS 308 7Chapter 10 -- 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: n’s value is greater than all values in its left subtree T L n’s value is greater than all values in its left subtree T L n’s value is less than all values in its right subtree T R n’s 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

8 CS 308 8Chapter 10 -- Trees This figure is an example of a binary search tree This figure is an example of a binary search tree

9 CS 308 9Chapter 10 -- 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:

10 CS 308 10Chapter 10 -- 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 ) )

11 CS 308 11Chapter 10 -- 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

12 CS 308 12Chapter 10 -- 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.

13 CS 308 13Chapter 10 -- 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.

14 CS 308 14Chapter 10 -- 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 tree’s nodes in one of several different orders. Traversal of a binary tree, however, visits the tree’s 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

15 CS 308 15Chapter 10 -- Trees In summary, the ADT binary tree has the following UML diagram In summary, the ADT binary tree has the following UML diagram

16 CS 308 16Chapter 10 -- 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.

17 CS 308 17Chapter 10 -- Trees Examples of preorder, inorder, and postorder traversals of the same tree. Examples of preorder, inorder, and postorder traversals of the same tree.

18 CS 308 18Chapter 10 -- 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) } } }

19 CS 308 19Chapter 10 -- 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

20 CS 308 20Chapter 10 -- 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; }; };

21 CS 308 21Chapter 10 -- 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.

22 CS 308 22Chapter 10 -- 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]

23 CS 308 23Chapter 10 -- Trees A Complete binary tree, and its array-based implementation: A Complete binary tree, and its array-based implementation:

24 CS 308 24Chapter 10 -- 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; }; };

25 CS 308 25Chapter 10 -- Trees Here is an illustration of this representation: Here is an illustration of this representation:

26 CS 308 26Chapter 10 -- 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++

27 CS 308 27Chapter 10 -- 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.

28 CS 308 28Chapter 10 -- Trees The UML diagram for a Binary Search Tree is given below: The UML diagram for a Binary Search Tree is given below:

29 CS 308 29Chapter 10 -- 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 == root’s item) else if(search_key == root’s item) the desired record is found the desired record is found else if (search_key < root’s item) else if (search_key < root’s item) search(left subtree) search(left subtree) else else serach (right subtree) serach (right subtree)

30 CS 308 30Chapter 10 -- Trees Insertion Insertion

31 CS 308 31Chapter 10 -- Trees

32 CS 308 32Chapter 10 -- Trees

33 CS 308 33Chapter 10 -- Trees

34 CS 308 34Chapter 10 -- Trees

35 CS 308 35Chapter 10 -- Trees

36 CS 308 36Chapter 10 -- Trees

37 CS 308 37Chapter 10 -- Trees

38 CS 308 38Chapter 10 -- Trees

39 CS 308 39Chapter 10 -- Trees

40 CS 308 40Chapter 10 -- Trees

41 CS 308 41Chapter 10 -- Trees

42 CS 308 42Chapter 10 -- Trees

43 CS 308 43Chapter 10 -- Trees

44 CS 308 44Chapter 10 -- Trees The Efficiency of Binary Search Tree Operations The Efficiency of Binary Search Tree Operations

45 CS 308 45Chapter 10 -- Trees

46 CS 308 46Chapter 10 -- Trees

47 CS 308 47Chapter 10 -- Trees

48 CS 308 48Chapter 10 -- Trees

49 CS 308 49Chapter 10 -- Trees

50 CS 308 50Chapter 10 -- Trees

51 CS 308 51Chapter 10 -- Trees

52 CS 308 52Chapter 10 -- Trees

53 CS 308 53Chapter 10 -- Trees

54 CS 308 54Chapter 10 -- Trees

55 CS 308 55Chapter 10 -- Trees

56 CS 308 56Chapter 10 -- Trees

57 CS 308 57Chapter 10 -- Trees

58 CS 308 58Chapter 10 -- Trees

59 CS 308 59Chapter 10 -- Trees

60 CS 308 60Chapter 10 -- Trees

61 CS 308 61Chapter 10 -- Trees

62 CS 308 62Chapter 10 -- Trees

63 CS 308 63Chapter 10 -- Trees

64 CS 308 64Chapter 10 -- Trees


Download ppt "Trees Chapter 10. CS 308 2Chapter 10 -- Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one."

Similar presentations


Ads by Google