Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 302 Data Structures Trees.

Similar presentations


Presentation on theme: "CS 302 Data Structures Trees."— Presentation transcript:

1 CS 302 Data Structures Trees

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.

3 Terminology Def: vertex, edge parent, child, sibling root, leaf
ancestor, descendant

4 Subtree: any node and it’s descendants

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

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

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

8 This figure is an example of a binary search tree

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:

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

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

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.

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.

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

15 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.

16 Examples of preorder, inorder, and postorder traversals of the same tree.

17 Code: preorder(Node *ptr) { if(ptr!=NULL) cout << ptr->data;
preorder(ptr->left) preorder(ptr->right) }

18 Code: inorder(Node *ptr) { if(ptr!=NULL) inorder(ptr->left)
cout << ptr->data; inorder(ptr->right) }

19 Code: postorder(Node *ptr) { if(ptr!=NULL) postorder(ptr->left)
postorder(ptr->right) cout << ptr->data; }

20

21 In summary, the ADT binary tree has the following UML diagram

22 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

23 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; };

24 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.

25 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]

26 A Complete binary tree, and its array-based implementation:

27 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; };

28 Here is an illustration of this representation:

29 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.

30 The UML diagram for a Binary Search Tree is given below:

31 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)

32 End of Chapter


Download ppt "CS 302 Data Structures Trees."

Similar presentations


Ads by Google