Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures and Database Applications Binary Trees in C#

Similar presentations


Presentation on theme: "Data Structures and Database Applications Binary Trees in C#"— Presentation transcript:

1 Data Structures and Database Applications Binary Trees in C#

2 Binary Tree Overview Automatic resizing and homogeneous, non-linear list of nodes. For Binary Trees nodes are limited to one root node and from zero to two child nodes.

3 The Tree ADT A tree is an ADT with these properties: leaf nodes
root node internal nodes A tree is an ADT with these properties: one entry point, the root Each node is either a leaf or an internal node An internal node has 1 or more children, nodes that can be reached directly from that internal node. The internal node is said to be the parent of its child nodes leaf nodes

4 Properties of Trees Only access point is the root
All nodes, except the root, have one parent like the inheritance hierarchy Traditionally trees drawn upside down root leaves

5 Properties of Trees and Nodes
root siblings: two nodes that have the same parent edge: the link from one node to another path length: the number of edges that must be traversed to get from one node to another edge siblings path length from root to this node is 3

6 More Properties of Trees
depth: the path length from the root of the tree to this node height of a node: The maximum distance (path length) of any leaf from this node a leaf has a height of 0 the height of a tree is the height of the root of that tree descendants: any nodes that can be reached via 1 or more edges from this node ancestors: any nodes for which this node is a descendant

7 Tree Visualization A B D C F G H I J E K L M N O

8 Binary Trees There are many variations on trees but we will start with binary trees binary tree: each node has at most two children the possible children are usually referred to as the left child and the right child parent right child left child

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

10 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 Where would the next node go to maintain a complete tree?

11 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 the tree height = 0 -> 1 node height = 1 -> 3 nodes height = 2 -> 7 nodes height = 3 -> 15 nodes

12 Binary Tree Traversals
Many algorithms require all nodes of a binary tree be visited and the contents of each node processed or examined. There are 4 traditional types of traversals In Order traversal: process the left sub tree, process the root, process the right sub tree Pre Order traversal: process the root, then process all sub trees (left to right) Post Order traversal: process the left sub tree, process the right sub tree, then process the root Level Order traversal (aka Breadth First Search): starting from the root of a tree, process all nodes at the same depth from left to right, then proceed to the nodes at the next depth.

13 In Order Traversal Recursively does Left, then Middle, then Right B C

14 In Order Traversal Recursive Step 1
Start with the Left Node B A C

15 In Order Traversal Recursive Step 2
Next Go To The Middle Node B A C

16 In Order Traversal Recursive Step 3
Finish with the Right Node B A C Result is: A – B – C

17 In Order Traversal Recursively does Left, then Middle, then Right D E
B E A C Recursively does Left, then Middle, then Right

18 In Order Traversal Recursively does Left, then Middle, then Right D E
B E A C Recursively does Left, then Middle, then Right

19 In Order Traversal Recursively does Left, then Middle, then Right D E
B E A C Recursively does Left, then Middle, then Right

20 In Order Traversal Recursively does Left, then Middle, then Right D E
B E A C Recursively does Left, then Middle, then Right

21 In Order Traversal Recursively does Left, then Middle, then Right D E
B E A C Recursively does Left, then Middle, then Right

22 In Order Traversal D B E A C Result is: A – B – C – D – E

23 Pre Order Traversal Recursively does Middle, then Left, then Right B C

24 Pre Order Traversal Recursive Step 1
Start with the Middle Node B A C

25 Pre Order Traversal Recursive Step 2
Next Go To The Left Node B A C

26 Pre Order Traversal Recursive Step 3
Finish with the Right Node B A C Result is: B – A – C

27 Pre Order Traversal Recursively does Middle, then Left, then Right D E
B E A C Recursively does Middle, then Left, then Right

28 Pre Order Traversal Recursively does Middle, then Left, then Right D E
B E A C Recursively does Middle, then Left, then Right

29 Pre Order Traversal Recursively does Middle, then Left, then Right D E
B E A C Recursively does Middle, then Left, then Right

30 Pre Order Traversal Recursively does Middle, then Left, then Right D E
B E A C Recursively does Middle, then Left, then Right

