qq


 1 int main()
 2 {
 3     int choice, num, tt = 0;
 4     RBtree<int> tr;
 5     PrintUsage();
 6     cout << endl;
 7     cout << "\t\t*Please Press:";
 8     cin >> choice;      //输入选择的操作
 9     while (choice != 0)
10     {
11         tt++;
12         if (choice < 6)
13             cin >> num;      //输入 插入 or 删除 or 查找 的数据
14         switch(choice) {
15             case(1):      //选择1,插入数据
16                 cout << "\033[32m" << "\t\tInsert - " << num << endl << "\033[0m";
17                 tr.Insert(num);
18                 break;
19             case(2):      //选择2,删除数据
20                 cout << "\033[32m" << "\t\tDelete - " << num << endl << "\033[0m";
21                 tr.Delete(num);
22                 break;
23             case(3):      //选择3,查找数据
24                 cout << "\033[32m" << "\t\tFind - " << num << endl << "\033[0m";
25                 if (tr.Find(num))
26                     cout << "\033[32m" << "\t\t" << num << " exist in tree" << endl << "\033[0m";
27                 else
28                     cout << "\033[32m" << "\t\t" << num << " doesn't exist in tree" << endl << "\033[0m";
29                 break;
30             case(4):      //选择4,查找后续结点
31                 if (!tr.Find(num))
32                     cout << "\033[31m" << "\t\t" << num << " doesn't exist in tree" << endl << "\033[0m";
33                 else if (tr.Max(num) == num)
34                     cout << "\033[32m" << "\t\t" << num << " doesn't have the successor" << endl << "\033[0m";
35                 else
36                 {
37                     cout << "\033[32m" << "\t\tthe successor of " << num << " is: " << "\033[0m";
38                     cout << "\033[32m" << tr.Succ(num) << endl << "\033[0m";
39                 }
40                 break;
41             case(5):      //选择5,查找前向结点
42                 if (!tr.Find(num))
43                     cout << "\033[31m" << "\t\t" << num << " doesn't exist in tree" << endl << "\033[0m";
44                 else if (tr.Min(num) == num)
45                     cout << "\033[32m" << "\t\t" << num << " doesn't have the predecessor" << endl << "\033[0m";
46                 else
47                 {
48                     cout << "\033[32m" << "\t\tthe predecessor of " << num << " is: " << "\033[0m";
49                     cout << "\033[32m" << tr.Pred(num) << endl << "\033[0m";
50                 }
51                 break;
52             case(6):      //选择6,查找最小值
53                 cout << "\033[32m" << "\t\tthe min value of the tree is: " << "\033[0m";
54                 cout << "\033[32m" << "\t\t" << tr.Min(num) << endl << "\033[0m";
55                 break;
56             case(7):      //选择7,查找最大值
57                 cout << "\033[32m" << "\t\tthe max value of the tree is: " << "\033[0m";
58                 cout << "\033[32m" << "\t\t" << tr.Max(num) << endl << "\033[0m";
59                 break;
60             case(8):      //选择8,打印二叉树
61                 cout << "\033[32m" << "\t\tprinting tree" << endl << "\033[0m";
62                 tr.Print();
63                 break;
64             case(9):      //选择9,对树上数据进行从大到小排序
65                 cout << "\033[32m" << "\t\tSort from smallest to biggest" << "\033[0m";
66                 cout << "\t\t";
67                 tr.SortA();
68                 break;
69             case(10):      //选择10,对树上数据进行从小到大排序
70                 cout << "\033[32m" << "\t\tSort from biggest to smallest" << "\033[0m";
71                 cout << "\t\t";
72                 tr.SortD();
73                 break;
74             case(11):      //选择11,求树上数据的平均数值
75                 cout << "\033[32m" << "\t\tGet Average " << "\033[0m";
76                 tr.Average();
77                 break;
78             case(12):      //选择12,竖向输出二叉树
79                 cout << "\033[32m" << "\t\tTest" << endl << "\033[0m";
80                 cout << "\t\t";
81                 tr.PPT();
82                 break;
83             default:      //输入非法,重新输入
84                 cout << "\033[32m" << "\t\tthe choice number is out of range." << endl << "\033[0m";
85                 cout << "\033[32m" << "\t\tPlease enter again!" << endl << "\033[0m";
86         }
87         cout << "\033[36m" << "\t\t===========================================================================" << endl << "\033[0m";
88         cout << "\t\t*Please Press:";
89         cin >> choice;
90     }
91     cout << "\t\t";
92     return 0;
93 }

 

 1 template <class T>
 2 void RBtree<T>::PPT()
 3 {
 4     RBnode *x = ROOT;
 5     queue<ass> q;
 6     ass d, l, o, u;
 7     d.dep = 1;
 8     d.rr = x;
 9     q.push(d);      //根结点入队
10     for (int i = 1; i <= pow(2, double(ww - 1)) - 1; i++)      //输出空格控制格式
11         cout << " ";
12     while (!q.empty())      //执行至所有结点遍历结束
13     {
14         d = q.front();
15         if (d.dep < ww)      //防止叶子结点无限向下拓展
16         {
17             l.dep = d.dep + 1;
18             l.rr = d.rr -> left;
19             o.dep = d.dep + 1;
20             o.rr = d.rr -> right;
21             q.push(l);
22             q.push(o);
23         }
24         q.pop();      //拓展完的结点退队
25         if (!q.empty())
26         {
27             u = q.front();
28             if (d.rr != NIL)
29             {
30                 if (d.rr -> color == Black)
31                     cout << d.rr -> value;
32                 else
33                     cout << "\033[31m" << d.rr -> value << "\033[0m";
34             }
35             else
36                 cout << " ";
37             if (d.dep == u.dep)
38             {
39                 for (int i = 1; i <= pow(2, double(ww - d.dep + 1)) - 1; i++)      //输出空格控制格式
40                     cout << " ";
41             }
42             else
43             {
44                 cout << endl;
45                 cout << "\t\t";
46                 for (int i = 1; i <= pow(2, double(ww - u.dep)) - 1; i++)      //输出空格控制格式
47                     cout << " ";
48             }
49         }
50         else
51         {
52             if (d.rr != NIL)
53             {
54                 if (d.rr -> color == Black)
55                     cout << d.rr -> value;
56                 else
57                     cout << "\033[31m" << d.rr -> value << "\033[0m";
58             }
59             else
60                 cout << " ";
61         }
62     }
63     cout << endl;
64 }

 

posted @ 2023-06-01 10:33  Z_MIKI  阅读(37)  评论(0)    收藏  举报