Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called.

Similar presentations


Presentation on theme: "Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called."— Presentation transcript:

1 Trees

2 2 Root leaf

3 CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called the root. n The remaining nodes are partitioned into n>=0 disjoint sets T 1,..., T n, where each of these sets is a tree. n We call T 1,..., T n the subtrees of the root.

4 4 Level and Depth Level 1 2 3 4 node (13) degree of a node leaf (terminal) nonterminal parent children sibling degree of a tree (3) ancestor level of a node height of a tree (4) 3 2 13 2 0 0 1 00 0 0 0 1 22 2 33 3 3 3 3 4 4 4

5 CHAPTER 5 5 Terminology n The degree of a node is the number of subtrees of the node –The degree of A is 3; the degree of C is 1. n The node with degree 0 is a leaf or terminal node. n A node that has subtrees is the parent of the roots of the subtrees. n The roots of these subtrees are the children of the node. n Children of the same parent are siblings. n The ancestors of a node are all the nodes along the path from the root to the node.

6 6 Representation of Trees n List Representation –( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) ) –The root comes first, followed by a list of sub-trees datalink 1link 2...link n How many link fields are needed in such a representation?

7 CHAPTER 5 7 Left Child - Right Sibling ABCD E F G H I J K LM data left child right sibling

8 CHAPTER 5 8 Binary Trees n A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary trees called the left subtree and the right subtree. n Any tree can be transformed into binary tree. –by left child-right sibling representation n The left subtree and the right subtree are distinguished.

9 J IM H L A B C D E F G K *Figure 5.6: Left child-right child tree representation of a tree

10 CHAPTER 5 10 Abstract Data Type Binary_Tree structure Binary_Tree(abbreviated BinTree) is objects: a finite set of nodes either empty or consisting of a root node, left Binary_Tree, and right Binary_Tree. functions: for all bt, bt1, bt2  BinTree, item  element Bintree Create()::= creates an empty binary tree Boolean IsEmpty(bt)::= if (bt==empty binary tree) return TRUE else return FALSE