31 Pre Order Traversal Recursively does Middle, then Left, then Right D E
B E A C Recursively does Middle, then Left, then Right

32 Pre Order Traversal D B E A C Result is: D – B – A – C – E

33 Post Order Traversal Recursively does Left, then Right, then Middle B

34 Post Order Traversal Recursive Step 1
Start with the Left Node B A C

35 Post Order Traversal Recursive Step 2
Next Go To The Right Node B A C

36 Post Order Traversal Recursive Step 3
Finish with the Middle Node B A C Result is: A – C – B

37 Post Order Traversal Recursively does Left, then Right, then Middle D
B E A C Recursively does Left, then Right, then Middle

38 Post Order Traversal Recursively does Left, then Right, then Middle D
B E A C Recursively does Left, then Right, then Middle

39 Post Order Traversal Recursively does Left, then Right, then Middle D
B E A C Recursively does Left, then Right, then Middle

40 Post Order Traversal Recursively does Left, then Right, then Middle D
B E A C Recursively does Left, then Right, then Middle

41 Post Order Traversal Recursively does Left, then Right, then Middle D
B E A C Recursively does Left, then Right, then Middle

42 Post Order Traversal D B E A C Result is: A – C – B – E – D

43 Results of Traversals To determine the results of a traversal on a given tree draw a path around the tree. start on the left side of the root and trace around the tree. The path should stay close to the tree. 12 in order: process when pass underneath node pre order: process when pass down left side of node post order: process when pass up right side of node 49 42 13 5

44 Breadth First - Depth First
Breadth First Search: Any search algorithm that considers neighbors of a vertex (node), that is, outgoing edges (links) of the vertex's predecessor in the search, before any outgoing edges of the vertex Depth First Search: Any search algorithm that considers outgoing edges (links to children) of a vertex (node) before any of the vertex's (node) siblings, that is, outgoing edges of the vertex's predecessor in the search. Extremes are searched first.

45 Breadth First A level order traversal of a tree could be used as a breadth first search Search all nodes in a level before going down to the next level

46 Breadth First Search of Tree
G X Z O W Q P U K B Z L M R

47 Breadth First Search search level 0 first C Find Node with B A G X Z O
Q P U K B Z L M R

48 Breadth First Search Find Node with B search level 1 next C A G X Z O
Q P U K B Z L M R

49 Breadth First Search Find Node with B search level 2 next C A G X Z O
Q P U K B Z L M R

50 Breadth First Search Find Node with B search level 3 next C A G X Z O
Q P U K B Z L M R

51 Depth First Search C Find Node with B A G X Z O W Q P U K B Z L M R

52 Depth First Search recursive pass 1 C Find Node with B A G X Z O W Q P
K B Z L M R

53 Depth First Search recursive pass 2 C Find Node with B A G X Z O W Q P
K B Z L M R

54 Depth First Search recursive pass 3 C Find Node with B A G X Z O W Q P
K B Z L M R

55 Depth First Search recursive pass 4 C Find Node with B A G X Z O W Q P
K B Z L M R

56 Depth First Search recursive pass 5 C Find Node with B A G X Z O W Q P
K B Z L M R

57 Depth First Search recursive pass 6 C Find Node with B A G X Z O W Q P
K B Z L M R

58 BFS - DFS Breadth first search typically implemented with a Queue
Depth first search typically implemented with recursion Which technique to use? depends on the problem

59 DFS Recursive Search/Traverse
Example: public bool DFS(TreeNode node, E element) { if (node == null) return false; else if (node.element == element) return true; if (DFS(node.left, element)) if (DFS(node.right, element)) }

60 Binary Tree Database Implementation
Since a BinaryTree is a non-linear list, none of the tables we previously created will be useful anymore. Instead we will create a new table to store tree nodes called the TreeNodes Table:

61 Binary Tree Database Implementation
We also need a table to tell us what the ID is of the root node for the List. This table will be called the TreeRoots Table:

62 Binary Tree Database Implementation
The following method gets a TreeNode when given its ID:

63 Binary Tree Database Implementation
And, finally, the following gets the value of the element stored in the node for given its ID, as well getting the IDs of the left and right nodes also.


Download ppt "Data Structures and Database Applications Binary Trees in C#"

Similar presentations


Ads by Google