Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees.

Similar presentations


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

1 Trees

2 Course Objectives At the end of the lesson students are expected to be able to: Understand the tree concept and terms related to tree. Identify characteristics of general tree, binary tree and binary search tree Identify basic operations of a tree such as tree traversals, insert node, delete node, searching. Understand and know how to apply and implement tree in problem solving and in programming.

3 1.0 Introduction to Tree

4 Introduction to tree - Definition
Tree is a non-linear data structure Data in a tree is stored in hierarchy Example of tree application: Represent algebraic formulas Store data in hierarchy form. Ex: organization chart Artificial intelligence – information is accessed based on certain decision which is stored in a tree. algebraic formulas : (1+2)*3 organization chart

5 Introduction to tree – Decision Tree
Binary tree associated with a decision process Internal nodes: questions with yes/no answer External nodes: decisions Example: dining decision Introduction to tree - Definition

6 Trees A tree is a collection of nodes and edges
The collection can be empty (recursive definition) If not empty, a tree consists of a distinguished node r (the root), and zero or more nonempty subtrees T1, T2, ...., Tk, each of whose roots are connected by a directed edge from r

7 2.0 Tree Terminology

8 Tree terminology Any two vertices must have one and only one path between them else its not a tree Trees are hierarchical Parent-child relationship between two nodes Ancestor-descendant relationships among nodes

9 Tree terminology General tree
A general tree T is a set of one or more nodes such that T is partitioned into disjoint subsets: A single node r, the root Sets that are general trees, called subtrees of r Subtree of a tree: Any node and its descendants

10 Terminology A general tree A subtree of the tree in general tree

11 Some Terminologies Root The only node in the tree with no parent
A tree has only one root Root : A Child and parent Every node except the root has one parent  Parent of node n The node directly above node n in the tree A is Parent to B,C,D,E,F,G A node can have an arbitrary number of children Child of node n A node directly below node n in the tree B,C,D,E,F,G Are children of A

12 Some Terminologies Leaves Nodes with no children B,C,H,I,P,Q,K,L,M,N
Sibling nodes with same parent P and Q are siblings Subtree of node n A tree that consists of a child (if any) of node n and the child’s descendants subtree of A Right subtree of E

13 Some Terminologies Ancestor of node n Descendant of node n
A node on the path from the root to n Ancesstor P : J,E,A Nod A is ancestor for all node in the tree Descendant of node n A node on a path from n to a leaf Descendant E: I,J,P,Q All nodes in the tree are descendant to the root.

14 Some Terminologies Path Length Depth of a node
sequence of nodes in which each node is adjacent o the next one. Example: Path from root to L: AFL, Path from root to P: AEJP Length number of edges on the path Length of Tree : 3 Depth of a node length of the unique path from the root to that node The depth of a tree is equal to the depth of the deepest leaf Depth of Tree : 3 Depth of K : 2

15 Some Terminologies Level of a node
the distance from the node to the root. Root is at level 0. Node B,C,D,E,F,G are at level 1. Node H,I,J,K,L,M,N are at level 2, while node P and Q are at level 3. Height of a tree the highest level of leaf in a tree plus 1. The highest level of the tree in the figure is 3. Thus, the height of the tree is 4.

16 Some Terminologies Height of a node
length of the longest path from that node to a leaf all leaves are at height 1 The height of a tree is equal to the height of the root or the number of nodes along the longest path from the root to a leaf Height

17 Trees - Example Level E R T L P M A S 1 2 3 root Child (of root)
Leaves or terminal nodes Child (of root) Depth of T: 2 Tree height: 4 1 2 3

18 3.0 Binary Tree

19 Binary Trees A tree in which no node can have more than two children
The depth of an “average” binary tree is considerably smaller than N, eventhough in the worst case, the depth can be as large as N – 1.

20 A Binary Tree A binary tree is a set T of nodes such that either
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

21 A General Tree & A Binary Tree
Figure (a) An organization chart; (b) a family tree