11 Class NodeBT{ NodeBT bTKi, bTKa; Object item; public NodeBT(NodeBt btki, Object it, NodeBt btka){ bTKi = btki; item = it; bTKa = btka; } Class BinaryTree{ NodeBt node; public create(){node = null;} // public BinaryTree(){node = null;} public isEmpty(BinaryTree bt){ return bt== null;} public Object inOrder(BinaryTree bt){} public Object preOrder(BinaryTree bt){} public Object posOrder(BinaryTree bt){} } 11

12 B public Object inOrder(BinaryTree bt){ inOrder(btKi); printData() intOrder(btKa); } public Object preOrder(BinaryTree bt){ printData() preOrder(btKi) preOrder(btKa) } public Object posOrder(BinaryTree bt){ posOrder(btKi) posOrder(btKa) printData() } preOrder(bt) print- A preOrder(btKi) print-B preOder(btKi) print-D preOder(btKa) print-E preOrder(btKa) print-C 12

13 CHAPTER 5 13 BinTree MakeBT(bt1, item, bt2)::= return a binary tree whose left subtree is bt1, whose right subtree is bt2, and whose root node contains the data item Bintree Lchild(bt)::= if (IsEmpty(bt)) return error else return the left subtree of bt element Data(bt)::= if (IsEmpty(bt)) return error else return the data in the root node of bt Bintree Rchild(bt)::= if (IsEmpty(bt)) return error else return the right subtree of bt

14 CHAPTER 5 14 Samples of Trees ABABABCGEIDHF Complete Binary Tree Skewed Binary Tree ECD 1 2 3 4 5

15 CHAPTER 5 15 Maximum Number of Nodes in BT n The maximum number of nodes on level i of a binary tree is 2 i-1, i>=1. n The maximum nubmer of nodes in a binary tree of depth k is 2 k -1, k>=1. i-1 Prove by induction.

16 CHAPTER 5 16 Relations between Number of Leaf Nodes and Nodes of Degree 2 For any nonempty binary tree, T, if n 0 is the number of leaf nodes and n 2 the number of nodes of degree 2, then n 0 =n 2 +1 proof: Let n and B denote the total number of nodes & branches in T. Let n 0, n 1, n 2 represent the nodes with no children, single child, and two children respectively. n= n 0 +n 1 +n 2, B+1=n, B=n 1 +2n 2 ==> n 1 +2n 2 +1= n, n 1 +2n 2 +1= n 0 +n 1 +n 2 ==> n 0 =n 2 +1

17 CHAPTER 5 17 Full BT VS Complete BT n A full binary tree of depth k is a binary tree of depth k having 2 -1 nodes, k>=0. n A binary tree with n nodes and depth k is complete iff its nodes correspond to the nodes numbered from 1 to n in the full binary tree of depth k. k A B C G E I D H F A B C G E K D J F I H O N M L Full binary tree of depth 4 Complete binary tree

18 CHAPTER 5 18 Binary Tree Sequential Representations n If a complete binary tree with n nodes (depth = log n + 1) is represented sequentially, then for any node with index i, 1<=i<=n, we have: –parent(i) is at i/2 if i!=1. If i=1, i is at the root and has no parent. –left_child(i) ia at 2i if 2i n, then i has no left child. –right_child(i) ia at 2i+1 if 2i +1 n, then i has no right child.

19 CHAPTER 5 19 Sequential Representation A B -- C -- D --. E [1] [2] [3] [4] [5] [6] [7] [8] [9]. [16] [1] [2] [3] [4] [5] [6] [7] [8] [9] ABCDEFGHIABCDEFGHI ABECDABCGEIDHF (1) waste space (2) insertion/deletion problem

20 20 List Representation

21 BST 21

22 Binary Search Trees22 Binary Search Trees n A binary search tree is a binary tree storing keys (or key-value entries) at its internal nodes and satisfying the following property: –Let u, v, and w be three nodes such that u is in the left subtree of v and w is in the right subtree of v. We have key(u)  key(v)  key(w) n External nodes do not store items n An inorder traversal of a binary search trees visits the keys in increasing order 6 92 418

23 Example Binary Searches n Find ( root, 2 ) 5 10 30 22545 5 10 30 2 25 45 10 > 2, left 5 > 2, left 2 = 2, found 5 > 2, left 2 = 2, found root

24 Example Binary Searches n Find (root, 25 ) 5 10 30 22545 5 10 30 2 25 45 10 < 25, right 30 > 25, left 25 = 25, found 5 < 25, right 45 > 25, left 30 > 25, left 10 < 25, right 25 = 25, found

25 Binary Search Tree Construction n How to build & maintain binary trees? –Insertion –Deletion n Maintain key property (invariant) –Smaller values in left subtree –Larger values in right subtree

26 Binary Search Tree – Insertion n Algorithm 1.Perform search for value X 2.Search will end at node Y (if X not in tree) 3.If X < Y, insert new leaf X as new left subtree for Y 4.If X > Y, insert new leaf X as new right subtree for Y n Observations –O( log(n) ) operation for balanced tree –Insertions may unbalance tree

27 Example Insertion n Insert ( 20 ) 5 10 30 22545 10 < 20, right 30 > 20, left 25 > 20, left Insert 20 on left 20

28 Iterative Search of Binary Tree Node Find( Node n, int key) { while (n != NULL) { if (n.data == key) // Found it return n; if (n.data > key) // In left subtree n = n.left; else // In right subtree n = n.right; } return null; } Node n = Find( root, 5);

29 Recursive Search of Binary Tree Node Find( Node n, int key) { if (n == NULL) // Not found return( n ); else if (n.data == key) // Found it return( n ); else if (n.data > key) // In left subtree return Find( n.eft, key ); else // In right subtree return Find( n.right, key ); } Node n = Find( root, 5);

30 Binary Search Tree – Deletion n Algorithm 1.Perform search for value X 2.If X is a leaf, delete X 3.Else // must delete internal node a) Replace with largest value Y on left subtree OR smallest value Z on right subtree b) Delete replacement value (Y or Z) from subtree Observation –O( log(n) ) operation for balanced tree –Deletions may unbalance tree

31 Example Deletion (Leaf) n Delete ( 25 ) 5 10 30 22545 10 < 25, right 30 > 25, left 25 = 25, delete 5 10 30 245

32 Example Deletion (Internal Node) n Delete ( 10 ) 5 10 30 22545 5 5 30 22545 2 5 30 22545 Replacing 10 with largest value in left subtree Replacing 5 with largest value in left subtree Deleting leaf

33 Example Deletion (Internal Node) n Delete ( 10 ) 5 10 30 22545 5 25 30 22545 5 25 30 245 Replacing 10 with smallest value in right subtree Deleting leafResulting tree

34 QUESTION?? 34


Download ppt "Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called."

Similar presentations


Ads by Google