Download presentation
Presentation is loading. Please wait.
Published byEleanore Peters Modified over 8 years ago
1
Chapter 7 Trees_ Part2 TREES
2
Depth and Height 2 Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. The depth of a node v can also be recursively defined as follows: If v is the root, then the depth of v is 0 Otherwise, the depth of v is one plus the depth of the parent of v.
3
Depth and Height con.. 3
4
4 The height of a node v in a tree T is also defined recursively: If v is an external node, then the height of v is 0 Otherwise, the height of v is one plus the maximum height of a child of v.
5
Depth and Height con.. 5
6
6
7
Tree Traversal Tree traversal is the process of visiting each node in the tree exactly one time. This definition does not specify the order in which the nodes are visited. There are three simple ways to traverse a tree. They’re called preorder, inorder, and postorder. The order most commonly used for binary search trees is inorder. 7
8
Breadth-first Traversal BFT is visiting each node starting from the highest(or lowest) level and moving down (or up) level by level, visiting nodes on each level from left to right(or from right to left). So, we will get 4 possibilities. This traverse an be done using queue: after a node is visited, its children, if any, are placed at the end of the queue, and the node at the beginning of the queue is visited. All nodes on level n must be visited before visiting any nodes on level n+1 is accomplished. 8
9
A top-down, left-to-right breadth first traversal of this tree is: 13, 10, 25, 2, 12, 20, 31, 29 Breadth-first Traversal con.. 9
10
Depth- First Traversal DFT proceeds as far as possible to the left(or right), then backs up until the first crossroad, goes one step to the right(or left), and again as far as possible to the left(or right). Repeat this process until all nodes are visited. There are some variations of the depth-first traversal. There are three tasks here: 1. V- visiting a node. 2. L- traversing the left subtree. 3. R- traversing the right subtree. 10
11
The number of different orders can be reduce to three traversals where the move is always from left to right and focus on the first column. 1. VLR – preorder tree traversal 2. LVR – inorder tree traversal 3. LRV – postorder tree traversal The real job is done by the system on the run-time stack (using double recursion) Remember that visiting a node means doing something to it. Depth- First Traversal con.. 11
12
12 Given a binary tree having a root, left subtree(LST), right sub tree(RST): 3 2 1 Preorder traversal NLR LR 3 1 2 LR 2 1 3 LR Inorder traversal LNR Postorder traversal LRN Standard Traversals Depth- First Traversal
13
In a preorder traversal of a tree T, the root of T is visited first and then the subtrees rooted at its children are traversed recursively. If the tree is ordered, then the subtrees are traversed according to the order of the children. Preorder Traversal 13
14
Postorder Traversal 14 Another important tree traversal algorithm is the postorder traversal. This algorithm can be viewed as the opposite of the preorder traversal, because it recursively traverses the subtrees rooted at the children of the root first, and then visits the root.
15
Preorder Traversal & Postorder Traversal Preorder Traversal 1- Visit the node. 2- Call itself to traverse the node's left subtree. 3- Call itself to traverse the node's right subtree. Postorder Traversal 1. Call itself to traverse the node’s left subtree. 2. Call itself to traverse the node’s right subtree. 3. Visit the node. 15
16
Traversing the tree using preorder would generate the expression *A+BC. This is called prefix notation. Note : parentheses are not required. Traversing the tree using postorder would generate the expression ABC+*. This is called postfix notation.. 16
17
Preorder (another example) A B E CDF DC BA F E ABCDEF 17
18
A B E CDF DC BA F E Postorder (another example) CDBFEA 18
19
Binary Tree 19 A binary tree is an ordered tree with the following properties: 1. Every node has at most two children. 2. Each child node is labeled as being either a left child or a right child. 3. A left child precedes a right child in the ordering of children of a node.
20
Binary Tree con.. 20 A Recursive Binary Tree Definition Incidentally, we can also define a binary tree in a recursive way such that a binary tree is either empty or consists of: A node r, called the root of T and storing an element A binary tree, called the left subtree of T A binary tree, called the right subtree of T.
21
The Binary Tree ADT 21 A binary tree is a specialization of a tree that supports three additional accessor methods: left(v): Return the left child of v; an error condition occurs if v has no left child. right(v): Return the right child of v; an error condition occurs if v has no right child. hasLeft(v): Test whether v has a left child. hasRight(v): Test whether v has a right child.
22
Properties of Binary Trees 22 In a binary tree, level 0 has at most one node (the root), level 1 has at most two nodes (the children of the root), level 2 has at most four nodes, and so on. In general, level d has at most 2 d nodes.
23
Properties of Binary Trees con.. 23
24
Binary tree can be implemented as : 1. A linked list. 2. An Array. Declare a node as a structure with an information field and two “pointers” fields. However, it may has problems when deleting and inserting nodes
26
References: Text book, chapter 7: Trees End Of Chapter 26
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.