Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees Chapter 10.

Similar presentations


Presentation on theme: "Trees Chapter 10."— Presentation transcript:

1 Trees Chapter 10

2 Preview: 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. In particular, this chapter discusses the specifications, implementations, and relative efficiency of the ADT binary tree and the ADT binary search tree. CS 308 Chapter Trees

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

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

5 The primary focus of this chapter will be on binary trees.
Formally a binary tree is a set T of nodes such that either: T is empty T is partitioned into three disjoint subsets: A single node r, the root Two possibly empty sets that are binary trees, called left and right subtrees CS 308 Chapter Trees

6 Here are some examples of how to use binary trees to store data in a hierarchical form.
CS 308 Chapter Trees

7 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: n’s value is greater than all values in its left subtree TL n’s value is less than all values in its right subtree TR Both TL and TR are binary search trees CS 308 Chapter Trees

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

9 The height of trees 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: CS 308 Chapter Trees

10 You can also define height recursively:
height (T) = 1 + max( height(TL), height(TR) ) CS 308 Chapter Trees

11 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. Here is a full binary tree of height 3 CS 308 Chapter Trees

12 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 308 Chapter Trees

13 The ADT Binary Tree 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. Other operations set or retrieve the data in the root of the tree and determine whether the tree is empty. CS 308 Chapter Trees

14 Traversal operations that visit every node in a binary tree are typical.
“Visiting” a node means “doing something with or to” the node. 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. The three standard orders are called preorder, inorder, and postorder CS 308 Chapter Trees

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

16 Traversals of a Binary 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. CS 308 Chapter Trees

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

18 Code: preorder(Node *ptr) { if(ptr!=NULL) cout << ptr->data;
preorder(ptr->left); preorder(ptr->right) } CS 308 Chapter Trees

19 Possible Representations of a Binary Tree.
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. In order to illustrate these three ways we will implement a binary tree of names each way CS 308 Chapter Trees

20 An Array-based representation:
const int MAX_NODES = 100; typedef string TreeIremType; class TreeNode{ private: TreeNode(); TreeNode(const TreeItemType & nodeItem, int left, int right); TreeItemType item; int leftChild; int rightChild; friend class BinaryTree; }; CS 308 Chapter Trees

21 We now know the root of the tree, as well a the head of a free list.
TreeNode[MAX_NODES] tree; int root; int free; We now know the root of the tree, as well a the head of a free list. CS 308 Chapter Trees

22 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 The left child (if it exists) is tree[2*i+1] 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] CS 308 Chapter Trees

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

24 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 typedef string TreeItemType; class TreeNode{ private: TreeNode(){ }; TreeNode(const TreeItemType& nodeItem, TreeNode *left = NULL, TreeNode *right = NULL): item(nodeItem), leftChildPtr(left), rightChildPtr(right) { } TreeItemType item; TreeNode * leftChildPtr; TreeNode * rightChildPtr; friend class BinaryTree; }; CS 308 Chapter Trees

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

26 A Pointer-Based Implementation of the ADT Binary Tree
This section gives the header and implementation of a binary tree in C++ CS 308 Chapter Trees

27 The ADT Binary Search Tree
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. CS 308 Chapter Trees

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

29 Algorithms for the ADT Binary Search Tree Operations
if the tree is empty the desired record is not found else if(search_key == root’s item) the desired record is found else if (search_key < root’s item) search(left subtree) else serach (right subtree) CS 308 Chapter Trees

30 Insertion CS 308 Chapter Trees

31 CS 308 Chapter Trees

32 CS 308 Chapter Trees

33 CS 308 Chapter Trees

34 CS 308 Chapter Trees

35 CS 308 Chapter Trees

36 CS 308 Chapter Trees

37 CS 308 Chapter Trees

38 CS 308 Chapter Trees

39 CS 308 Chapter Trees

40 CS 308 Chapter Trees

41 CS 308 Chapter Trees

42 CS 308 Chapter Trees

43 CS 308 Chapter Trees

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

45 CS 308 Chapter Trees

46 CS 308 Chapter Trees

47 CS 308 Chapter Trees

48 CS 308 Chapter Trees

49 CS 308 Chapter Trees

50 CS 308 Chapter Trees

51 CS 308 Chapter Trees

52 CS 308 Chapter Trees

53 CS 308 Chapter Trees

54 CS 308 Chapter Trees

55 CS 308 Chapter Trees

56 CS 308 Chapter Trees

57 CS 308 Chapter Trees

58 CS 308 Chapter Trees

59 CS 308 Chapter Trees

60 CS 308 Chapter Trees

61 CS 308 Chapter Trees

62 CS 308 Chapter Trees

63 CS 308 Chapter Trees

64 CS 308 Chapter Trees


Download ppt "Trees Chapter 10."

Similar presentations


Ads by Google