Presentation is loading. Please wait.

Presentation is loading. Please wait.

Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.

Similar presentations


Presentation on theme: "Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling."— Presentation transcript:

1 Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling a few terms: parent, child, decendent, ancestor, sibling, subtree, path, degree,

2 Subtree a b d e f ij g h c k root A node and all of its descendents.

3 Paths in a Tree a c b e f d g j i h Path 1 Path 2 Path 1: { a, b, f, j } Path 2: { d, i } From a parent node to its child and other descendents. There exists a unique path from any node to any of its descendents.

4 Depth and Height 7 310 8 4 12 1 65 211 9 tree height = 4 depth 0 depth 1 depth 2 depth 3 depth 4 node height = 2 Height of a node: the length of a node to its deepest descendent.

5 Degree 7 310 8 4 12 1 65 211 9 The number of children of a node x is called the degree of x. degree = 3 degree = 1degree = 0

6 Binary Trees Each node has at most two children. r a) it is empty, or b) it consists of three disjoint subsets: 1) a root node 2) a left binary subtree 3) a right binary subtree A set of nodes T is a binary tree if a d b c f e left subtree right subtree Left child: the child node on the left. Right child: the child node on the right.

7 Representing Rooted Trees: Direct Way Every node has three references One reference to the object stored at that node One reference to the node’s parent one reference to the node’s children

8 Representing Rooted Trees: Child-sibling representation Every node has three references (direct way) One reference to the object stored at that node One reference to the node’s parent one reference to the node’s left most children One reference to the node’s sibling

9 Child-sibling representation

10 Tree Traversal A traversal is a manner of visiting each node in a tree once. There are several different traversals, each of which orders the nodes differently. A recursive way (preorder traversal) 1.visit the root of T 2.for each subtree T’ of the root, recursive traverse T’ The order in which you visit the subtrees in step 2 is arbitrary. The simplest way is to start at the root ʼ s firstChild and follow the nextSibling links. In binary trees, the order is always left to right. A typical application: listing file directory

11 Implementation What is the time complexity?

12 Tree Traversal Another recursive way (postorder traversal) 1.for each subtree T’ of the root, recursive traverse T’ 2.visit the root of T In a postorder traversal, you visit each node’s children (in the left- to-right order) before the node itself A typical application: sum the total disk space used root directory

13 Postorder tree Traversal

14 Tree Traversal Another recursive way (inorder traversal for a binary tree) 1.recursively traverse T’s left subtree 2.visit the root of T 3.recursively traverse T’s right subtree Yet another way (level order traversal) 1.visit the root of T 2.visit all depth-1 nodes from left to right 3.visit all depth-2 nodes, and so on how would you implement this?

15 Implementing Level Traversal Enqueue the root 1.Dequeue a node 2.Visit it 3.Enqueue its children (in order from left to right)

16 Using Tree Traversal 1 Computing the arity (or degree) of a tree, i.e., the maximum number of children of a node Arity/degree = 6

17 Computing arity

18 Using Tree Traversal 2 Computing the height of a tree Using recursive 1.If the root has no children, the height is 0 2.Otherwise, the height is 1+max(height(leftsubtree), height(rightsubtree)

19 Computing tree height

20 Expression Trees An expression tree is a binary tree that represents an arithmetic expression leaves are operands internal nodes are operators prefix infix postfix

21 From Postfix to Expression Tree The idea is to use a stack to store subtrees for subexpressions.

22

23 Evaluating an expression tree

24 Binary Trees A few APIs isLeaf() setLeft() setRight() setData()

25 Set A set is a collection of elements that contains no duplicates. Standard Operations: containment, union, and set differences

26 Binary Search Tree A binary search tree is often used to implement a set where the element type if comparable. Property: For any node X, every key (element) in the left subtree of X is less than X’s key, and every key in the right subtree of X is greater than X’s key A same set of elements may have different BSTs, and an inorder traversal of a binary search tree visits the nodes in sorted order.

27 Building a BST for a set

28 Contains(c) Checking if a given element is C contained by a set is easier when the set is represented by a BST

29 Contains(c)

30 Add(k) The worst-case time needed to search for a key or to add a key to a BST T is O(height(T)).

31 Building a BST for a set The tree built for a set of elements depends on the insertion order. The tree can be in two extreme cases: 1.Well-balanced tree (best case) 2.Very unbalanced tree (worst case) Starting from a root, adding elements in the set one by one

32 remove() Case 1: the node to be removed has no children Solution: Just delete the node

33 remove() Case 2: the node to be removed has only one child Solution: Just replace the deleted node with its child.

34 remove() Case 3: the node to be removed has two children Solution: replace the node with its successor

35 remove() Case 3: the node to be removed has two children The successor’s left child cannot exist, why?

36 successor() Case 1: The node has a right subtree. Go down to leftmost element in right subtree. Case 2: The node does not have a right subtree. If there actually is a successor, then it must be the root r of this subtree. To find r, go up the tree following parent pointers, stopping when the previous node in the path is a left child of the current node.

37 iterator() The tree iterator follow the nature order, starting at the minimum, or leftmost, element of the tree, proceeding all the way to the maximum. next() Go down the tree, following left child pointers until we find a node with no left child. This must be the minimum. hasNext() Implement using successor() remove() What is their time complexity()?


Download ppt "Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling."

Similar presentations


Ads by Google