Download presentation

Presentation is loading. Please wait.

1
1 Trees

2
2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees

3
3 Tree Structures Arrays, vectors, lists are linear structures, one element follows another. Trees are hierarchical structure.

4
4 Tree Structures

5
5 Tree Terminology Tree is a set of nodes. The set may be empty. If not empty, then there is a distinguished node r, called root, all other nodes originating from it, and zero or more non-empty subtrees T 1,T 2, …,T k, each of whose roots are connected by a directed edge from r. (inductive definition) T1 T2 Tk … r

6
6 Tree Terminology root Nodes in subtrees are called successors or descendents of the root node An immediate successor is called a child A leaf node is a node without any children while an interior node has at least one child. The link between node describes the parent-child relation. The link between parent and child is called edge A path between a parent P and any node N in its subtrees is a sequence of nodes P= X 0,X 1, …,X k =N, where k is the length of the path. Each node X i in the path is the parent of X i+1, (0≤i<k) Size of tree is the number of nodes in the tree

7
7 Tree Node Level and Path Length The level of a node N in a tree is the length of the path from root to N. Root is at level 0. The depth of a tree is the length of the longest path from root to any node.

8
8 Binary Tree In a binary tree, no node has more than two children, and the children are distinguished as left and right. A binary tree T is a finite set of nodes with one the following properties: 1.T is a tree if the set of nodes is empty. 2.The set consists of a root R and exactly two distinct binary trees. The left subtree T L and the right subtree T R, either or both subtree may be empty.

9
9 Examples of Binary Trees Size? depth?

10
10 Full Binary Tree full binary tree: a binary tree is which each node was exactly 2 or 0 children

11
11 Density of a Binary Tree At any level n, a binary tree may contain from 1 to 2 n nodes. The number of nodes per level contributes to the density of the tree. Degenerate tree: there is a single leaf node and each interior node has only one child. An n-node degenerate tree has depth n-1 Equivalent to a linked list A complete binary tree of depth n is a tree in which each level from 0 to n-1 has all possible nodes and all leaf nodes at level n occupy the leftmost positions in the tree.

12
12 Complete Binary Tree complete binary tree: a binary tree in which every level, except possibly the deepest is completely filled. At depth n, the height of the tree, all nodes are as far left as possible

13
13 Complete or noncomplete?

14
14 Complete or noncomplete?

15
15 Complete or noncomplete? occup y

16
16 Complete or noncomplete?

17
17 Perfect Binary Tree perfect binary tree: a binary tree with all leaf nodes at the same depth. All internal nodes have exactly two children. a perfect binary tree has the maximum number of nodes for a given height a perfect binary tree has 2 (n+1) - 1 nodes where n is the height of a tree –height = 0 -> 1 node –height = 1 -> 3 nodes –height = 2 -> 7 nodes –height = 3 -> 15 nodes

18
Binary Search Trees Key property –Value at node Smaller values in left subtree Larger values in right subtree –Example X > Y X < Z Y X Z

19
Binary Search Trees Examples Binary search trees Not a binary search tree 5 10 30 22545 5 10 45 22530 5 10 30 2 25 45

20
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);

21
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->left, key ); else // In right subtree return Find( n->right, key ); } Node * n = Find( root, 5);

22
Example Binary Searches 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

23
Example Binary Searches 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

24
Types of Binary Trees Degenerate – only one child Complete – always two children Balanced – “mostly” two children –more formal definitions exist, above are intuitive ideas Degenerate binary tree Balanced binary tree Complete binary tree

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

26
Binary Search Tree – Insertion 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 Observations –O( log(n) ) operation for balanced tree –Insertions may unbalance tree

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

28
Binary Search Tree – Deletion 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

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

30
Example Deletion (Internal Node) 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

31
Example Deletion (Internal Node) 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

Similar presentations

© 2021 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google