22 Collection of Binary Trees
Tree = NULL Empty Tree – a binary tree with no nodes. Tree One node – Level 0 A Tree Three nodes – Level 1 A B C Tree Skewed to left – Level 3 A B C Tree Skewed to right – Level 3 A B C

23 More Binary Trees Binary trees that represent algebraic expressions:

24 A Binary Search Tree A binary search tree
A binary tree that has the following properties for each node n n’s value is > all values in n’s left subtree TL n’s value is < all values in n’s right subtree TR Both TL and TR are binary search trees

25 A Binary Search Tree A binary search tree of names

26 Full Binary Trees A binary tree of height h is full if
Nodes at levels < h have two children each Recursive definition If T is empty, T is a full binary tree of height 0 If T is not empty and has height h > 0, T is a full binary tree if its root’s subtrees are both full binary trees of height h – 1 A full binary tree of height 3

27 Complete Binary Trees A binary tree of height h is complete if
It is full to level h–2, and Level h is filled from left to right

28 Height of a Full Binary Tree
At each level the number of the nodes is doubled. total number of nodes: = = 15

29 Nodes and Levels in a Full Binary Tree
Number of the nodes in a tree with M levels: …. 2M = 2 (M+1) - 1 = 2*2M - 1 Let N be the number of the nodes. N = 2*2M - 1, 2*2M = N + 1 2M = (N+1)/2 M = log( (N+1)/2 ) N nodes : log( (N+1)/2 ) = O(log(N)) levels M levels: 2 (M+1) = O(2M ) nodes

30 Binary Tree Example A complete binary tree but not a full binary tree
Not a full binary tree and not a complete binary tree Not a full binary tree and not a complete binary tree

31 Balanced Binary Trees A binary tree is balanced if the heights of any node’s two subtrees differ by no more than 1 Complete binary trees are balanced Full binary trees are complete and balanced Unbalanced tree : skewed to the right

32 Example: Expression Trees
Leaves are operands (constants or variables) The other nodes (internal nodes) contain operators Will not be a binary tree if some operators are not binary

33 Exercise Week 13_1 Is the tree complete?Yes/NO
Is the tree full? Yes/No Is the tree balances? Yes/No List the nodes that have D as an ancestor. List the children of J. Determine the height of the tree? Determine the level of node A. List the leave nodes. Identify the root node. Identify the siblings of B.

34 Tree traversal A traversal visits each node in a tree
You do something with the node during a visit For example, display the data in the node Used to print out the data in a tree in a certain order Type of Traversal Inorder traversal Preorder traversal Postordertraversal

35 Pre-order traversal Pre-order traversal prefix expression
Print the data at the root Recursively print out all data in the left subtree Recursively print out all data in the right subtree prefix expression ++a*bc*+*defg

36 Postorder traversal Postorder traversal Postfix expression
Recursively print out all data in the left subtree Recursively print out all data in the right subtree Print the data at the root Postfix expression abc*+de*f+g*+

37 Inorder traversal Inorder traversal infix expression
Recursively print out all data in the left subtree Print the data at the root Recursively print out all data in the right subtree infix expression a+b*c+d*e+f*g

38 Traversals of a Binary Tree

39 Exercise Week 13_2 Give the inorder, preorder and postorder traversal of the binary search tree.

40 Summary Binary trees provide a hierarchical organization of data
The implementation of a binary tree is usually pointer-based If the binary tree is complete, an efficient array-based implementation is possible Traversing a tree to “visit”—that is, do something to or with—each node is useful You pass a client-defined “visit” function to the traversal operation to customize its effect on the items in the tree

41 Summary The binary search tree allows you to use a binary search- like algorithm to search for an item having a specified value Binary search trees come in many shapes The height of a binary search tree with n nodes can range from a minimum of log2(n + 1) to a maximum of n The shape of a binary search tree determines the efficiency of its operations

42 Summary An inorder traversal of a binary search tree visits the tree’s nodes in sorted search-key order The treesort algorithm efficiently sorts an array by using the binary search tree’s insertion and traversal operations


Download ppt "Trees."

Similar presentations


Ads by Google