1 / 16

Tree Data Structures

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 Nodes Each node can have 0 or more children A node can have at most one parent

Télécharger la présentation

Tree Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Tree Data Structures

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

  3. Trees Data Structures • 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

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

  5. 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 3 3 2 3 3 1 3 0 3 0 0 0 0 4 0 4 4 0

  6. + * E * D C / B A 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 back

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

  8. Tree traversal Two types of tree traversal are; Recursive Non recursive back

  9. Recursive traversal • Preorder: node, left, right • Inorder: left, node, right • Postorder: left, right, node back

  10. 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

  11. 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

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

  13. 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

  14. Threaded Binary Trees • Two many null pointers in current representationof binary trees n: number of nodes number of non-null links: n-1 total links: 2nnull links: 2n-(n-1)=n+1 • Replace these null pointers with some useful “threads”. back

  15. Applications • Pre-order traversal while duplicating nodes and values can make a complete duplicate of a binary tree. It can also be used to make a prefix expression (Polish notation) from expression trees: traverse the expression tree pre-orderly. • In-order traversal is very commonly used on binary search trees because it returns values from the underlying set in order, according to the comparator that set up the binary search tree (hence the name). • Post-order traversal while deleting or freeing nodes and values can delete or free an entire binary tree. It can also generate a postfix representation of a binary tree. back

  16. Thank You

More Related