# Tree Data Structures.

## Presentation on theme: "Tree Data Structures."— Presentation transcript:

Tree Data Structures

Topics to be discussed….
Trees Data Structures Trees Binary Search Trees Tree traversal Types of Binary Trees Threaded binary trees Applications

Trees Data Structures Tree Binary Tree back Tree Nodes
Each node can have 0 or more children A node can have at most one parent Binary tree Tree with 0–2 children per node Tree Binary Tree back

Trees Root node Height Interior nodes Leaf nodes Terminology
Root  no parent Leaf  no child Interior  non-leaf Height  distance from root to leaf Root node Height Interior nodes Leaf nodes

Level and Depth Level 1 2 3 4 node (13) degree of a node leaf (terminal) Non terminal parent children sibling degree of a tree (3) ancestor level of a node height of a tree (4) 3 1 2 2 1 2 3 2 2 3 3 3 1 3 3 3 4 4 4

Arithmetic Expression Using BT
+ inorder traversal A / B * C * D + E infix expression preorder traversal + * * / A B C D E prefix expression postorder traversal A B / C * D * E + postfix expression level order traversal + * E * D / C A B * E * D / C A B back

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

Two types of tree traversal are; Recursive Non recursive
back

Recursive traversal back Preorder: node, left, right
Inorder: left, node, right Postorder: left, right, node back

Non recursive traversal
In this stack is used to implement the non recursive traversal. All nodes first put into the stack and then each node is processed according to traversal. back

Not a binary search tree
Binary Search Trees 5 10 10 2 45 5 30 5 45 30 2 25 45 2 25 30 10 25 Not a binary search tree Binary search trees

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

Degenerate binary tree
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 back