trees overview
play

Trees Overview We have discussed linear data structures Linear Vs - PowerPoint PPT Presentation

Trees Overview We have discussed linear data structures Linear Vs non-linear data structures arrays, linked lists, stacks, queues Types of binary trees Some other data structures we will consider Binary tree traversals


  1. Trees Overview � We have discussed linear data structures � Linear Vs non-linear data structures � arrays, linked lists, stacks, queues � Types of binary trees � Some other data structures we will consider � Binary tree traversals � trees, tables, graphs, hash-tables � Representations of a binary tree � Trees are extremely useful and suitable for a wide range of applications � Binary tree ADT � sorting, searching, expression evaluation, data set � Binary search tree representation � especially well suited to recursive algorithm implementation EECS 268 Programming II 1 EECS 268 Programming II 2 Terminology Terminology � A Tree T is a set of n >= 0 elements: � if n == 0, T is an empty tree � if n > 0 then there exists some element called r � T called the root of T such that T - {r} can be partitioned into zero or more disjoint sets T 1 ,T 2 , ... where each subset forms a tree � Trees are composed of nodes and edges � Trees are hierarchical � parent-child relationship between two nodes � ancestor-descendant relationships among nodes � Subtree of a tree: Any node and its descendants Figure 10-1 A general tree Figure 10-2 A subtree of the tree in Figure 10-1 EECS 268 Programming II 3 EECS 268 Programming II 4

  2. Terminology Terminology � Parent of node n � Leaf � The node directly above node n in the tree � A node with no children � Child of node n � Siblings � A node directly below node n in the tree � Nodes with a common parent � Root � Ancestor of node n � The only node in the tree with no parent � A node on the path from the root to n � Subtree of node n � Descendant of node n � A tree that consists of a child (if any) of node n � A node on a path from n to a leaf ��������������������������� EECS 268 Programming II 5 EECS 268 Programming II 6 A Binary Tree A General Tree & A Binary Tree � A binary tree is a set T of nodes such that � T is empty, or � T is partitioned into three disjoint subsets: � a single node r, the root � two possibly empty sets that are binary trees, called the left subtree of r and the right subtree of r � Binary trees are ordered A A � These trees are not equal L R B B EECS 268 Programming II 7 EECS 268 Programming II 8

  3. � ����������������������������� � ����������������������������� More Binary Trees A Binary Search Tree � A binary search tree is a binary tree that has the following properties for each node n ��������� subtree TL ���������� subtree TR � both TL and TR are binary search trees Figure 10-4 Binary trees that represent algebraic expressions 9 EECS 268 Programming II 10 The Height of Trees The Height of Trees � Height of a tree � Level of a node n in a tree T � Number of nodes along the longest path from the � If n is the root of T, it is at level 1 root to a leaf � If n is not the root of T, its level is 1 greater than the level of its parent Height 3 Height 5 Height 7 � Height of a tree T defined in terms of the levels of its nodes Figure 10-6 � If T is empty, its height is 0 Binary trees with � If T is not empty, its height is equal to the the same nodes but maximum level of its nodes different heights 11 EECS 268 Programming II 12

  4. �������������������������� Full Binary Trees The Height of Trees � A binary tree of height h � A recursive definition of height is full if � If T is empty, its height is 0 � Nodes at levels < h have two children each � If T is not empty, � Recursive definition � height(T) = 1 + max{height(TL), height(TR)} � If T is empty, T is a full r binary tree of height 0 � If T is not empty and has / \ height h > 0, T is a full TL TR subtrees are both full binary trees of height h � 1 Figure 10-7 A full binary tree of height 3 14 EECS 268 Programming II 13 Complete Binary Trees Complete Binary Trees � A binary tree of height h is complete if � Another definition: � It is full to level h � 1, and � A binary tree of height h is complete if � Level h is filled from left to right � All nodes at levels <= h � 2 have two children each, and � When a node at level h � 1 has children, all nodes to its left at the same level have two children each, and � When a node at level h � 1 has one child, it is a left child EECS 268 Programming II 15 EECS 268 Programming II 16

  5. Balanced Binary Trees Traversals of a Binary Tree � A traversal visits each node in a tree � A binary tree is balanced if the heights of any � to do something with or to the node during a visit �������������������������������������������� � for example, display the data in the node � Complete binary trees are balanced � General form of a recursive traversal algorithm � Full binary trees are complete and balanced traverse (in binTree:BinaryTree) if (binTree is not empty) ������������������������������������������� ����������������������������������������� } EECS 268 Programming II 17 EECS 268 Programming II 18 Traversals of a Binary Tree Traversals of a Binary Tree � Preorder traversal � Visit root before visiting its subtrees � i. e. Before the recursive calls � Inorder traversal � Visit root between visiting its subtrees � i. e. Between the recursive calls � Postorder traversal � Visit root after visiting its subtrees Figure 10-10 � i. e. After the recursive calls Traversals of a binary tree: (a) preorder; (b) inorder; (c) postorder EECS 268 Programming II 19 EECS 268 Programming II 20

  6. The ADT Binary Tree Traversals of a Binary Tree +createBinaryTree() +createBinaryTree(in rootItem: TreeItemType) +createBinaryTree(in rootItem: TreeItemType, � A traversal operation can call a function to inout leftTree: BinaryTree, inout rightTree: BinaryTree) perform a task on each item in the tree +destroyBinaryTree() +isEmpty(): boolean {query} � �������������������������������������������� +getRootData(): TreeItemType throw TreeException +setRootData(in newItem: TreeItemType) throw TreeException � the client defines and passes this function as an +attachLeft(in newItem: TreeItemType) throw TreeException argument to the traversal operation +attachRight(in newItem: TreeItemType) throw TreeException +attachLeftSubtree(inout leftTree: BinaryTree) throw TreeException � Tree traversal orders correspond to algebraic +attachRightSubtree(inout rightTree: BinaryTree) throw TreeException +detachLeftSubtree(out leftTree: BinaryTree) throw TreeException expressions +detachRightSubtree(out rightTree: BinaryTree) throw TreeException +getLeftSubtree(): BinaryTree � infix, prefix, and postfix +getRightSubtree(): BinaryTree +preorderTraverse(in visit:FunctionType) +inorderTraverse(in visit:FunctionType) +postorderTraverse(in visit:FunctionType) EECS 268 Programming II 21 EECS 268 Programming II 22 Possible Representations of a The ADT Binary Tree Binary Tree � An array-based representation � Building the ADT binary tree in Fig. 10-6b � Uses an array of tree nodes ���������������������� � Requires the creation of a free list that keeps track ��������������������� of available nodes ���������������������� tree2.attachLeftSubtree(tree1) � only suitable for complete binary trees ���������������������� � A pointer-based representation tree3.attachLeftSubtree(tree2) ��������������������� � Nodes have two pointers that link the nodes in the ���������������������� tree ������������������������������������������ 23 EECS 268 Programming II 24

Recommend


More recommend