Presentation is loading. Please wait.

Presentation is loading. Please wait.

Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.

Similar presentations


Presentation on theme: "Main Index Contents 11 Main Index Contents Week 6 – Binary Trees."— Presentation transcript:

1 Main Index Contents 11 Main Index Contents Week 6 – Binary Trees

2 Sequence and Associative Containers Sequence containers access data by position 1.Array (index) 2.Vector (index) 3.List (iterator) Associate containers can access data by value 1.Set and Map / Binary search tree Arrays Vectors Linked lists Trees

3 Tree in Nature

4 Tree in our life Need to turn it upside down

5 Tree in Computer Science Similar to tree in nature Root Leaves

6 Main Index Contents 66 Main Index Contents Tree Structures Tree Structures Root Parent Child Edge Leaf Interior node Subtree Level Depth = max level

7 Terminologies used in Trees - Wiki

8 Main Index Contents 88 Main Index Contents Tree Node Level and Path Length What is the Depth?

9 Main Index Contents 9 Binary Tree Definition A binary tree T is a finite set of nodes with one of the following properties: – (a) T is a tree if the set of nodes is empty. (An empty tree is a tree, size=0.) – (b) The set consists of a root, R, and exactly two distinct binary trees, the left subtree T L and the right subtree T R. The nodes in T consist of node R and all the nodes in T L and T R. Any node in a binary tree has at most two children

10 Main Index Contents 10 Main Index Contents Selected Samples of Binary Trees Does tree B have left subtree T L ?

11 Density of a Binary Tree Intuitively, density is a measure of the size of a tree (number of nodes) relative to the depth of the tree. Trees with a higher density are important as data structures, because they can “pack” more nodes near the root. Access to the nodes is along relatively short paths from the root.

12 Complete binary tree

13 Degenerate tree A degenerate (or pathological) tree is where each parent node has only one associated child node. This means that performance-wise, the tree will behave like a linked list data structure.linked list

14 Evaluating Tree Density Complete binary trees are an ideal storage structure, because of their ability to pack a large number of nodes near the root Assume we want to store n elements in a complete binary tree. We would like to know the depth d of such a tree.

15 Depth d --- Size n in complete binary tree … ………………… … … level d has 2 d nodes 2 0 = 1 nodes 2 1 = 2 nodes Geometric series

16

17 Main Index Contents 17 Main Index Contents Binary Tree Nodes

18 Node class template { public: T nodeValue; tnode *left, *right; tnode() {} tnode(const T& item, tnode *lptr=NULL, tnode *rptr=NULL):nodeValue(item), left(lptr), right(rptr) {} };

19 Node structure

20 Building a Binary Tree A binary tree consists of a collection of dynamically allocated tnode objects whose pointer values specify links to their children.

21 Recursion Solution to a problem depends on solutions to smaller instances of the same problem. As a tree is a self-referential (recursively defined) data structure, traversal can naturally be described by recursion.recursion Recursive function: a function that calls itself. 21

22 BINARY TREE SCAN ALGORITHMS How to traverse the tree so that each node is visited exactly once? 1. Depth-first Pre-order In-order Post-order 2. Breadth-first (level-order)

23 Depth-first Defined as operations recursively at each node. The actions include:  visiting the node and performing some task (N),  making a recursive descent to the left subtree (L),  making a recursive descent to the right subtree (R). The different scanning strategies depend on the order in which we perform the tasks.

24 In-order Scan The prefix “in” comes from the fact that the visit occurs between the descents into the two subtrees. In this example, we use the order LNR. 1. Traverse the left subtree (“go left”). 2. Visit the node. 3. Traverse the right subtree (“go right”). L N R Recursively!

25 In-order example L N R L N R   Recursively! In-order scan: B, D, A, E, C  

26 In-order output

27 Post-order scan 1. Traverse the left subtree (“go left”). 2. Traverse the right subtree (“go right”). 3. Visit the node D, B, E, C, A

28 Post-order output

29 Pre-order scan 1. Visit the node. 2. Traverse the left subtree (“go left”). 3. Traverse the right subtree (“go right”) A, B, D, C, E

30 Breadth-first (Level-Order) Scan

31 Example Question F B CE I H DA G Pre-order? In-order? Post-order? Level-order ?

32 WIKI

33 Computing the Leaf Count Pre-order scan

34 Computing the Depth of a Tree Post-order scan

35 Deleting Tree Nodes Post-order scan

36 Reading Chapter 4


Download ppt "Main Index Contents 11 Main Index Contents Week 6 – Binary Trees."

Similar presentations


Ads by Google