首页 > 代码库 > 二叉树链式存储结构
二叉树链式存储结构
-
二叉链表的C语言描述
-
基本运算的算法——建立二叉链表、先序遍历二叉树、中序遍历二叉树、后序遍历二叉树、后序遍历求二叉树深度
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | #include<iostream> #include<cstdio> using namespace std; class Tree { private : struct Node { char data; Node * lchild; Node * rchild; Node() { lchild = NULL; rchild = NULL; } Node( char a) { data = http://www.mamicode.com/a; lchild = NULL; rchild = NULL; } }; void creatTree(Node* &head) { char a; cin>>a; if (a == ‘#‘ ) { head->lchild = head->rchild = NULL; head = NULL; } else { head->data = http://www.mamicode.com/a; head->lchild = new Node(); head->rchild = new Node(); creatTree(head->lchild); creatTree(head->rchild); } } void NLR(Node *head) { if (head) { cout<<head->data; NLR(head->lchild); NLR(head->rchild); } else cout<< "#" ; } void LNR(Node *head) { if (head) { LNR(head->lchild); cout<<head->data; LNR(head->rchild); } else cout<< "#" ; } void LRN(Node * head) { if (head) { LRN(head->lchild); LRN(head->rchild); cout<<head->data; } else cout<< "#" ; } public : Node * head; Node * cur; Tree() {} Tree( char a) { Node * tem = new Node(a); head = tem; cur = tem; } void creatTree() { head = new Node(); creatTree(head); } void NLR() { if (head) NLR(head); else cout<< "#" ; } void LNR() { if (head) LNR(head); else cout<< "#" ; } void LRN() { if (head) { LRN(head); } else cout<< "#" ; } int BiTreeDeep(Node * head) { num++; int dept = 0; if (head) { int lchilddept = BiTreeDeep(head->lchild); int rchilddept = BiTreeDeep(head->rchild); dept = lchilddept >= rchilddept ? (lchilddept + 1) : (rchilddept + 1); } return dept; } }; int main() { Tree dusk; dusk.creatTree(); int cnt = 0; dusk.NLR(); cout<<endl; dusk.LNR(); cout<<endl; dusk.LRN(); cout<<endl; cout<<dusk.BiTreeDeep(dusk.head); } |
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。